2015-09-30 60 views
0

我正在處理一個宏,這個宏可以幫助我將Excel中的數據從一個工作表複製到另一個工作表中。我試着下面的代碼,但我得到了一個需要對象錯誤我該如何解決一個對象所需的錯誤

任何幫助表示讚賞

Sub copier() 
    Dim ws1 As Worksheet, ws2 As Worksheet, src As Range, dest As Range, i As Integer 

    Set ws1 = Worksheets("Workload - Charge de travail") 
    Set ws2 = Worksheets("Sheet1") 

    For i = 2 To ws1.Range("A1").SpecialCells(xlLastCell).Row   

     Set src = ws1.Range("A" & i & ":AL" & i + 50) 
     Set dest = ws2.Range("A" & i & ":AL" & i + 50) 

     If Source.Cells(i, 31).Value = "Completed - Appointment made/Complété - Nomination faite" Then 

      '(i,31) this is for my drop down list's condition 


      src.Copy Destination:=dest 
      dest.Value = dest.Value 
     End If 
    Next i 
End Sub 
+0

什麼是「源」?它應該是'src'嗎? –

+0

發生什麼錯誤? – transistor1

回答

0

您使用Source代替src

If Source.Cells(i, 31).Value = "Completed - Appointment made/Complété... 
+0

謝謝我嘗試過它的作品,但它不尊重它複製所有行的條件,我只需要那些有(「完成 - 任命製作/Complété - 提名Faite」)的人 –

1

薩呂,veuyer驗證闕「源」,應該不是表格「src」。

我也猜你想只有一行一次複製,

  • Range("A" & i & ":AL" & i) which is "A2:AL2" rather than
  • Range("A" & i & ":AL" & i + 50) is "A2:AL52"

同時檢查細胞(Y,31)意成爲AL之前的「AE」(第38欄)。

0

有幾個問題,Source不應該是src它應該是ws1。在行src已經引用後,src.cells(i ...)將轉到i行。如果我是50,那麼它將得到第100行的結果。

另外我相信你只需要一次複製一行而不是50行。

Sub copier() 
    Dim ws1 As Worksheet, ws2 As Worksheet, src As Range, dest As Range, i As Integer 

    Set ws1 = Worksheets("Workload - Charge de travail") 
    Set ws2 = Worksheets("Sheet1") 

    For i = 2 To ws1.Range("A1").SpecialCells(xlLastCell).Row 

     Set src = ws1.Range("A" & i & ":AL" & i) 
     Set dest = ws2.Range("A" & i & ":AL" & i) 

     If ws1.Cells(i, 31).value = "Completed - Appointment made/Complété - Nomination faite" Then 

      '(i,31) this is for my drop down list's condition 


      src.Copy Destination:=dest 
      'dest.value = dest.value 
     End If 
    Next i 
End Sub