Cutting and Pasting Cells in Excel VBA
该程序的功能是找到第一行有一个空单元格,在该单元格旁边是一个需要剪切并粘贴到第26列上一行的数字。
-我正在尝试运行一个循环,该循环遍历整个工作表并在复制和粘贴后删除该行。
-如果里面已经有数据,它将跳过它并继续。
-第二种方法是我最有前途的,但我似乎无法找出如何运行 Cells(x,1)
的格式
(方法一)
1 2 3 4 5 6 7 8 9 10 11 | Dim x As Integer x = 1 this = x - 1 rowdelete = x If Cells(x, 1) ="" Then Cells(x, 1).Select.Cut Cells(x - 1, 26).PasteSpecial Rows([rowdelete]).EntireRow.Delete End If |
(方法二)
1 2 3 4 5 6 7 8 9 10 11 | Dim x As Integer x = 1 this = x - 1 rowdelete = x If Cells(x, 1) ="" Then Cells(x, 1).Select.Cut Cells(x - 1, 26).PasteSpecial Rows([rowdelete]).EntireRow.Delete End If |
我不知道我是否只是遗漏了一些东西,但您的 Method1 和 Method2 看起来和我一模一样。我认为您的问题是您试图粘贴到不存在的
试试下面的代码,它遍历 Col A 中的空白单元格,并且不使用剪贴板,这应该会让你的事情变得更快。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | Sub LoopThroughBlanks() Dim wbk As Workbook Dim ws As Worksheet Dim lRow As Long Dim Cell As Range Set wbk = ActiveWorkbook Set ws = wbk.Worksheets("Sheet1") 'Be sure to change this to your worksheet name! With ws 'Find last row of data in worksheet lRow = .Cells.Find(What:="*", _ After:=.Cells(1, 1), _ LookIn:=xlFormulas, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row 'Loop through blank cells in col a For Each Cell In .Range("A1:A" & lRow).SpecialCells(xlCellTypeBlanks) If Cell.Row = 1 Then .Range("A1").EntireRow.Insert End If 'Copy to column 26 Cell.Offset(-1, 25).Value = Cell.Offset(0, 1).Value .Rows(Cell.Row).EntireRow.Delete Next Cell End With End Sub |