简体   繁体   English

Excel VBA-如何在空白单元格之后复制和粘贴数据组?

[英]Excel VBA - How to copy and paste groups of data following blank cells?

I have data in B6 and M6 that correspond directly with each other. 我在B6和M6中有彼此直接对应的数据。 The data goes from B6:B12 and then there are two blank cells in B13:B14. 数据来自B6:B12,然后B13:B14中有两个空白单元格。 The data then goes on from B15:B23 and then there are two blank cells and this pattern repeats down the page...(the same goes for column M). 然后,数据从B15:B23继续,然后有两个空白单元格,并且该模式在页面下重复...(M列也是如此)。

I researched finding blank cells and was able to use this code to grab that first set of data from B6:B12 and M6:M12 and paste it on to a new worksheet in the location I wanted. 我研究发现空白单元格,并能够使用此代码从B6:B12和M6:M12抓取第一组数据并将其粘贴到所需位置的新工作表中。 Here is the code: 这是代码:

Sub CopyandPaste()

NextFree = Range("B6:B" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("B" & NextFree).Select

NextFree2 = Range("M6:M" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("M" & NextFree2).Select

Sheets("Sheet 1").Range("B6:B" & NextFree).Copy Destination:=Sheets("Sheet 2").Range("B13")

Sheets("Sheet 1").Range("M6:M" & NextFree2).Copy Destination:=Sheets("Sheet 2").Range("J13")

End Sub

This works to grab the first group before the blank the two blank cells but I cannot find a way to grab the second, third, and so on groups that follow two blank cells. 这可以抢先在两个空白单元格之前将第一个组捕获,但是我无法找到一种方法来抢夺在两个空白单元格之后的第二个,第三个组等等。 Any help would be appreciated. 任何帮助,将不胜感激。

If you know the pattern of blocks (block - 2 spaces - block) you can do a nested loop. 如果您知道块的模式(块-2个空格-块),则可以进行嵌套循环。

Sub grabBlocks()

Dim cFirst As Range, cLast As Range
Dim sh As Worksheet

Set sh = ThisWorkbook.Sheets(1)
Set cFirst = sh.Range("B6") 'First Cell of each new block
Set cLast = cFirst 'This becomes last cell of the block

Do While Not cFirst = ""

    'Get Last Cell of Block
    Do While Not cLast.Offset(1, 0) = ""
        Set cLast = cLast.Offset(1, 0)
    Loop

    'Do copy with this address
    Debug.Print Range(cFirst.Address & ":" & cLast.Address).Address
    '... copy code goes here...

    'Go to next block
    Set cFirst = cLast.Offset(3, 0) 'First cell of new block is 2 + 1 cells below the last
    Set cLast = cFirst
Loop

End Sub

This code will terminate when the next block is more than 2 cells away, expecting no more blocks to come. 当下一个区块距离超过2个像元时,此代码将终止,并期望不再有其他区块出现。

Beware that these loops can become nasty if your termination condition can not be satisfied (eg your cells contain 'invisible' data like spaces) 请注意,如果无法满足终止条件(例如,单元格包含“不可见”数据(例如空格)),这些循环可能会变得很讨厌

Sub copynPaste()
Dim i As Integer, j As Integer
j = 1
   'loops from 1 to the last filled cell in column 2 or "B"
    For i = 1 To Sheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
       'checks if the cell has anything in it
        If IsEmpty(Sheets("Sheet1").Range("B" & i)) = False Then
            'this is where the copying and pasting happens (well basically)
            Sheets("Sheet2").Range("B" & j).Value = Sheets("Sheet1").Range("B" & i).Value
            Sheets("Sheet2").Range("M" & j).Value = Sheets("Sheet1").Range("M" & i).Value
            j = j + 1
        End If
    Next i
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 VBA 复制 excel 中的一列单元格,直到出现空白并将其粘贴到新工作表中? - How can I copy a column of cells in excel, with VBA, until there is a blank and paste it into a new sheet? 如何循环复制列中空白单元格之后的数据并将其粘贴到最后一个空列? - How to loop to copy data following blank cells in a column and paste it to last empty column? 如何复制带有空白单元格的列并粘贴到新工作表 VBA 中? - How to copy columns with blank cells and paste in new worksheet VBA? Excel VBA如何将单元格的一部分复制并粘贴到新制作的工作表中 - Excel VBA How to copy and paste a section of cells into a newly made sheet 如何使用vba在excel中仅复制和粘贴过滤的单元格 - How to copy and paste only filtered cells in excel using vba 如何通过Excel中的VBA复制/粘贴已过滤/可见单元格中的公式 - How to copy/paste formula in filtered/visible cells via VBA in Excel 如何复制单元格并将其粘贴到 Excel VBA 宏中的多个单元格 - How to copy a cell & paste it to multiple cells in Excel VBA Macro 在 VBA excel 中复制和粘贴多个单元格和行 - Copy & Paste multiple cells & rows in VBA excel excel vba 复制并粘贴以修复“#REF”单元格 - excel vba copy and paste to fix “#REF” cells Excel VBA宏复制单元格并粘贴到另一个 - Excel vba macro to copy cells and paste in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM