【发布时间】:2021-10-18 16:35:47
【问题描述】:
我以前做过很多循环,但由于某种原因,这个不起作用。我想在以“vip”开头的文件夹中查找所有 Excel 文件并运行另一个宏。第二个宏做了很多事情并将文件保存在另一个文件夹中(因此为什么当循环结束时,我会杀死所有剩余的文件)。
问题是当我运行循环时,找到了第一个文件,运行了第二个宏,但是当它循环时,它找不到第二个文件。我做了一个带有计数器的版本,但这并没有真正起作用。计数器可以找到我需要运行宏的三个文件,但是在启动第二个宏的循环时发生了同样的问题,它会找到第一个文件,而第二个文件将找不到。我什至尝试了一个循环打开所有 Excel 文件的版本,使用 IF 语句来查找要在哪些文件上运行宏以及要简单地关闭哪些文件,但我遇到了与其他两个版本完全相同的问题。
所以基本上,它会找到第一个文件,执行它必须执行的操作,循环然后给我错误 1004 消息,其中文件夹可能已被移动、重命名或删除。我被难住了。
感谢您的帮助!
这是我的代码(对不起法语的说法,但无论如何你应该明白代码的作用!):
' Loop_pour_importation_Zoho
'
'Bloquer mise-à-jour visuelle
Application.ScreenUpdating = False
'Bloquer les alertes
Application.DisplayAlerts = False
'Loop pour mise en page de tous les fichiers Excel
Dim VIP As String
Dim Folder As String
Dim Compte As Integer
Dim CompteLoop As Integer
Folder = "C:\Users\Raid\Desktop\RAID\Test\importation\"
VIP = Dir(Folder & "vip*.xls*")
Do While Len(VIP) > 0
With Workbooks.Open(Folder & VIP)
Call Module20.Mise_en_forme_Zoho
End With
VIP = Dir
Loop
'Effacer les fichiers originaux
Kill "C:\Users\Raid\Desktop\RAID\Test\importation\*.xls*"
'Réactiver mise-à-jour visuelle
Application.ScreenUpdating = Vrai
'Réactiver les alertes
Application.DisplayAlerts = Vrai
End Sub
Sub Mise_en_forme_Zoho()
'
' Mise_en_forme_Zoho Macro
'
'Nommer les variables
Dim LastRow As Integer
Dim Dossier As String
Dim Document As String
Dim RapportTraid As Workbook
Dim Fichier As String
Dim Liste As Workbook
Dim DernLigne As Integer
Dim ProchLigne As Integer
Dim NextRow As Integer
Dim Infos As Integer
Dim CodeTel As Workbook
Dim LigneFin As Integer
Dim LigneSuite As Integer
'Définir le rapport à modifier
Dossier = "C:\Users\Raid\Desktop\RAID\Test\importation\"
Fichier = Dir(Dossier & "vip*.xls")
Set Liste = Workbooks.Open(Dossier & Fichier)
'Compteur de lignes à partir de la colonne 1
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
'Insérer et nommer les nouvelles colonnes
Columns("A:S").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Source"
Range("B1").Select
ActiveCell.FormulaR1C1 = "code tel"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Phone"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Lead Status"
Range("E1").Select
ActiveCell.FormulaR1C1 = "start_date"
Range("F1").Select
ActiveCell.FormulaR1C1 = "end_date"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Offers"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Concessionnaire"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Semaine"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Event Date"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Email"
Range("L1").Select
ActiveCell.FormulaR1C1 = "Brand/Marque"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Model/Modèle"
Range("N1").Select
ActiveCell.FormulaR1C1 = "Year/Année"
Range("O1").Select
ActiveCell.FormulaR1C1 = "KM"
Range("P1").Select
ActiveCell.FormulaR1C1 = "marque"
Range("Q1").Select
ActiveCell.FormulaR1C1 = "model"
Range("R1").Select
ActiveCell.FormulaR1C1 = "année"
Range("S1").Select
ActiveCell.FormulaR1C1 = "Last Name"
'Remplir les informations de base
For i = 2 To LastRow
Cells(i, 1).Select
ActiveCell.FormulaR1C1 = "VP"
Cells(i, 4).Select
ActiveCell.FormulaR1C1 = "Imported"
Cells(i, 3).Select
ActiveCell.FormulaR1C1 = "=RC[24]"
Cells(i, 11).Select
ActiveCell.FormulaR1C1 = "=RC[22]"
Cells(i, 16).Select
ActiveCell.FormulaR1C1 = "=RC[18]"
Cells(i, 17).Select
ActiveCell.FormulaR1C1 = "=RC[18]"
Cells(i, 18).Select
ActiveCell.FormulaR1C1 = "=RC[18]"
Cells(i, 19).Select
ActiveCell.FormulaR1C1 = "=RC[6]&"" ""&RC[7]"
Next i
'Ouvrir le rapport traid
Range("U2").Select
Selection.Copy
Document = Dir(Dossier & "traid*.xls")
Set RapportTraid = Workbooks.Open(Dossier & Document)
'Compter les lignes du rapport à partir de la ligne 1
DernLigne = Cells(Rows.Count, 1).End(xlUp).Row
ProchLigne = DernLigne + 1
'Trouver la ligne correspondant à l'événement
Range("A" & ProchLigne).Activate
Range("A" & ProchLigne).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
For i = 2 To DernLigne
If Cells(i, 3).Value = Range("A" & ProchLigne) Then
Range("A" & i & ":S" & i).Copy
Liste.Activate
NextRow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row
Worksheets("vip").Activate
Range("A" & NextRow & ":S" & NextRow).Activate
Worksheets("vip").Select
Range("A" & NextRow & ":S" & NextRow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next i
'Remplir les information du rapport Traid
Infos = LastRow + 1
For i = 2 To LastRow
Cells(i, 5).Select
ActiveCell.Value = Cells(Infos, 8)
Cells(i, 7).Select
ActiveCell.Value = Cells(Infos, 13)
Cells(i, 8).Select
ActiveCell.Value = Cells(Infos, 7)
Next i
'Séparer le début et la fin de l'événement
Columns("E:E").Select
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.TextToColumns Destination:=Range("E1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="/", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
'Remplir le numéro de la semaine
Range("I2").Select
ActiveCell.FormulaR1C1 = "=ISOWEEKNUM(RC[-4])"
'Trouver le jour de la semaine
Range("T" & Infos).Select
ActiveCell.FormulaR1C1 = "=WEEKDAY(R2C[-15])"
Range("U" & Infos).Select
ActiveCell.FormulaR1C1 = "=WEEKDAY(R2C[-16])+1"
Range("V" & Infos).Select
ActiveCell.FormulaR1C1 = "=WEEKDAY(R2C[-16])"
'Changer le jour en nom
Range("T52:V52").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.Replace What:="1", Replacement:="Sunday", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.Replace What:="2", Replacement:="Monday", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.Replace What:="3", Replacement:="Tuesday", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.Replace What:="4", Replacement:="Wednesday", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.Replace What:="5", Replacement:="Thursday", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.Replace What:="6", Replacement:="Friday", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.Replace What:="7", Replacement:="Saturday", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
'Rentrer les jours dans Event Date
Range("J" & Infos & ":J" & Infos).Select
Selection.TextToColumns Destination:=Range("J52"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:=Chr(10), FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
Range("J3").Select
ActiveCell.FormulaR1C1 = "=R" & Infos & "C[10]&RIGHT(R" & Infos & "C,LEN(R" & Infos & "C)-5)"
Range("J4").Select
ActiveCell.FormulaR1C1 = "=R" & Infos & "C[11]&RIGHT(R" & Infos & "C[1],LEN(R" & Infos & "C[1])-5)"
Range("J5").Select
ActiveCell.FormulaR1C1 = "=R" & Infos & "C[12]&RIGHT(R" & Infos & "C[2],LEN(R" & Infos & "C[2])-5)"
Range("J2").Select
ActiveCell.Value = Range("J3") & Chr(10) & Range("J4") & Chr(10) & Range("J5")
'Compléter les infos dans toutes les lignes
For i = 3 To LastRow
Cells(i, 9).Value = Cells(2, 9).Value
Cells(i, 10).Value = Cells(2, 10).Value
Next i
'Mise en forme générale
Range("F1").Value = "end_date"
Columns("A:A").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'Supprimer la ligne du rapport Traid dans Liste
Rows(Infos).Select
Selection.Delete Shift:=xlUp
'Supprimer le numéro de campagne du rapport Traid et fermer
RapportTraid.Activate
Rows(ProchLigne).Select
Selection.Delete Shift:=xlUp
ActiveWorkbook.Close
'Copier le nom du concessionnaire
Liste.Activate
Range("H2").Select
Selection.Copy
'Ouvrir le rapport code de téléphonie
Set CodeTel = Workbooks.Open("C:\Users\Raid\Desktop\RAID\Test\Macro TRaid\Code Tel.xlsx")
CodeTel.Activate
'Compter les lignes de Code Tel à partir de la ligne 1
LigneFin = Cells(Rows.Count, 1).End(xlUp).Row
LigneSuite = LigneFin + 1
'Trouver la ligne correspondant au concessionnaire
Range("A" & LigneSuite).Activate
Range("A" & LigneSuite).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
For i = 1 To LigneFin
If Cells(i, 1).Value = Range("A" & LigneSuite) Then
Range("B" & i).Copy
End If
Next i
Liste.Activate
Worksheets("vip").Activate
Range("B2").Activate
Worksheets("vip").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Fermer Code Tel
CodeTel.Activate
Rows(LigneSuite).Select
Selection.Delete Shift:=xlUp
ActiveWorkbook.Close
'Remplir Liste avec Code Tel
Liste.Activate
Worksheets("vip").Activate
For i = 3 To LastRow
Cells(i, 2).Value = Cells(2, 2).Value
Next i
Columns("B:B").Select
Selection.NumberFormat = "@"
'Sauvegarder, fermer et supprimer Liste
ChDir "C:\Users\Raid\Desktop\RAID\Test\vers Zoho\"
ActiveWorkbook.SaveAs Filename:= _
Fichier, FileFormat:=xlExcel8, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
Liste.Close
End Sub
【问题讨论】:
-
Mise_en_forme_Zoho会发生什么? -
@TimWilliams 一大堆东西。它打开“vip”Excel文件,添加列,用文件中已有的信息填充其中的一些,并用两个单独的Excel文件(一个在“文件夹”中,一个在其他地方不移动)的信息填充其余部分。然后保存文件并关闭它。代码太长,无法粘贴到这里。
-
你在那个方法中使用
Dir()吗?如果您注释掉Call行,您的代码是否会打开文件夹中的每个工作簿?你说它“打开文件”但文件已经打开了? -
@TimWilliams 不,Mise_en_forme_Zoho 不会打开 VIP 文件,只有循环会。 Mise_en_forme_Zoho 在与该模块中定义的 VIP 文件相同的文件夹中打开一个 Excel 报告,并在完成后将其关闭。它还会打开一个在模块中定义的参考 Excel 文件,并在完成后将其关闭。 Mise_en_forme_Zoho 关闭了 VIP 文件,我不应该这样做吗?
-
@TimWilliams 在 Mise_en_forme_Zoho 中,我不使用 Dir()。我定义目录来调用我需要的不同文件,但我不循环或类似的东西。