2016-06-12 82 views
0

我想要做的是循環通過列A找到空白單元格並複製上面一行中該行的列B和C中的兩個數字在行中的最後一個單元格之後。然後在A列空白刪除行運行時錯誤1004:在VBA循環中的對象範圍失敗的方法

這裏是指在執行代碼之前的空白單元格的圖像: Here is an image of the blank cell before the numbers were copied

和第二圖像顯示我的代碼的結果: And the second image shows the result of my code

Sub PivotTableLayout() 

Dim rng As Range 
'Dim row As Range 
Dim cell As Range 

Set rng = ThisWorkbook.Sheets("CopyPivot").Columns("A") 
For Each cell In rng.Rows 
    If IsEmpty(cell) Then 
     ' Do while is blank 
     Do While IsEmpty(cell) 
      'offset one right and copy two cells 
      ActiveCell.Offset(0, 1).Resize(1, 2).Copy 
      ' offset counter and 1 up 
      ActiveCell.Offset(-1, 0).End(xlToRight).Offset(0, 1).Select 
      ActiveSheet.Paste 
      ActiveCell.Offset(1, 0).Select 
      ActiveCell.EntireRow.Delete 
     Loop 
    End If 
Next cell 

End Sub 

不過,我一直在這條線接收運行時錯誤1004,但它的作品,如果我從代碼中刪除它......我不能找出原因:

ActiveCell.Offset(-1, 0).End(xlToRight).Offset(0, 1).Select 
+0

用'cell'替換對'ActiveCell'的所有引用。 –

+0

@ Gary'sStudent謝謝!這工作,但是,現在我的代碼沒有做我想做的事情。如果一個接一個地存在多個空格,則它只執行第一個空白的複製粘貼,然後跳過其餘的空白。 Do while while循環不適合在這裏使用嗎? – erikasoladams

+0

避免使用activeCell或在正確的地方開始。 單元格替換activeCell似乎是正確的,但有問題...您可以看到,刪除後循環中的下列單元格將單元格置於奇怪的狀態,並使您的算法在下面的空單元格中崩潰。 最好的方法是直接使用工作簿中的單元格並避免 設置rng = ... 當刪除單元格範圍內時。 @Mrig作出了溶液 – ruirodrigues1971

回答

4

當您刪除(或列)時,始終更適合從底部循環到頂部(或從右到左)。以下代碼會給你想要的結果。

Sub PivotTableLayout() 
    Dim i As Long 
    Dim lastRow As Long, lastColumn As Long 
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row 
    For i = lastRow To 1 Step -1 
     If Cells(i, 1).Value = "" Then 
      lastColumn = Cells(i - 1, Columns.Count).End(xlToLeft).Column 
      Range("B" & i, Range("B" & i).End(xlToRight)).Copy Cells(i - 1, lastColumn + 1) 
      Rows(i).Delete 
     End If 
    Next 
End Sub 

編輯: ___________________________________________________________________________

Sub PivotTableLayout2() 
    Application.ScreenUpdating = False 
    Application.Calculation = xlCalculationManual 
    Dim i As Long 
    Dim lastRow As Long, lastColumn As Long 
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row 
    For i = lastRow To 1 Step -1 
     If Cells(i, 1).Value = "" Then 
      lastColumn = Cells(i - 1, Columns.Count).End(xlToLeft).Column 
      Range(Cells(i, 2), Cells(i, Cells(i, Columns.Count).End(xlToLeft).Column)).Copy Cells(i - 1, lastColumn + 1) 
      Rows(i).Delete 
     End If 
    Next 
    Application.Calculation = xlCalculationAutomatic 
    Application.ScreenUpdating = False 
End Sub 
+0

@Mrig嗨,這將產生對一個錯誤: lastColumn =細胞(I - 1,Columns.Count).END(xlToLeft).COLUMN 運行時間錯誤1004:方法 「_Default」 或對象範圍失敗。 任何想法爲什麼? – erikasoladams

+0

我已經測試和工作,也許把代碼 表(「CopyPivot」)的beggining選擇 只是爲了防止活動工作表是正確 – ruirodrigues1971

+1

@erikasoladams - 有沒有數據的任何排在所有IE完全空白的行? – Mrig

1

你也可以使用函數specialcells:

Sub remove() 
    Dim rFirstColumn As Range 
     Set rFirstColumn = Worksheets("Sheet2").Columns("A") 
    Dim rBlanks As Range 
     Set rBlanks = rFirstColumn.SpecialCells(xlCellTypeBlanks) 
    Dim iBlanks As Integer 
     iBlanks = rBlanks.Count 
    Dim iCount As Integer 

    For iCount = 1 To iBlanks Step -1 
     Set rPrevious = rBlanks.Cells(iCount, 1).Offset(-1, Columns.Count - 1).End(xlToLeft) 
     Range("B" & rBlanks.Cells(iCount, 1).Row, Range("B" & rBlanks.Cells(iCount, 1).Row).End(xlToRight)).Copy rPrevious.Offset(0, 1) 
     Rows(rBlanks.Cells(iCount, 1).Row).Delete 
    Next iCount 
End Sub 
0

使用@Mrig解決方案的兩行,解決一些細節。該解決方案只能看到空單元格。

'more efficient ...just work with empty cells 
    Sub PivotTableLayout3() 
     Dim i As Long 
     Dim aux As Object 
     Dim lastRow As Long, lastColumn As Long 
     Sheets("CopyPivot").Select 
     lastRow = Cells(Rows.Count, "A").End(xlUp).Row 
     i = Cells(1, 1).End(xlDown).Row + 1 'first empty cell 
     While (i < lastRow) 
      While IsEmpty(Cells(i, 1)) And i < lastRow 'working with empty cells 
       Set aux = Range("B" & i).End(xlToRight) 
       If Not aux.Column = 16384 Then 'not empty row 
        lastColumn = Cells(i - 1, Columns.Count).End(xlToLeft).Column 
        Range("B" & i, Range("B" & i).End(xlToRight)).Copy Cells(i - 1, lastColumn + 1) 
       End If 
       Rows(i).Delete 
       lastRow = lastRow - 1 'my last row has changed 
      Wend 
      i = Cells(i, 1).End(xlDown).Row + 1 'next empty cell 
     Wend 
    End Sub 
相關問題