VBA Code Optimisation

scott_od

New Member
Joined
Jan 25, 2016
Messages
27
I am fairly new to VBA and have created a file where the VBA code seems excessive, due to repetition
My feeling is that it can be optimised, but I am not sure of the best way to do it, so I was looking for some guidance on how it could be rewritten.

Below is a part of the code (to update validation input messages) which uses the Worksheet Change event & as you can see, this is the same code repeated, but with varying targets & output values

VBA Code:
If Target.Address = "$G$2" Then
With Worksheets("Gantt Chart").Range("$F$3").Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.InputTitle = "Comment"
.InputMessage = Sheet3.Range("$G$2").Value
End With
End If

If Target.Address = "$G$3" Then
With Worksheets("Gantt Chart").Range("$F$4").Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.InputTitle = "Comment"
.InputMessage = Sheet3.Range("$G$3").Value
End With
End If

If Target.Address = "$G$4" Then
With Worksheets("Gantt Chart").Range("$F$5").Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.InputTitle = "Comment"
.InputMessage = Sheet3.Range("$G$4").Value
End With
End If

If Target.Address = "$G$5" Then
With Worksheets("Gantt Chart").Range("$F$6").Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.InputTitle = "Comment"
.InputMessage = Sheet3.Range("$G$5").Value
End With
End If

Any advise would be gratefully received. Thanks :)
 

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.
What is the name of the worksheet that this code is in?
 
Upvote 0
If you're looking to just reduce the size, you can use a loop (especially since this is so uniform).
VBA Code:
Dim i As Integer
i = 2
Do While i <= 5
    If Target.address = "$G$" & i Then
        With Worksheets("Gantt Chart").Range("$F$" & i + 1).Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, operator _
            :=xlBetween
            .InputTitle = "Comment"
            .InputMessage = Sheet3.Range("$G$" & i).Value
        End With
    End If
    i = i + 1
Loop
 
Upvote 0
If you're looking to just reduce the size, you can use a loop (especially since this is so uniform).
VBA Code:
Dim i As Integer
i = 2
Do While i <= 5
    If Target.address = "$G$" & i Then
        With Worksheets("Gantt Chart").Range("$F$" & i + 1).Validation
            .Delete
            .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, operator _
            :=xlBetween
            .InputTitle = "Comment"
            .InputMessage = Sheet3.Range("$G$" & i).Value
        End With
    End If
    i = i + 1
Loop
Thank you - I think this is exactly what I was looking for, I just wasn't sure how to connect target & the output
 
Upvote 0
Yes that is correct
In that case I don't think that you need any looping at all. See if this does the job.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.CountLarge = 1 And Not Intersect(Target, Range("G2:G5")) Is Nothing Then
    With Worksheets("Gantt Chart").Range("F" & Target.Row + 1).Validation
      .Delete
      .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween
      .InputTitle = "Comment"
      .InputMessage = Target.Value
    End With
  End If
End Sub
 
Upvote 0
Solution
Private Sub Worksheet_Change(ByVal Target As Range) If Target.CountLarge = 1 And Not Intersect(Target, Range("G2:G5")) Is Nothing Then With Worksheets("Gantt Chart").Range("F" & Target.Row + 1).Validation .Delete .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween .InputTitle = "Comment" .InputMessage = Target.Value End With End If End Sub

@Peter_SSs that works perfectly - thank you so much!
How would I adapt that solution to another variation which already has target range (same sheet as the previous scenario & input message from the same sheet)?

VBA Code:
If Not Application.Intersect(Target, Range("H2:L2")) Is Nothing Then
With Worksheets("Gantt Chart").Range("$H$3").Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.InputTitle = "FTE"
.InputMessage = Sheet3.Range("$Z$2").Value
End With
End If

If Not Application.Intersect(Target, Range("H3:L3")) Is Nothing Then
With Worksheets("Gantt Chart").Range("$H$4").Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.InputTitle = "FTE"
.InputMessage = Sheet3.Range("$Z$3").Value
End With
End If

If Not Application.Intersect(Target, Range("H4:L4")) Is Nothing Then
With Worksheets("Gantt Chart").Range("$H$5").Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.InputTitle = "FTE"
.InputMessage = Sheet3.Range("$Z$4").Value
End With
End If

If Not Application.Intersect(Target, Range("H5:L5")) Is Nothing Then
With Worksheets("Gantt Chart").Range("$H$6").Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.InputTitle = "FTE"
.InputMessage = Sheet3.Range("$Z$5").Value
End With
End If
 
Upvote 0
How would I adapt that solution to another variation which already has target range (same sheet as the previous scenario & input message from the same sheet)?
Is this to be combined with the previous worksheet_change code or does it replace that previous code?
 
Upvote 0

Forum statistics

Threads
1,215,597
Messages
6,125,738
Members
449,255
Latest member
whatdoido

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