Excel VBA Worksheet_Change

MaxMiller

New Member
Joined
Apr 6, 2011
Messages
11
I am trying to automatically change values by a percentage when they are entered based on criteria contained in a previous row.

moz-screenshot.png
I have tried to do this using the following code:

Public Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("I14:I44")) Is Nothing Then
With Target
If IsNumeric(.Value) Then
.Value = .Value * 1.08
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

Problem is I want to calculate a different percentage based on the values of "E14:E44" which are bound by the following list: "LTL", "Truckload", "Tank Truck" & "IM". I think some sort of Vlookup would be ideal but not sure if that can be incorporated into Worksheet_change.

Any help is greatly appreciated!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Whataver you tried to post image-wise did not display, so maybe if you explain a bit farther about what "bound by the following list: "LTL", "Truckload", "Tank Truck" & "IM"" means in terms of its relevance to "the values of "E14:E44"" which is 31 cells, many more than that first 4-item list. Having a hard time grasping what you are working with.
 
Upvote 0
Sorry for the cofusion... the cell reference has list data validation. If the user has chosen "LTL" in E14 and then enters a number value in I14, I want the number to be automatically multiplied by 1.08. If the user has entered TL, the number should be multiplied by a different percentage (1.1) or whatever.
 
Upvote 0
I was thinking maybe an if function in the VBA script looking for a certain value in cells "E14:E44" but could not get anything to work.
 
Upvote 0
If the percentages don't change often then they can be hard coded, otherwise a lookup table somewhere convenient can be assembled where you can change the percentages. Which is the situation...the percentages and "LTL" items change often, or not often.

And, just to be clear, is it that what is in cell E20 is what should be looked at when a number is entered into I20? is that what is going on?
 
Last edited:
Upvote 0
Correct. I want the percentage to be added to the numeric values in I14:I44 based on the Value in E14:E44. To use your example, if E20 = "LTL" then automatically multiply any value entered in I20 by 1.08. If E20 = "TL" then automatically multiply any value entered in I20 by 1.1.
 
Upvote 0
The percentages are directly correlated to the text. LTL = 1.08 ; TL = 1.1 ; Tank Truck = 1.09 ; IM = 1.12
 
Upvote 0
Delete the current SheetChange code you were using, and try this instead:

Code:
Public Sub Worksheet_Change(ByVal Target As Range)
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
Dim pctVal As Double
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 Sub
 
Upvote 0
So... If I want to do the same thing to rows J,K,&L; how would I duplicate the code... I copied the if and pasted but excel is giving me the error "Dim pctVal As Double".

the code looks like this...

Public Sub Worksheet_Change(ByVal Target As Range)

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
Dim pctVal As Double
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
Dim pctVal As Double
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
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,239
Members
452,898
Latest member
Capolavoro009

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