user1679941
user1679941

Reputation:

Compile error when concatenating a string using Excel VBA

I am trying to run this code:

Private Sub CommandButton1_Click()

Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer, cellValue1 As String

myFile = "c:\h\sales.TXT"
Set rng = Selection

Open myFile For Output As #1

For i = 1 To rng.Rows.Count
    cellValue1 = rng.Cells(i, 1).Value
    Write #1 "INSERT INTO JLPT COL1,COL2 VALUES ('" & cellValue1 & "')"
Next i

Close #1

End Sub

But it gives me a compile error with no more details on the line with Write.

Can someone give me some advice on this?

Upvotes: 0

Views: 169

Answers (1)

Shai Rado
Shai Rado

Reputation: 33682

you are missing a comma, replace your line :

Write #1 "INSERT INTO JLPT COL1,COL2 VALUES ('" & cellValue1 & "')"

With:

Write #1, "INSERT INTO JLPT COL1,COL2 VALUES ('" & cellValue1 & "')"

Upvotes: 1

Related Questions