VBA:从另一列获取数据列表并使用循环将它们传输到另一列

假设我想得到一个数据列表,只想在每个列表上得到一个特定的string,我想将它们转移到不同的列。

例如,我想要在列A中的每个数据都得到一个特定的string,我想将这些特定的string转移到列E的另一列。到目前为止,我得到的是这个,但它不工作。 我相当新的VBA,我希望你们可以帮助我。 谢谢。

PS:我的意思是某些string是在该列的每个string上使用中间函数。

Sub test() With Sheets("Sheet1") LastRowE = .Range("E" & .Rows.Count).End(xlUp).Row End With For Each MyCell In Range("E2:E" & LastRowE) MyCell.Value = Mid(Range("A2:A6"), 7, 2) Next MyCell End Sub 

请继续尝试下面的代码。 您可以根据自己的情况调整代码。

数据之前

  ABCDE 1 hello 19 2 hello 20 3 hello 40 4 hello rr 

 Sub test() ' find the last row assuming data starts from A1 Dim LastRow As Integer LastRow = Range("A1").End(xlDown).Row ' loop through E1..E<n> ' take value of corresponding row of column A and ' do MID on it. Put the result in A's row Dim MyCell As Range For Each MyCell In Range("E1:E" & LastRow) MyCell.Value = Mid(Range("A" & MyCell.Row), 7, 2) Next End Sub 

运行代码后

  ABCDE 1 hello 19 19 2 hello 20 20 3 hello 40 40 4 hello rr rr 

希望这个信息有帮助。