How to insert if function into cell using vba

JasonT 1 Reputation point
2021-11-17T12:08:26.74+00:00

Hi, I wrote the following to auto fill cell in spreadsheet. But I stucked on this formula =IF(U2=U1,"",A2) and =LEFT(A2,8)&"-"&U2 in the following script.
Please advice.

Private Sub CommandButton1_Click()

Dim lr As Long

lr = Worksheets("New").Range("A" & Rows.Count).End(xlUp).Row

Worksheets("New").Range("T2").Formula = "=LEFT(B2,2)" (work)

Worksheets("New").Range("T2").AutoFill Destination:=Worksheets("New").Range("T2:T" & lr)

Worksheets("New").Range("U2").Formula = "=(T2&0&0)" (work)

Worksheets("New").Range("U2").AutoFill Destination:=Worksheets("New").Range("U2:U" & lr)

Worksheets("New").Range("V2").Formula = "=IF(U2=U1,"",A2)" (not work)

Worksheets("New").Range("V2").AutoFill Destination:=Worksheets("New").Range("V2:V" & lr)

Worksheets("New").Range("V2").Formula = "=LEFT(A2,8)&"-"&U2" (not work)

Worksheets("New").Range("V2").AutoFill Destination:=Worksheets("New").Range("V2:V" & lr)

End Sub

Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2021-11-17T19:13:35.57+00:00

    Check if this works:

    Worksheets("New").Range("V2").Formula = "=IF(U2=U1,"""",A2)"
    Worksheets("New").Range(...).Formula = "=LEFT(A2,8)&""-""&U2"
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.