简体   繁体   English

VBA将单元格范围匹配到工作表名称,并将其复制/粘贴到相应的工作表

[英]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 . 我会为b3b4b5直到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.

相关问题 Excel VBA如何将WorkSheet名称与单元格值匹配,然后复制并粘贴一系列单元格 - Excel VBA How to match WorkSheet name with Cell value, then Copy and Paste a Range of Cells Excel VBA复制/粘贴范围到另一个工作表中的下一个可用单元格 - Excel VBA Copy/Paste Range to the next available cell in another worksheet VBA 复制和粘贴具有匹配工作表名称的数据 - VBA Copy and Paste Data with Matching Worksheet Name VBA复制范围到新工作表中的单元格 - vba copy range to cell in new worksheet VBA复制并粘贴到新工作表 - VBA Copy and Paste to new Worksheet 复制一个静态的命名范围并将其粘贴到另一个工作表中的变量单元格地址 - Copy a static named range and paste it to a variable cell adress in another worksheet 如何通过匹配单元格值将范围复制并粘贴到另一个工作表 - How to copy and paste a range to another worksheet by matching a cell value 来自多个工作表的VBA Excel复制/粘贴特定范围 - VBA Excel Copy/Paste Specific Range from multiple worksheet 复制单元格范围并根据日期粘贴到另一个工作表中? - Copy cell range and paste in another worksheet based on the date? 使用vba在工作表数组中复制并粘贴特定单元格(使用.find) - copy and paste specific cell(using .find) in worksheet array using vba
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM