2013-03-09 41 views
1

如何將數據從列A(Sheet1)複製到列B(Sheet2)而不修改柱狀圖B上的先前數據?先謝謝你。從列A(Sheet1)複製到列B(Sheet2),低於之前柱狀數據B

+1

你剛纔問過這個嗎?問題是「vba excel 2010將數據複製到另一個空列表」?這個問題有什麼不同? – cardmagik 2013-03-09 20:50:06

+0

昨天我問如何把數據放在順序空列中。現在我想知道如何將數據複製到同一列而無需修改colunm B的先例數據。 – 2013-03-09 20:57:03

+0

如果您制定了該方案,那將是最好的。例如,Sheet1中的單元格A1,A2,A3中的數據 - 只有在B1,B2和B3爲空時纔會複製到Sheet 2 B1,B2,B3。那麼如果B1,B2,B3中有值,會發生什麼?不復制?或放入B4,B5,B6(或其他空單元格)。 – cardmagik 2013-03-09 21:02:33

回答

1

下面是一個通用的答案,它不會替換單元格,但會查找下一個空單元格。我試着把意見放在那裏,這有助於理解它。

Sub CopySheet1toSheet2() 

    Dim CurrRow As Integer 
    Dim CurrCol As Integer 
    Dim TargetRow As Integer 
    Dim TargetCol As Integer 

    Dim Sheet1 As String 
    Dim Sheet2 As String 

    Sheet1 = "Sheet1" ' name to your source sheet name 
    Sheet2 = "Sheet2" ' change to your target sheet name 
    CurrRow = 1 
    CurrCol = 1 ' This is column A 

    TargetRow = 1 
    TargetCol = 2 ' This is column B 

    ' Cycle through all rows in Col A until empty field reached 
    While Sheets(Sheet1).Cells(CurrRow, CurrCol) <> "" 

     ' See if there's a value in column B in the same row - if so, go to the next row 
     While Sheets(Sheet2).Cells(TargetRow, TargetCol) <> "" 
      TargetRow = TargetRow + 1 
     Wend 

     ' Copy the value from the source to the target 
     Sheets(Sheet2).Cells(TargetRow, TargetCol) = Sheets(Sheet1).Cells(CurrRow, CurrCol) 

     ' Move to the next source and target rows 
     CurrRow = CurrRow + 1 
     TargetRow = TargetRow + 1 

    Wend 

End Sub 
+0

非常感謝。幫助我很多。 – 2013-03-09 21:33:33

相關問題