[英]VBA_Error '1004' while using the InStr Function
早上好,
我想使用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
對於Subcompé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
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
並更改第二個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
我還簡化了選擇最后一行和最后一列的方法; 嘗試避免使用ActiveCell
和Select
因為實際上在任何情況下都不需要它們,並且會降低速度
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.