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 :)
 
Is this to be combined with the previous worksheet_change code or does it replace that previous code?
I'll assume combined. If it was replace, then just remove the top block in the code below.

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
 
  If Not Intersect(Target, Range("H2:L5")) Is Nothing And Target.Rows.Count = 1 Then
    With Worksheets("Gantt Chart").Range("H" & Target.Row + 1).Validation
    .Delete
    .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween
    .InputTitle = "FTE"
    .InputMessage = Range("Z" & Target.Row).Value
    End With
  End If
End Sub
 
Last edited:
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I'll assume combined. If it was replace, then just remove the top block in the code below.

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
 
  If Not Intersect(Target, Range("H2:L5")) Is Nothing And Target.Rows.Count = 1 Then
    With Worksheets("Gantt Chart").Range("H" & Target.Row + 1).Validation
    .Delete
    .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween
    .InputTitle = "FTE"
    .InputMessage = Range("Z" & Target.Row).Value
    End With
  End If
End Sub
It was indeed combined. Thanks again Peter.
 
Upvote 0
You're welcome.

It was indeed combined.
In that case, since if the first condition is met then there is no need to test for the second one as it cannot also be met so the code would be marginally more efficient written like this.

Rich (BB 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
  ElseIf Not Intersect(Target, Range("H2:L5")) Is Nothing And Target.Rows.Count = 1 Then
    With Worksheets("Gantt Chart").Range("H" & Target.Row + 1).Validation
    .Delete
    .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween
    .InputTitle = "FTE"
    .InputMessage = Range("Z" & Target.Row).Value
    End With
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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