2017-02-14 176 views
0

我正在使用以下代碼嘗試將列表動態地複製到另一個工作表。它運行,但不是複製,而是刪除源工作表上的所有列E,並且不會將任何內容移動到目標工作表。我不確定發生了什麼,有什麼建議嗎?如何使用for循環VBA有條件地複製和粘貼行Excel

Option Explicit 

Sub findCells() 

Dim topCell As String 
Dim leftCell As String 
Dim refCell As Range 
Dim sht As Worksheet 
Dim lastRow As Long 
Dim i As Long 

Set refCell = ActiveCell 

topCell = refCell.End(xlUp).Value 
leftCell = refCell.End(xlToLeft).Value 

MsgBox topCell 
MsgBox leftCell 

Worksheets(topCell).Activate 

Set sht = Worksheets(topCell) 

lastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row 
MsgBox lastRow 

For i = 1 To lastRow 
    Dim cellVal As String 
    Dim altCounter As Integer 
    altCounter = 31 
    Cells(i, 5).Value = cellVal 
    If leftCell = cellVal Then 
    Dim crange As Range 
    altCounter = altCounter + 1 
    Let crange = "A" & i & ":" & "G" & i 
    Range(crange).Copy Worksheets("Summary").Range("A" & altCounter & ":" & "G" & altCounter) 
    End If 
Next i 

End Sub 
+0

爲什麼在循環中創建變量?將您的Dim從您的循環移至工作簿的開頭。它也看起來像你正在分配循環中的單元格(i,5)到「」(因爲CellVal是空的)。它看起來像你想要的是cellVal = Cells(i,5).Value。我還建議調試你的crange以確保它正確設置。我沒有見過以前那樣使用過。我只看到它在Class模塊中使用。 –

回答

1

這不是完整的答案,但你有你的For i = 1 To lastRow循環內的一些錯誤(和它太長寫的評論)。

首先,完全限定您的CellsRange您的定義和設置sht對象。其次,每次進入循環時都不需要聲明變量(cellValaltCountercrange)。

三,要設置一個範圍,這個Let crange = "A" & i & ":" & "G" & i會產生一個錯誤,你需要使用Set crange = .Range("A" & i & ":" & "G" & i)

第四,在你的代碼都給人一種價值cellVal,所以沒有在那裏我覺得你在Cells(i, 5).Value = cellVal語法意味着是cellVal = .Cells(i, 5).Value

Dim cellVal As String 
Dim altCounter As Long '<-- use Long instead of Integer 
Dim crange As Range 

With sht   
    altCounter = 31 
    For i = 1 To lastRow 
     cellVal = .Cells(i, 5).Value 
     If leftCell = cellVal Then 
      altCounter = altCounter + 1 
      Set crange = .Range("A" & i & ":" & "G" & i) 
      crange.Copy Worksheets("Summary").Range("A" & altCounter & ":" & "G" & altCounter) 
     End If 
    Next i 
End With 
0

這是太長了評論爲好,但由於夏嘉曦雷達 - 這是一個完整的答案,代碼在我實施後運作。

但是,我編輯後,它停止工作。它不會引發錯誤,只是不會像以前那樣複製和粘貼行。

我不確定發生了什麼,但是當我使用MsgBox來驗證代碼的某些部分時,它看起來像是循環不起作用。但是,沒有踢出一個錯誤,我不知道爲什麼。

Option Explicit 

Sub findCells() 

Dim topCell As String 
Dim leftCell As String 
Dim refCell As Range 
Dim sht As Worksheet 
Dim lastRow As Long 
Dim i As Long 
Dim cellVal As String 
Dim altCounter As Long 
Dim crange As Range 
Dim rangeToDelete As Range 

Set rangeToDelete = Worksheets("Summary").Cells(31, "A").CurrentRegion 
    rangeToDelete.Value = "" 

Set refCell = ActiveCell 

topCell = refCell.End(xlUp).Value 
leftCell = refCell.End(xlToLeft).Value 

Set sht = Worksheets(topCell) 

lastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row 

With sht 
    .Range("A1:G1").Copy Worksheets("Summary").Range("A31:G31") 
    altCounter = 31 
    For i = 1 To lastRow 
     cellVal = Cells(i, 5).Value 
     If leftCell = cellVal Then 
      altCounter = altCounter + 1 
      Set crange = .Range("A" & i & ":" & "G" & i) 
      crange.Copy Worksheets("Summary").Range("A" & altCounter & ":" & "G" & altCounter) 
     End If 
    Next i 
End With 

End Sub 
+0

你從列A中獲得'lastRow',向下在E列上運行 - cellVal = Cells(i,5).Value',這是故意的嗎? –

+0

這應該不會影響AFAIK。 A和E都完全填充。 – VBAnking