Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA, If value Like

Tags:

excel

vba

I have a column(x) and trying to do a If and like in another column(d). Getting a range error on the selection autofill part.

Dim src As Range
Set src = Worksheets("File").Range("2:17783")


    If (Range("x2").Value Like "*ProductType:'FXD';*") Then
                         Range("D2").Value = "FXD"
            ElseIf (Range("x2").Value Like "*;ProductSubType:'SWLEG'*") Then
                         Range("D2").Value = "XSW" 
            End If 

Selection.AutoFill Destination:=src.Columns("D")
like image 224
excelguy Avatar asked Mar 07 '23 22:03

excelguy


1 Answers

Instead of doing autofill just set the value at once.

Also do not forget to provide the sheet parent to all your range objects:

Dim src As Range
Set src = Worksheets("File").Range("2:17783")


If (Worksheets("File").Range("x2").Value Like "*ProductType:'FXD';*") Then
    Worksheets("File").Range("D2:D17783").Value = "FXD"
ElseIf (Worksheets("File").Range("x2").Value Like "*;ProductSubType:'SWLEG'*") Then
    Worksheets("File").Range("D2:D17783").Value = "XSW" 
End If 
like image 143
Scott Craner Avatar answered Mar 16 '23 01:03

Scott Craner