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 for sum calculation

0

Hello,

I found the latest solution i.e. Record Value through Remote data is working super fast. Once again Thank you for the same.

Therefore, I am trying to incorporate the same type of coding in our old solution i.e.Record Number of sum to formula based cell - Excel.

I have tried coding with your reference of latest solution.

Below code in sheet2:

Private Sub Worksheet_Calculate()
    Call ChkEvnts
End Sub

Below code in Module:

Sub 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)


        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


            Rpt.Offset(0, 4).Value = Now
            If Cll.Offset(0, 1).Value > 0 Then
                    Rpt.Offset(0, 2).Value = Rpt.Offset(0, 2).Value + Cll.Value
                ElseIf Cll.Offset(0, 1).Value < 0 Then
                    Rpt.Offset(0, 3).Value = Rpt.Offset(0, 3).Value + Cll.Value
            End If
            With Rpt.Offset(0, -1).Resize(1, 6)
                 .Interior.Color = vbGreen
            End With

            Else:
            With Rpt.Offset(0, -1).Resize(1, 6)
                 .Interior.Color = vbWhite
            End With
        End If
    Next Cll


    Application.EnableEvents = True
End Sub

Can you help me understand where i am lacking in getting the result. I am working to get the result like Record Number of sum to formula based cell - Excel with coding reference of Record Value through Remote data

If the Rate changes in column C and increase from past rate, than the respected quantity in Column D to add to Column E and if Rate changes in column C and decrease from past rate, than the respected quantity D in to add in column F.

Can you please able to share your expert input. 

Regards,

Answer
Discuss

Discussion

Sunil

Firstly, you shouldn't address your question to me- anyone should feel free to Answer you (though it's likely to be me)

I won't try to answer you since you have not made your question clear- e.g. which column change in the Remote file triggers the update?

Furthermore it seems to me you've done little to try to change the code to suit this situation (apart from removing the comments in mine). That or you didn't understand the code form the previous Answer (and I'm just wasting my time!)
John_Ru (rep: 6142) Mar 7, '24 at 9:05 am
Hi,
Thank you for your response. I regret for inconvenience caused.
I have amend the main question:
The column change in remote file is as per below:
If the Rate changes in column C and increase from past rate, than the respected quantity in Column D to add to Column E and if Rate changes in column C and decrease from past rate, than the respected quantity D in to add in column F.
Further, I have changed the code, as i feel that was only change required in the coding to work, rest coding is good to go.
Request you to please guide with your expertise.

Regards,
SunilA (rep: 58) Mar 8, '24 at 12:49 am
Add to Discussion

Answers

0
Selected Answer

Sunil

Thanks for adding detail to your question but I think you got the column references wrong..

To implement that approach, your mistakes were to look in the quantity column (but you explained that it was changes in rates which should trigger the updates) and not to change the If tests to check those rates.

In the attached .xlsm file, I've modified the module code (with comments and some but not all changes in bold):

Sub ChkRates()

    Application.EnableEvents = False

    Dim Rpt As Range
    Dim Cll As Range

    ' look at the rates not quantities
    For Each Cll In Sheet2.Range("D3:D25") ' was ("C3:C25")
        Set Rpt = Sheet1.Range(Cll.Address)

    ' see if rate changed
       If IsNumeric(Cll.Value) And Cll.Value <> Rpt.Value Then ' 0 Then _
'            And Cll.Offset(0, 1).Value <> Rpt.Offset(0, 1).Value _
'            And Cll.Offset(0, 1).Value <> "" Then

            ' see if rate increased
            If Cll.Value > Rpt.Value Then 'Offset(0, 1).Value > 0 Then
                    Rpt.Offset(0, 1).Value = Rpt.Offset(0, 1).Value + Cll.Offset(0, -1).Value
                ElseIf Cll.Value < Rpt.Value Then
                    Rpt.Offset(0, 2).Value = Rpt.Offset(0, 2).Value + Cll.Offset(0, -1).Value
            End If

            ' write new values
            Rpt.Offset(0, 3).Value = Now
            Rpt.Offset(0, -1).Value = Cll.Offset(0, -1).Value
            Rpt.Value = Cll.Value
            ' highlight row
            With Rpt.Offset(0, -2).Resize(1, 6)
                 .Interior.Color = vbGreen
            End With

            Else:
            With Rpt.Offset(0, -2).Resize(1, 6)
                 .Interior.Color = vbWhite
            End With
        End If
    Next Cll


    Application.EnableEvents = True
End Sub

and the Sheet2 code to:

Private Sub Worksheet_Calculate()
    Call ChkRates
End Sub

I also renamed your "remote" file to match the VLOOKUPs in Sheet2 (though you may need to alter them).

Now if you launch both files and change a (quantity followed by a) rate in the remote  .xlsx file, the sheet1 values should be added and highlighted correctly.

Hope this fixes your problem. If so, please mark this Answer as Selected.

Discuss

Discussion

The said answer has solved my question perfectly.Thank you for your very kind support and expert input. 
Regards,
SunilA (rep: 58) Mar 10, '24 at 12:39 am
Add to Discussion


Answer the Question

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