[英]VBA Match Cell Range to Worksheet Name and copy/paste to respective worksheet
I'm trying to write a macro that matches a cell in the with the sheet name and then pulls data from that sheet and then copies it in the next column with the initial sheet. 我正在尝试编写一个与工作表名称中的单元格匹配的宏,然后从该工作表中提取数据,然后将其复制到具有初始工作表的下一列中。 Here's what I have so far:
这是我到目前为止的内容:
Sub example()
Dim wkSht As Worksheet
For Each wkSht In Sheets
For Each Cell In Sheets("Reporting").Range("B2:B200")
If Cell.Value = wkSht.Name Then
On Error Resume Next
wkSht.Range("D15").Copy Destination:=Sheets("Reporting").Range("c2:c36")
End If
Next Cell
Next wkSht
End Sub
I want to do that for the whole column but I don't know the code for copying it in the next column over. 我想对整个专栏都这样做,但是我不知道在下一专栏中复制它的代码。 Basically, it would match the
Cell (B2)
with the sheet tab name and then pull from the same cell in that respective tab and then copy to the next column (C2)
. 基本上,它将使
Cell (B2)
与工作表标签名称匹配,然后从相应标签中的同一单元格中提取,然后复制到下一column (C2)
。 And I would do that for b3
, b4
, b5
, till b200
. 我会为
b3
, b4
, b5
直到b200
。 Thanks! 谢谢!
I think I have understood your question. 我想我已经理解了你的问题。 You would need a helper variable to increment as you copy data into a new column.
在将数据复制到新列中时,需要一个辅助变量来增加。
Code, 码,
Sub example()
Dim wkSht As Worksheet
Dim i As Long
i = 3
For Each wkSht In Sheets
For Each Cell In Sheets("Reporting").Range("B2:B200")
If Cell = wkSht.Name Then
On Error Resume Next
Sheets("Reporting").Cells(2, i).Resize(35).Value = wkSht.Range("D15").Value
i = i + 1
End If
Next Cell
Next wkSht
End Sub
The use of the long
variable i
will allow you to store the position of the next avaliable column to enter data into. 使用
long
变量i
将允许您存储下一个可用列的位置,以将数据输入其中。
You can use Offset()
to specify a cell with a position relative to the cell in ColB: 您可以使用
Offset()
来指定相对于ColB中的单元格具有位置的单元格:
Sub example()
Dim wkSht As Worksheet
For Each wkSht In Sheets
For Each Cell In Sheets("Reporting").Range("B2:B200")
If Cell.Value = wkSht.Name Then
wkSht.Range("D15").Copy Destination:=Cell.Offset(0,1)
End If
Next Cell
Next wkSht
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.