2013-05-10 212 views
0

我真的很擅長並且遇到一些問題。我試圖創建一個出勤表,出於安全原因而定期更新一整天。在所有可能的名字上,我都有一張專欄供不同公司,名稱,營地,房間和現場使用。我已經寫了我的代碼,以便如果一個人在現場而不是1在現場專欄中,並且如果他們不在現場,則爲0。當出現1時,我希望將他們的姓名和所有其他信息傳輸到出席表中,以便唯一顯示的名稱是現場出現的名稱。如果他們在現場,我希望空間留空。 我有兩個問題,我的代碼:我在運行vba時出現運行時錯誤1004

Sub onsite() 

    x = 3 'start at row 3 


    'start the loop 
    Do While Cells(x, 6) <> "" 

     'look for data with '1' 
     If Cells(x, 6) = "1" Then 

     'copy the row if it contains '1' 
     Worksheets("Sheet1").Rows(x).Copy 

     'go to main ERP. activate it 
     Worksheets("Sheet2").Activate 

     **erow = Sheet2.Cells(Rows.Count, 6).End(x1Up).Offset(1, 0).Row** 
     'paste data 

     '**ERROR OCCURS HERE** 
     ActiveSheet.Paste Destination:=Worksheets("Sheet2").Rows(erow) 

     End If 

     'go to all names and activate 
     Worksheets("AllNames").Activate 

     'loop through the other rows 
     x = x + 1 

    Loop 

End Sub 

的第一個問題是,在我到達粗線我得到一個錯誤「1004」消息和代碼停止工作

的另一個問題是,我不知道如何將'erow ='換成代碼,當一個人在他們的網站列上有0時跳過代碼

請幫忙!

回答

0

This MS Knowledgebase article描述了錯誤的原因和解決方法。

基本上這是編程複製和粘貼整行時出現的問題。您必須僅選擇行中實際使用的單元格。

1
Sub onsite() 
    Dim x as long, erow as Long 
    Dim shtSrc as Worksheet, shtDest as worksheet 

    Set shtSrc = Worksheets("Sheet1") 
    Set shtDest = Worksheets("Sheet2") 
    erow = shtDest.Cells(rows.count, 6).End(x1Up).Row+1 

    x = 3  

    Do While shtSrc.Cells(x, 6) <> "" 

     If shtSrc.Cells(x, 6) = "1" Then 

      shtSrc.Rows(x).Copy shtDest.cells(erow,1) 
      erow = erow+1 

     End If 
     x = x + 1 

    Loop 

End Sub