2016-06-13 51 views
0

好,VBA_Error「1004」,而使用InStr函數早上

我想使用一個For循環來比較兩片的內容,並做一些事情,如果條件滿足。

For j = 1 To ligne_Brouillon 

    If InStr(Sheets("Brouillon").Cells(j, 7).Text, Sheets("Data").Cells(i, 18).Text) <> 0 Then 'compare two cells' content 

     For k = 8 To colonne_brouillon 

      If InStr(Sheets("Brouillon").Cells(j, k).Text, Sheets("Data").Cells(i, 20).Text) <> 0 Then 'if the first cell contents the keyword 
       Sheets("Data").Cells(i, ma_Colonne).Text = Sheets("Brouillon").Cells(j, k).Text 
      End If 

     Next 

    End If 

Next 

但我在試圖運行時遇到了錯誤「1004」。我不知道誰來解決這個問題。如果你有一些想法,請給我一些建議。 在此先感謝。


當然,我的目標是填寫一張質量控制表,其中包含測試名稱和結果。測試和結果在一些文件中單獨保留。 我的工作是在我進行新測試時填寫表格。並且對於每個測試點,標準都是相同的(例如顏色,表單等),我只需要填寫正確的單元格以獲得正確的結果。 我想打開一張新表並填寫所有結果,然後用這張新表填寫表格。我認爲這可能會更容易。

下面是完整的代碼:

Private Sub Button_Importer_Click() 

    Sheets.Add After:=Sheets(Sheets.Count) 
    Sheets(Sheets.Count).Name = "Brouillon" 

    list_de_controle = "TEXT;" & listPath 'here I have a list to open 

    For i = 1 To nombre_Ligne 'From the first to the last line in "Data"sheet 

     mon_Objet = Cells(i, 15).Text + "_" + Cells(i, 17).Text 
     Open listPath For Input As #1 'open the list of tests 

     Do While Not EOF(1) 
      Line Input #1, nom_de_Fich 
      If InStr(nom_de_Fich, mon_Objet) > 0 Then 
      'if this file is the correct test file I want to open 

       mfile = Dir(nom_de_Fich & "*.*") 
       If mfile <> "" Then 
        GoTo_Brouillon 'go to the new sheet 

        Open nom_de_Fich For Input As #2 'open the file 
         Insérer_contenu 
        Close #2 

       End If 

       compléter_Tableau 'with this new sheet, I will fill the table 

      End If 

     Loop 
     Close #1 

    Next 

End Sub 

,併爲小組compéter_Tableau():

Public Sub compléter_Tableau() 

    Dim ligne_Brouillon 
    Sheets("Brouillon").Range("A1").Select 
    ActiveCell.End(xlDown).Select 
    ligne_Brouillon = Selection.Row 
    ActiveCell.End(xlToRight).Select 
    colonne_brouillon = Selection.Column 

    For j = 1 To ligne_Brouillon 

     If InStr(Sheets("Brouillon").Cells(j, 7).Text, Sheets("Data").Cells(i, 18).Text) <> 0 Then 

      For k = 8 To colonne_brouillon 

       If InStr(Sheets("Brouillon").Cells(j, k).Text, Sheets("Data").Cells(i, 20).Text) <> 0 Then 
        Sheets("Data").Cells(i, ma_Colonne).Text = Sheets("Brouillon").Cells(j, k).Text 
       End If 

      Next 

     End If 

    Next 
End Sub 
+1

'Sheets _(「Brouillon」)'刪除下劃線和空格,與'Sheets _(「Data 「)'。在代碼分成多行的地方使用'_' – Dave

+0

您也不會在顯示的代碼中將「i」設置爲任何內容。之前定義過嗎? – Dave

+0

對不起,我的宏中沒有這些下劃線,就在我發佈這個問題時,這一行太長了,所以我添加了一個下劃線。但似乎它不能像VBA一樣將工作分爲兩部分。 – Hiddenllyy

回答

0
Private Sub Button_Importer_Click() 

    Sheets.Add After:=Sheets(Sheets.Count) 
    Sheets(Sheets.Count).Name = "Brouillon" 

    list_de_controle = "TEXT;" & listPath 'here I have a list to open 

    For i = 1 To nombre_Ligne 'From the first to the last line in "Data"sheet 

     mon_Objet = Cells(i, 15).Text + "_" + Cells(i, 17).Text 
     Open listPath For Input As #1 'open the list of tests 

     Do While Not EOF(1) 
      Line Input #1, nom_de_Fich 
      If InStr(nom_de_Fich, mon_Objet) > 0 Then 
      'if this file is the correct test file I want to open 

       mfile = Dir(nom_de_Fich & "*.*") 
       If mfile <> "" Then 
        GoTo_Brouillon 'go to the new sheet 

        Open nom_de_Fich For Input As #2 'open the file 
         Insérer_contenu 
        Close #2 

       End If 

       compléter_Tableau i ' pass i into the other sub so it can be used 

      End If 

     Loop 
     Close #1 

    Next 
    End Sub 

並更改第二次接受參數,以便它知道什麼i是:

Public Sub compléter_Tableau(byVal i) 

    Dim ligne_Brouillon 
    ligne_Brouillon = Sheets("Brouillon").Cells(Sheets("Brouillon").Rows.Count, "A").End(xlUp).Row 
    colonne_brouillon = Sheets("Brouillon").Cells("A", Sheets("Brouillon").Columns.Count).End(xlToLeft).Column 

    For j = 1 To ligne_Brouillon 

     If InStr(Sheets("Brouillon").Cells(j, 7).Text, Sheets("Data").Cells(i, 18).Text) <> 0 Then 

      For k = 8 To colonne_brouillon 

       If InStr(Sheets("Brouillon").Cells(j, k).Text, Sheets("Data").Cells(i, 20).Text) <> 0 Then 
        Sheets("Data").Cells(i, ma_Colonne).Text = Sheets("Brouillon").Cells(j, k).Text 
       End If 

      Next 

     End If 

    Next 
End Sub 

我也簡化了選擇最後一行和一列的方法;儘量避免使用ActiveCellSelect s,因爲它們在幾乎任何情況下都不是必需的,並且會減慢速度

+0

謝謝!但'compléter_Tableaui'這一行會導致編譯錯誤:錯誤的參數數量或無效的屬性賦值 – Hiddenllyy

+0

錯誤發生時'i'的值是多少?使用F8鍵執行代碼執行,並注意值 – Dave

+0

我是vide。但第一個文件已被打開並放入新的表格中。我不明白 – Hiddenllyy