My code concatenates the values of the columns of the first row of table "Data" and writes the result in table "Insert" cell A1.
The error is that the concatenation result of next row is added at the end of the first result in table "Insert" A1 and so on. The results should be written in table "Insert" in column A row by row too.
What is wrong with my code?
Sub InsertStatementRow()
Dim x As String
Dim rng As Range
Dim cel As Range
Dim ColMax As Integer
Dim i As Long
Sheets("Data").Select
ColMax = Cells(1, Columns.Count).End(xlToLeft).Column
row = 1
Do While Cells(row, "A").Value <> ""
With Worksheets("Data")
i = 1
Set rng = Range(.Cells(i, 1), .Cells(i, ColMax))
End With
For Each cel In rng
x = x & cel.Value
Next
Sheets("Insert").Cells(i, 1).Value = x
row = row + 1
Loop
End Sub
Thanks for your help!
you set
i=1
before
Set rng = Range(.Cells(i, 1), .Cells(i, ColMax))
but you seem to bee working with variable row
. So delete/comment the line i=1
and change Set rng
Set rng = Range(.Cells(row, 1), .Cells(row, ColMax))
hope it solves your problem.
i is being set as "1" each time the Do While loop begins, so the rng variable will always refer to row 1 of the Data worksheet. It's also copying to row 1 on the Insert worksheet. Change the i to row and it should work:
Do While Cells(row, "A").Value <> ""
With Worksheets("Data")
Set rng = Range(.Cells(row, 1), .Cells(row, ColMax))
End With
x = ""
For Each cel In rng
x = x & cel.Value
Next
Sheets("Insert").Cells(row, 1).Value = x
row = row + 1
Loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.