Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formula with quotes into cell using VBA throws error

I have a code below which concatenate Values in A and B columns. When I want to add an empty space between A and B which is an Excel formula =CONCATENATE(A4," ",B4) or =A4 & " " & B4 the macro would flag it as an error when using VBA:

With ws3.Range("E4:E" & LastRow3)
    .Formula = "=A4&B4"
End With
like image 722
Batyr Nazarov Avatar asked Mar 09 '26 13:03

Batyr Nazarov


2 Answers

Quotes within quotes need to be doubled

.Formula = "=A4 & "" "" & B4"

and this will result in =A4 & " " & B4 as a formula.

like image 86
Pᴇʜ Avatar answered Mar 12 '26 12:03

Pᴇʜ


This appears to work:

Sub jhgfd()
    Set ws3 = ActiveSheet
    LastRow3 = 10

    With ws3.Range("E4:E" & LastRow3)
        .Formula = "=A4 & CHAR(32) & B4"
    End With
End Sub
like image 44
Gary's Student Avatar answered Mar 12 '26 11:03

Gary's Student



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!