2012-09-20 136 views
0

我試圖獲得一個VBA編碼,它將搜索格式化爲日期的單元格以及所有日期格式,再次搜索它上面的特定文本並將其複製到其中的行日期單元格是。Excel VBA - 複製並粘貼

爲了說明,下面是我試圖解決的數據示例。對於格式爲日期的列C中的任何單元格,我希望代碼找到客戶編號(單元格C2)和客戶名稱(D2),並將其複製到適用行的列A和B上。問題出現了,因爲我的數據中的許多客戶擁有多個憑證,因此我無法將其嚴格編碼爲只有具有日期的單元格上方的兩行。我希望我有道理!

Customer account Name 
1001010 Internal : Sales Admin (9900) 
Date   Voucher 
12/7/2011 CINV00000980 
1/26/2012 CINV00001011 
2/9/2012  CINV00001050 
3/6/2012  CINV00002003 
3/13/2012 CINV00002067 

我試圖使用嵌套DO循環現在(見下文),顯然它不工作。代碼正在運行,但沒有複製/粘貼正在發生。

Sub ARAging() 
    Dim row As Integer 
    row = 2 
    finalrow = ActiveSheet.UsedRange.Rows.Count 
Do 

    If Range(Cells(row, 3), Cells(row, 3)).NumberFormat = "dd/mm/yyyy" Then 
     Do 
     If Range(Cells(row, 3), Cells(row, 3)).Value = "Customer account" Then 
     Range(Cells(row - 1, 3), Cells(row - 1, 4)).Select 
      With Selection.Copy 
      End With 
     End If 
     row = row - 1 
     Loop Until Range(Cells(row, 3), Cells(row, 3)).Value = "Customer account" 

     Range(Cells(row, 1), Cells(row, 2)).Select 
      With Selection.Paste 
      End With 
    End If 

row = row + 1 

Loop Until row = finalrow 
End Sub 

任何幫助,將不勝感激!

+0

哇,我的示例數據沒有複製好。列A和B是空白的。 C列有文本「客戶賬戶」,客戶賬號,「日期」,然後是五個日期(C1:C8)。我希望對於五個日期中的每一個日期,代碼都會搜索上面的文本「客戶賬戶」,併爲五個日期中的每一個返回適用的客戶賬戶號碼。希望這個澄清有助於。 – user1686737

+0

也許添加一個屏幕截圖? – JMK

+1

只需注意:您的示例數據中的日期是m/d/y格式,但是您的代碼會測試d/m/y格式。在我的答案中,我不測試格式,但不是因爲這個原因。 –

回答

2

我認爲這段代碼是做你想做的。我希望我已經包含足夠的評論來解釋我的代碼。如果有必要,請回答問題。

' Option Explicit means every variable must be explicitly declared. Without 
' it a misspelt variable name becomes an implicit declaration. This can be 
' a very difficult error to find. 
Option Explicit 

Sub ARAging() 

    ' On a 32-bit system, the native size is what Excel calls Long. So integer 
    ' variables are slower than long variables. 

    ' My style is to start a variable name with its type and then add classifiers 
    ' to identify its purpose. This means that when I return to this macro in 
    ' six or twelve months, I know what the variables are. If you do not like my 
    ' style, develop your own or develop one with colleagues. Consistent names 
    ' are a godsend during maintenence. 
    Dim acctNumCust As String 
    Dim rowCrnt As Long 
    Dim rowFinal As Long 

    With Worksheets("Sheet1") 
    ' I avoid ActiveSheet unless I want the user to be able to run the macro 
    ' against a worksheet of their choice. 

    acctNumCust = "" 
    rowFinal = .UsedRange.Rows.Count 
    For rowCrnt = 2 To rowFinal 

     ' In .Cells(R,C), C can be a number or a column code. 
     ' If I am accessing a single cell, I do not need to make it into 
     ' a range since .Cells(R,C) is a range. 

     If .Cells(rowCrnt, "C").Value = "Customer account" Then 

     ' Since I know column C is "Customer account", I do not need to 
     ' save it. Save the value of column 4 in a variable not the 
     ' scratchpad. 
     acctNumCust = .Cells(rowCrnt, "D").Value 

     ' I do not like testing for the formatting. What if the user changes 
     ' it to dd-mmm-yy? 

     ElseIf IsDate(.Cells(rowCrnt, "C").Value) Then 
     .Cells(rowCrnt, "A").Value = "Customer account" 
     .Cells(rowCrnt, "B").Value = acctNumCust 

     Else 

     Call MsgBox("Cell C" & rowCrnt & " is neither """ & _ 
            "Customer account"" nor a date", vbOKOnly) 

     End If 

    Next rowCrnt 

    End With ' Worksheets("Sheet1") 

End Sub