2016-07-20 147 views
0

我的宏有問題,請執行Microsoft Excel。我想創建一個宏來檢查在一行中是否有單詞表示它與用戶在單元格中輸入的單詞相同,而不是檢查整行以比較其他信息。在前。用戶輸入亞當斯密12.05.2016和宏必須在姓名列中找到名稱亞當,並檢查此亞當是否具有姓史密斯和日期12.05.2016。如果是打印整行。 我已經創建了整個宏,但是我得到了error1004應用程序定義或對象定義的錯誤。Visual Basic宏錯誤

Sub Test() 
Dim r As Integer 
Dim c1 As Integer 
Dim c2 As Integer 
Dim i As Integer 
r = 0 
c1 = 0 
c2 = 0 
i = 0 
' Iterates through name column 
Do Until Cells(r, c1).Value = "" 
    ' If program finds the name in name row equal to name in (6, G) returns true 
     If Cells(r, c1).Value = Cells(6, G).Value Then 
      ' Iterates through surname row 
      Do Until Cells(r, c2).Value = "" 
       ' If surname is the same and date is on the right of the surname Return tur 
       If Cells(r, c2).Value = Cells(6, H).Value And Cells(r, c2 + 1).Value = Cells(6, i) Then 
        Do Until i <= 0 
        ' Output whole data from the row 
        Cells(10, H + i).Value = Cells(r, c1 + i).Value 
        i = i + 1 
        Loop 
       End If 
      c2 = c2 + 1 
      Loop 
     End If 
r = r + 1 
Loop 
End Sub 
+0

由於行和列零不存在,您的變量不能以值0開頭。從1開始。另外,你不能寫'Cells(6,G)'列被稱爲A爲1,B爲2等。 –

+0

你可以用公式來做到這一點,是否需要VBA? –

+0

@Nathan_Sav我是總新手,如果我們談論Excel。 :( – Trefl

回答