[英]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.