I'm a VBA beginner. I get a 'run time error 91 object variable or with block variable not set' trying to run the following code. Here is what I'm trying to do:
- Select all data in my worksheet
- Name the selection to AllData
- Go through this range and wherever 'X' is found in Column D, change value of Column W to 'No'.
The error refers to the 9th row where I try to set No_Of_Rows to the row count of my range. Apparently I should 'Set' my object beforehand..? But I don't know what I'm doing wrong...
Thanks in advance for your help.
Sub ChngColW()
Dim AllData As Range
Dim No_Of_Rows As Integer
Dim i As Long
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Name = "AllData"
No_Of_Rows = AllData.Rows.Count
For i = 1 To No_Of_Rows
If AllData("D" & i).Value = "X" Then
AllData("W" & i).Value = "No"
End If
Next i End Sub
AllDatais the name of a range, not the name of a range variable. UseSet AllData = Selectioninstead of naming it.