Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Record Value through Remote data

0

Hello,

I trying below concept in below coding:

Whenever in the Column C, there is any change from "Empty value" to "Buy" or from "Sell" to "Buy" than the respected quantity available in Column D to add to Column E. 

Whenever in the Column C, there is any change from "Empty value" to "Sell" or from "Buy" to "Sell" than the respected quantity available in Column D to add to Column F 

For example, If whenever there is any change in Trade Column from Blank value to Buy or from Sell to Buy (positive move), than the given quantity i.e. 100 in quantity column to add to Total Buy column and If whenever there is any change in Trade Column from Blank value to Sell or from Buy to Sell (negative move), than the given quantity i.e. 100 in quantity column to add to Total Sell column and so on.

I am trying the VBA coding based on your solution given in Record Number of sum to formula based cell - Excel , but stuck in getting the desired result.

Below is the code in Sheet1:

Private Sub Worksheet_Change(ByVal Target As Range)


If Intersect(Target, Range("C:C")) Is Nothing Or Not IsNumeric(Target.Value) Then Exit Sub

Dim Rmt As Range

Set Rmt = Sheet2.Range(Target.Address)

Application.EnableEvents = False

Select Case Target.Value - Rmt.Value

    Case Is = "Buy"
        Target.Offset(0, 2).Value = Rmt.Offset(0, 1).Value
    Case Is = "Sell"
        Target.Offset(0, 3).Value = Rmt.Offset(0, 1).Value
    Case Is = 0

    Case Else

End Select

Target.Value = Rmt.Value
Target.Offset(0, 1).Value = Rmt.Offset(0, 1).Value

Application.EnableEvents = True

End Sub

Below is the code in Sheet2:

Private Sub Worksheet_Calculate()


Dim Rpt As Range, Cll As Range

Application.EnableEvents = False
Application.Calculation = xlCalculationManual

For Each Cll In Sheet2.Range("C3:C200")
    If Not IsNumeric(Cll.Value) Then

        Set Rpt = Sheet1.Range(Cll.Address)

        Select Case Cll.Value
            Case Is = "Buy"
                Rpt.Offset(0, 2).Value = Rpt.Offset(0, 2).Value + Cll.Offset(0, 1).Value
            Case Is = "Sell"
                Rpt.Offset(0, 3).Value = Rpt.Offset(0, 3).Value + Cll.Offset(0, 1).Value
            Case Is = 0

            Case Else

        End Select
        Rpt.Offset(0, -1).Value = Cll.Offset(0, -1).Value
        Rpt.Value = Cll.Value
        Rpt.Offset(0, 1).Value = Cll.Offset(0, 1).Value
    End If
 Next Cll

Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic


End Sub

Attached the excel sheet.

Please guide.

Thanks,

Answer
Discuss

Discussion

Hi John,
I am trying to apply whichever i was able to learn from you from the past Answers in the said coding, I am struggling in getting result.
Can I request you for your expert input.
Thanks
SunilA (rep: 58) Feb 21, '24 at 11:58 am
Hi John,
Thank you very much for your Answer.
When I run the excel with given code, after a while the computer gets shut down. I have tested number of time, but i don't know the reason behind the issue. Can you please guide me on the same.
Further, I have tried to write the same type of code (above written in main question) which you helped in my past question "Record Number of sum to formula based cell - Excel" which is working fine. Once again Thank you for the same. I have almost succeeded in getting the result (refer to code mention in main question), but i am unable to figure out to connect with the previous value through the code. Can you please help to fix code same like past Answer.
Please guide me Sir with your expertise.
Thank you
SunilA (rep: 58) Feb 27, '24 at 6:50 am
Sunil. Please see my Answer below 
John_Ru (rep: 6142) Feb 27, '24 at 1:51 pm
Hi Sir,
Sorry, I have gone through the below answer, but didn't understand what you want to say.
Can you please help me understand.
Thanks
SunilA (rep: 58) Feb 27, '24 at 4:17 pm
Sunil

Just try my. xlsm file but in Sheet2 make sure that the VLookup formulae point to wherever you have stored the remote file (not to a location including John in the filename!) . It worked well for me 
John_Ru (rep: 6142) Feb 27, '24 at 5:48 pm
Thank you John Sir for your valuable Answer.
You are really a excel expert Genius. 
SunilA (rep: 58) Feb 28, '24 at 3:56 am
Add to Discussion

Answers

0
Selected Answer

Sunil (/Nitil A)

In the attached (.xlsm) file, I've disabled your code behind sheet 1 and changed the prodcure behind sheet 2 to be simply:

Private Sub Worksheet_Calculate()
    Call ChkEvnts
End Sub

and added this in Module1:

ub ChkEvnts()


    Application.EnableEvents = False

    Dim Rpt As Range
    Dim Cll As Range

    For Each Cll In Sheet2.Range("C3:C25")
        Set Rpt = Sheet1.Range(Cll.Address)

        'check Buy/Sell status changed
        If IsNumeric(Cll.Value) And Cll.Value <> 0 _
            And Cll.Offset(0, 1).Value <> Rpt.Offset(0, 1).Value _
            And Cll.Offset(0, 1).Value <> "" Then

            ' add timestamp to G
            Rpt.Offset(0, 4).Value = Now
            If Cll.Offset(0, 1).Value = "Buy" Then
                    'write new Buy trade values and sum
                    Rpt.Offset(0, 2).Value = Rpt.Offset(0, 2).Value + Cll.Value
                    Rpt.Value = Cll.Value
                    Rpt.Offset(0, 1).Value = Cll.Offset(0, 1).Value
                ElseIf Cll.Offset(0, 1).Value = "Sell" Then
                    'write new Sell trade values and suum
                    Rpt.Offset(0, 3).Value = Rpt.Offset(0, 3).Value + Cll.Value
                    Rpt.Value = Cll.Value
                    Rpt.Offset(0, 1).Value = Cll.Offset(0, 1).Value
            End If
            ' indicate last trades with colour
            With Rpt.Offset(0, -1).Resize(1, 6)
                 '.ClearContents
                 .Interior.Color = vbGreen
            End With

            Else:
            ' remove colour but leave last value
            With Rpt.Offset(0, -1).Resize(1, 6)
                 '.ClearContents
                 .Interior.Color = vbWhite
            End With
        End If
    Next Cll


    Application.EnableEvents = True
End Sub

You may need to restore the VLOOKUP formulae in column D of sheet 2 (to point to location of the second Remote file, which needs to be opened). Note that I added Data Validation to  the cells in column D so you can only pick Buy, Sell or blank (using --). This has to be done AFTER the trade quantity is set.

Only once a row in the Remote... file is changed to Buy or Sell (from blank or the opposite trade) is sheet 1 of the .xlsm file updated. The row is (or rows are) set to green to show the last transaction(s). Previous trades (including the timestamp in colkum G) are retained but any shading removed.

Hope this fixes things for you.

Discuss

Discussion

Thank you John Sir for your valuable answer.
You are really a excel Genius.
SunilA (rep: 58) Feb 28, '24 at 3:55 am
Glad that worked for you Sunil. Thanks for selecting my Answer. 
John_Ru (rep: 6142) Feb 28, '24 at 7:12 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login