简体   繁体   中英

VBA code to separate cell string and copy it to next rows in a different sheet

I am looking for a VBA code that will:

copy Column A data in sheet 1 to Column A in sheet 2, and for each specific row in sheet 1: if row i column C has text separated by commas then copy each value separate by a comma and create new rows in sheet 2 to have all of those values in consecutive rows while copying the value of column A (only) from sheet one with it as well and leave the rest of the columns blank while this occurs.

here is an example of sheet 1

表 1 的示例

here is how it should be translated in sheet 2

表 2 示例

I am kinda new to VBA but I have been researching and trying different codes to represent what I am looking for, but nothing I could have find or created worked for me so far.

Thanks for the help !

Here's a small sample of setting an array based on a string (which you could get from cell contents) then looping through the array and placing the values of the array in to a row for each. Hopefully you can build on this to fit your needs:

Sub loopingz()

Dim i As Long
Dim currentRow As Long
Dim myArray() As String
Dim myString As String

myString = "25,26,39"
myArray = Split(myString, ",")
currentRow = 1


For i = LBound(myArray) To UBound(myArray)
    Range("A" & currentRow).Value2 = myArray(i)
    currentRow = currentRow + 1
Next i

End Sub

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM