Can't set macro range in VBA

Bobba the Fat

New Member
Joined
Mar 8, 2017
Messages
2
Hello.

I need my macro to automatically multiply the value I enter in the cell (by *9.81) and to return the result to the same cell. I have gotten this code, but I can't set the Range of my macro to work in. I would like this macro to work just in columns BH:BJ, BM:BO, BR:BT, BW:BY, CB:CD, CG:CI, CL:CN and CQ:CS.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target.Value <> "" Then
Application.EnableEvents = False
Target.Value = Target.Value * 9.81
End If
errhandler:
Application.EnableEvents = True
End Sub

Thank you for your cooperation!
May the force be with you
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, d As Range, e As Range
Set d = Union(Range("BH:BJ"), Range("BM:BO"), Range("BR:BT"), Range("BW:BY"), Range("CB:CD"), Range("CG:CI"), Range("CL:CN"), Range("CQ:CS"))
Set e = Intersect(Target, d)
If e Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each c In e
    If c <> "" And IsNumeric(c) Then c = c * 9.81
Next
Application.EnableEvents = True
End Sub
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("BH:BJ,BM:BO,BR:BT,BW:BY,CB:CD,CG:CI,CL:CN,CQ:CS")) Is Nothing Then
If Target.Count > 1 Then Exit Sub
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Application.EnableEvents = False
Target.Value = Target.Value * 9.81
Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Scott would you please explain how this code works? I m a beginner in vba
 
Upvote 0
It assigns multiple ranges to one range object.
It then checks to see if Target intersects with any of the cells in the range stored in variable d.
If it returns Nothing the values weren't in the range, and then exits the sub
It then loops through every cell that was in your range, this would allow you to say copy a value into multiple cells and still have it function.
If the value is numeric and not blank it multiples that value by 9.81
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("BH:BJ,BM:BO,BR:BT,BW:BY,CB:CD,CG:CI,CL:CN,CQ:CS")) Is Nothing Then
If Target.Count > 1 Then Exit Sub
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Application.EnableEvents = False
Target.Value = Target.Value * 9.81
Application.EnableEvents = True
End If
End Sub

Thank you, it works perfectly!
All the best
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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