Worksheet_Change Multiple ranges

MaxMiller

New Member
Joined
Apr 6, 2011
Messages
11
I am trying to modify the following code:

Public Sub Worksheet_Change(ByVal Target As Range)
Dim pctVal As Double

If Intersect(Target, Range("I14:I44")) Is Nothing _
Or Target.Cells.Count > 1 Then Exit Sub
If IsEmpty(Target) = True Then Exit Sub
With Target
If IsNumeric(.Value) Then
Select Case Cells(.Row, .Column - 4).Value
Case "LTL": pctVal = 1.08
Case "TL": pctVal = 1.1
Case "Tank Truck": pctVal = 1.09
Case "IM": pctVal = 1.12
Case Else: pctVal = 1
End Select
Application.EnableEvents = False
.Value = .Value * pctVal
Application.EnableEvents = True
End If
End With

If Intersect(Target, Range("J14:J44")) Is Nothing _
Or Target.Cells.Count > 1 Then Exit Sub
If IsEmpty(Target) = True Then Exit Sub
With Target
If IsNumeric(.Value) Then
Select Case Cells(.Row, .Column - 5).Value
Case "LTL": pctVal = 1.08
Case "TL": pctVal = 1.1
Case "Tank Truck": pctVal = 1.09
Case "IM": pctVal = 1.12
Case Else: pctVal = 1
End Select
Application.EnableEvents = False
.Value = .Value * pctVal
Application.EnableEvents = True
End If
End With


The problem is that before the second If Intersect is reached, the sub is exited. I want to make this code update columns I, J, K & L.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Your two blocks look essentially the same, except for the range.

I haven't tried to figure out what you are trying to do, but note thay you can combine all the ranges together, so you might be able to do it in a single block like:
Code:
Public Sub Worksheet_Change(ByVal Target As Range)
 
    Dim pctVal As Double
 
    If Intersect(Target, Range("I14:I44,J14:J44,K14:K44,L14:L44")) Is Nothing _
        Or Target.Cells.Count > 1 Then Exit Sub
        If IsEmpty(Target) = True Then Exit Sub
            With Target
                If IsNumeric(.Value) Then
                    Select Case Cells(.Row, .Column - 4).Value
                        Case "LTL": pctVal = 1.08
                        Case "TL": pctVal = 1.1
                        Case "Tank Truck": pctVal = 1.09
                        Case "IM": pctVal = 1.12
                        Case Else: pctVal = 1
                    End Select
                    Application.EnableEvents = False
                    .Value = .Value * pctVal
                    Application.EnableEvents = True
                End If
            End With
        End If
    End If
 
End Sub
The only thing I noticed that looks like it changes is this line:
Select Case Cells(.Row, .Column - 4).Value

You should be able to edit the column reference to make it static, i.e.:
Select Case Cells(.Row, "E").Value
if need be (depending on what you are doing).
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,777
Members
452,942
Latest member
VijayNewtoExcel

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