Johnny Thunder
Well-known Member
- Joined
- Apr 9, 2010
- Messages
- 693
- Office Version
- 2016
- Platform
- MacOS
Hello All,
I am working with a piece of code that works for the first if statement but is not triggering anything on the second If Statement.
Purpose of the code is to scan a validation column (Column W) and if there are any errors autofit the column so that the errors can be read easily. As the user corrects certain cells the validation comment shrinks and also does another Autofit. I have a second range in Column X that is a comment field that I would also like to autofit as the user enters text. Currently the code will only do the Column W Autofitbut bypasses the Column X.
Any idea what is wrong?
I am working with a piece of code that works for the first if statement but is not triggering anything on the second If Statement.
Purpose of the code is to scan a validation column (Column W) and if there are any errors autofit the column so that the errors can be read easily. As the user corrects certain cells the validation comment shrinks and also does another Autofit. I have a second range in Column X that is a comment field that I would also like to autofit as the user enters text. Currently the code will only do the Column W Autofitbut bypasses the Column X.
Any idea what is wrong?
VBA Code:
'Re-Autofit column V for Validation
Private Sub Worksheet_Calculate()
Dim rng As Range, cell As Range, rng2 As Range
Dim LastR As Long
LastR = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row
'see if any changes are in the monitored range...
Set rng = Range("W4:W" & LastR)
If Not rng Is Nothing Then
For Each cell In rng.Cells
If cell.Value <> "" Then
Range("W4").EntireColumn.AutoFit
End If
Next
End If
Set cell = Nothing
'see if any changes are in the monitored range...
Set rng2 = Range("X4:X" & LastR)
If Not rng2 Is Nothing Then
For Each cell In rng2.Cells
If cell.Value <> "" Then
Range("X4").EntireColumn.AutoFit
End If
Next
End If
Range("E3:X3").EntireColumn.AutoFit
With ActiveSheet
.EnableSelection = xlNoSelection
End With
End Sub