2013-06-02 66 views
2

我有我的工作人員誰登錄,使用以下命令從一個工作表複製多列到另一個

Categories Washing Cleaning Office Duties Baking Cooking 

Date   Hours Hours  Hours Hours  Hours Hours 
Jan/1/13    3.00    6.00 
Jan/2/13       
Jan/6/13      3.00  
Jan/10/13      

基本上我想是有其複製日期與數目的代碼片的時間的時間片根據相關類別將其小時數調整爲另一張名爲報告的表格我需要三列作爲輸出日期時間類別。

回答

1

試試下面的代碼:

Sub sample() 


    Dim lastRow As Long 
    Dim col As Long 
    Dim a As String, b As String, c As String 

    With Sheets("sheet1") 
     lastRow = .Range("A" & Rows.Count).End(xlUp).Row 

     If lastRow < 4 Then lastRow = 4 

     For i = 4 To lastRow 
      For col = 2 To 7 
       If .Cells(i, col) <> "" Then 

        a = .Cells(i, 1) 
        b = .Cells(i, col) 
        c = .Cells(1, col) 

        With Sheets("Report") 
         .Range("A" & .Range("A" & .Rows.Count).End(xlUp).Row + 1).Value = a 
         .Range("B" & .Range("B" & .Rows.Count).End(xlUp).Row + 1).Value = b 
         .Range("C" & .Range("C" & .Rows.Count).End(xlUp).Row + 1).Value = c 
        End With 
       End If 
      Next 
     Next 

    End With 
End Sub 

enter image description here

+0

親愛的桑托斯,非常感謝您對這樣一個偉大的解決方案。真的,我找不到話來謝謝你。我還有一件事,因爲我有一個以上的工人,比如讓Sheet1代表John,Sheet2代表Michael並且Sheet3代表Tom,我希望所有工人都有詳細信息,那麼我應該如何重複其他工人的代碼,以便我可以在一張表中獲取數據。 – OMK28

+0

@ OMK28'昏暗SHT作爲工作表 對於每個SHT在ThisWorkbook.Sheets 隨着SHT '代碼 ' ' 結束隨着 Next'使用上述代碼時可以循環片材。非常感謝:) – Santosh

+0

非常感謝您的幫助,實際上我是一名初學者,對VBA知之甚少,所以如果您能夠知道在上面的代碼中何處以及如何放置這些行,那將是非常棒的。謝謝 – OMK28

0

你有沒有嘗試過這樣的:

Sheet2.Range("A1").Value = Sheet1.Range("A1").Value 

Sheet2.Range("A1").Text = Sheet1.Range("A1").Text 

也許你可以做一個循環,這樣做:

Sheet2.Range("A" & i).Value = Sheet1.Range("A" & i).Value 

是有很多的選擇可以做到這一點...甚至Range.Copy(見:http://msdn.microsoft.com/en-us/library/office/ff837760.aspx

祝你好運。

相關問題