Can I transform a Worksheet_Change event in a normal macro?

gubertu

Board Regular
Joined
May 24, 2015
Messages
147
Hi all,

I have the following code. This is a Worksheet_Change event and I would like to know if it is possible to modify this code so I can execute it when I want, as a normal macro, pressing F5.

Thanks in advance!


VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("BV1:BV" & Range("CJ" & Rows.Count).End(xlUp).Row)) Is Nothing Then
    Application.ScreenUpdating = False
    With Sheets("SYS Agregado")
        For MY_ROWS = 2 To .Range("CJ" & Rows.Count).End(xlUp).Row
                If .Range("CJ" & MY_ROWS).Value & .Range("CL" & MY_ROWS).Value = _
                    .Range("CJ" & Target.Row).Value & .Range("CL" & Target.Row).Value Then
                        MY_TOTAL = MY_TOTAL + .Range("BV" & MY_ROWS).Value
                End If
        Next MY_ROWS
        Sheets(.Range("CJ" & Target.Row).Value).Range(.Range("CL" & Target.Row).Value).Value = MY_TOTAL
    End With
    Application.ScreenUpdating = True
    End If
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Can you post some sample data, and explain exactly what you want this to do?
 
Upvote 0
Hi,

I received that code in one question I did years ago. The link is the following.

Hope you could help me.

Thanks!

 
Upvote 0
See if this does what you want:
VBA Code:
Sub MyMacro()

    Dim MY_ROWS As Long
    Dim MY_TOTAL As Double
    Dim sht As String
    Dim rng As String

    With Sheets("Data")
        For MY_ROWS = 2 To .Range("A" & Rows.Count).End(xlUp).Row
'           Build sheet name and range variables
            sht = .Range("A" & MY_ROWS)
            rng = .Range("B" & MY_ROWS)
'           Find value from other sheet and add it to current row
            MY_TOTAL = Sheets(sht).Range(rng).Value + .Range("C" & MY_ROWS).Value
'           Put new value on other sheet
            Sheets(sht).Range(rng).Value = MY_TOTAL
        Next MY_ROWS
    End With
    
    MsgBox "Macro complete!"
    
End Sub
Note that you will need to assign it to a keyboard shortcut or button. I don't know that you can assign it to F5, as that is the refresh button.
 
Upvote 0
Thanks a lot!

It is possible to modify the code so it works even if the column B "Destination cell" has empty cells, as in the example below?

Thanks in advance!


Sheet "Data".

ABC
1Tab NameDestination CellValue
2BSA11.000
3BS500
4BSA21.000
5PLA15.000
6PL6.000
 
Upvote 0
It is possible to modify the code so it works even if the column B "Destination cell" has empty cells, as in the example below?
What should it do in that case?
Just ignore the entry?
 
Upvote 0
Just add an "IF" statement in there to check the value in column B, i.e.
VBA Code:
Sub MyMacro()

    Dim MY_ROWS As Long
    Dim MY_TOTAL As Double
    Dim sht As String
    Dim rng As String

    With Sheets("Data")
        For MY_ROWS = 2 To .Range("A" & Rows.Count).End(xlUp).Row
'           Check to see if there is a value in column B
            If .Range("B" & MY_ROWS) <> "" Then
'               Build sheet name and range variables
                sht = .Range("A" & MY_ROWS)
                rng = .Range("B" & MY_ROWS)
'               Find value from other sheet and add it to current row
                MY_TOTAL = Sheets(sht).Range(rng).Value + .Range("C" & MY_ROWS).Value
'               Put new value on other sheet
                Sheets(sht).Range(rng).Value = MY_TOTAL
            End If
        Next MY_ROWS
    End With
    
    MsgBox "Macro complete!"
    
End Sub
 
Upvote 0
I don't know that you can assign it to F5, as that is the refresh button
;)
ScreenShot001.png
 
Upvote 0

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top