写一个VBA代码实现将A列字符串复制粘贴到表2的A列,且,字符串对应行的B列单元格的值数字是多少,就在A列依次复制多少个相应的字符串
时间: 2024-03-02 21:52:44 浏览: 62
好的,下面是VBA代码实现将A列字符串复制粘贴到表2的A列,且字符串对应行的B列单元格的值数字是多少,就在A列依次复制多少个相应的字符串:
```VBA
Sub CopyStrings()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceRange As Range
Dim targetRange As Range
Dim sourceCell As Range
Dim targetCell As Range
Dim i As Long
Set sourceSheet = ThisWorkbook.Sheets("Sheet1") '将"Sheet1"替换为源表格名称
Set targetSheet = ThisWorkbook.Sheets("Sheet2") '将"Sheet2"替换为目标表格名称
Set sourceRange = sourceSheet.Range("A1:A" & sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row)
Set targetRange = targetSheet.Range("A1:A" & sourceRange.Cells(sourceRange.Rows.Count, "B").End(xlUp).Row)
For Each sourceCell In sourceRange
For i = 1 To sourceCell.Offset(0, 1).Value
Set targetCell = targetRange.Cells(targetRange.Cells.Count).Offset(1, 0)
targetCell.Value = sourceCell.Value
Next i
Next sourceCell
End Sub
```
注意替换代码中的表格名称和单元格范围。此代码将会把“Sheet1”中的“A”列字符串复制到“Sheet2”中的“A”列,并且根据对应行的“B”列单元格的数字值,在“A”列复制相应的字符串。
阅读全文