2014-01-21 103 views
1

這是我的宏所做的。它找到一個給出大型Excel文件的字符串並轉到該列。此時,它會找到用戶輸入的字符串並將所有結果複製到其旁邊的列上。我昨天開始學習VBA,所以任何幫助表示讚賞。運行時錯誤'1004'應用程序定義或對象定義的錯誤。 VBA EXCEL

這裏那裏得到錯誤 While InStr(UCase(Worksheets("Sheet1").Cells(1, j)), UCase("request")) = 0

這裏是我完整的宏觀至今。

Sub FineMe() 

Dim i, j As Long 
Dim count, test As Integer 

userinput = InputBox("Enter String", Search, "Collect user input") 
Cells.Interior.ColorIndex = 28 

While InStr(UCase(Worksheets("Sheet1").Cells(1, j)), UCase("request")) = 0 
    j = j + 1 

    Wend 

EndRow = Worksheets("Sheet1").Cells(Rows.count, j).End(xlUp).Row 
count = 1 
    For i = 1 To EndRow 
     test = InStr(UCase(Cells(i, j)), UCase(userinput)) 
      If test > 0 Then 
      Cells(count, j + 1).Value = Cells(i, j).Value 

      count = count + 1 

      End If 
    Next i 

End Sub 

任何幫助,將不勝感激。謝謝!

回答

1

While前加j = 1,因爲聲明Dim i, j As Long後,我們有j等於0Worksheets("Sheet1").Cells(1, 0)觸發一個錯誤(我們沒有Cells(1,0)

Sub FineMe() 

    Dim i, j As Long 
    Dim count, test As Integer 

    userinput = InputBox("Enter String", Search, "Collect user input") 
    Cells.Interior.ColorIndex = 28 

    j = 1 
    While InStr(UCase(Worksheets("Sheet1").Cells(1, j)), UCase("request")) = 0 
     j = j + 1 
    Wend 

    EndRow = Worksheets("Sheet1").Cells(Rows.count, j).End(xlUp).Row 
    count = 1 
    For i = 1 To EndRow 
     test = InStr(UCase(Cells(i, j)), UCase(userinput)) 
      If test > 0 Then 
      Cells(count, j + 1).Value = Cells(i, j).Value 

      count = count + 1 

      End If 
    Next i 

End Sub 

順便說一句,在行Dim i, j As Long只有jLong,但iVariant。您應該使用Dim i As Long, j As Long。與Dim count, test As Integer同樣的事情 - 你應該聲明如下:Dim count As Integer, test As Integer

+1

非常感謝您! – user3221162

0

我認爲INSTR(1,你寫 CLIC INSTR在VB編輯器,然後按F1什麼

相關問題