Good morning,
I'd like to use a For cycle to compare two sheets' content, and do something if the condition is satisfied.
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
but I've met the error '1004' when I tried to run. I don't know who to fix this. Please give me some advices if you got some ideas. Thanks in advance.
Sure, my object is to fill a table of quality control, which contains the name of tests and the results. The tests and the results are reserved separately in some files. My work is to fill the table when I have a new test. And for each point of test, the criteria are the same (such as the colour, the form etc.), and I just need to fill the correct cell with the correct result. I want to open a new sheet and fill it with all the results and then fill the table with this new sheet. I think that could easier.
Here is the complete code:
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
and for the 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
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
and change the second Sub to accept the parameter so it knows what i
is:
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
I've also simplified your method for selecting the last row and column; try to avoid the use of ActiveCell
, and Select
s as they are not required in virtually any instance and slow things down
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.