动态数组的 VBA 错误

发布于 2025-01-12 07:48:37 字数 2542 浏览 5 评论 0原文

我是 VBA 初学者。我正在尝试编写一个宏来检查多个文件中是否存在丢失的数据。如果数据丢失,它将把文件名添加到一个数组中,最后给出数据不完整的文件列表。

我需要一个可变数组,但无论我做什么来修复它,我都会不断收到错误。请参阅下面的代码。任何帮助将非常感激!

Sub Macro()

Dim Path1 As String
Dim Path2 As String
Dim Path3 As String
Dim Path4-5-6 As String
Dim FileName_1 As String
Dim FileName_2 As String
Dim FileName_3 As String
Dim FileName_4 As String
Dim FileName_5 As String
Dim FileName_6 As String
Dim W As Long
Dim Missing As Boolean
Dim len_ListMissing As Long
Dim Found As Boolean
Dim k As Variant
Dim dict
Set dict = CreateObject("Scripting.Dictionary")
Dim n As Integer, i As Variant
ReDim ListMissing(n)

Path1 = "https://_path1_"
Path2 = "https://_path2_"
Path3 = "https://_path3_"
Path4-5-6 = "https://_path4-5-6_"
FileName_1 = "file1.xlsx"
FileName_2 = " file2.xlsx"
FileName_3 = " file3.xlsx"
FileName_4 = " file4.xlsx"
FileName_5 = " file5.xlsx"
FileName_6 = " file6.xlsx"

W = Workbooks("_currentfile_.xlsm").Sheets("Sheet1").Cells(1, "A").Value
dict.Add Path1 & FileName_1, FileName_1
dict.Add Path2 & FileName_2, FileName_2
dict.Add Path3 & FileName_3, FileName_3
dict.Add Path4-5-6 & FileName_4, FileName_4
dict.Add Path4-5-6 & FileName_5, FileName_5
dict.Add Path4-5-6 & FileName_6, FileName_6

ListMissing(0) = "test"

For Each k In dict.keys
Workbooks.Open (k)
    If Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “X”).Value = "" Or Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “X”).Value = "0" Or Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “Y”).Value = "" Or Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “Y”).Value = "0" Or Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “Z”).Value = "" Or Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “Z”).Value = "0" Then
        Missing = True
    Else
        Missing = False
    End If
    
    n = 0
    If Missing = True Then
        For Each i In ListMissing
            If i = dict(k) Then
                Found = True
            Else
                Found = False
            End If
                
            If Found = False Then
                n = n + 1
                ReDim Preserve ListMissing(1 To n)
                ListMissing(n) = dict(k)
            Else

            End If

        Next i
    End If
Workbooks(dict(k)).Close SaveChanges:=False
Next

len_ListMissing = UBound(ListMissing) - LBound(ListMissing) + 1
If len_ListMissing = 1 Then
    MsgBox "Data is complete."
Else
    MsgBox "Data is incomplete in the below files:" & vbCrLf & vbCrLf & ListMissing(i)
End If

End Sub

I am a beginner in VBA. I am trying to write a macro that checks if there is any missing data in several files. If data is missing, it would add the name of the file to an array, and at the end it would give the list of files with incomplete data.

I need to have a variable array, but whatever I do to fix it, I keep getting errors. See the code below. Any help would be very much appreciated!!!

Sub Macro()

Dim Path1 As String
Dim Path2 As String
Dim Path3 As String
Dim Path4-5-6 As String
Dim FileName_1 As String
Dim FileName_2 As String
Dim FileName_3 As String
Dim FileName_4 As String
Dim FileName_5 As String
Dim FileName_6 As String
Dim W As Long
Dim Missing As Boolean
Dim len_ListMissing As Long
Dim Found As Boolean
Dim k As Variant
Dim dict
Set dict = CreateObject("Scripting.Dictionary")
Dim n As Integer, i As Variant
ReDim ListMissing(n)

Path1 = "https://_path1_"
Path2 = "https://_path2_"
Path3 = "https://_path3_"
Path4-5-6 = "https://_path4-5-6_"
FileName_1 = "file1.xlsx"
FileName_2 = " file2.xlsx"
FileName_3 = " file3.xlsx"
FileName_4 = " file4.xlsx"
FileName_5 = " file5.xlsx"
FileName_6 = " file6.xlsx"

W = Workbooks("_currentfile_.xlsm").Sheets("Sheet1").Cells(1, "A").Value
dict.Add Path1 & FileName_1, FileName_1
dict.Add Path2 & FileName_2, FileName_2
dict.Add Path3 & FileName_3, FileName_3
dict.Add Path4-5-6 & FileName_4, FileName_4
dict.Add Path4-5-6 & FileName_5, FileName_5
dict.Add Path4-5-6 & FileName_6, FileName_6

ListMissing(0) = "test"

For Each k In dict.keys
Workbooks.Open (k)
    If Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “X”).Value = "" Or Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “X”).Value = "0" Or Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “Y”).Value = "" Or Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “Y”).Value = "0" Or Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “Z”).Value = "" Or Workbooks(dict(k)).Sheets("Sheet2").Cells(W + 20, “Z”).Value = "0" Then
        Missing = True
    Else
        Missing = False
    End If
    
    n = 0
    If Missing = True Then
        For Each i In ListMissing
            If i = dict(k) Then
                Found = True
            Else
                Found = False
            End If
                
            If Found = False Then
                n = n + 1
                ReDim Preserve ListMissing(1 To n)
                ListMissing(n) = dict(k)
            Else

            End If

        Next i
    End If
Workbooks(dict(k)).Close SaveChanges:=False
Next

len_ListMissing = UBound(ListMissing) - LBound(ListMissing) + 1
If len_ListMissing = 1 Then
    MsgBox "Data is complete."
Else
    MsgBox "Data is incomplete in the below files:" & vbCrLf & vbCrLf & ListMissing(i)
End If

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

血之狂魔 2025-01-19 07:48:37

这里有一个使用字典存储缺失状态(代码而不是测试集)的示例,

dict.Add sFullPath1, true
dict.Add sFullPath2, true

for each k in dict
   set wbToCheck = Workbooks.Open(k)
   if wbToCheck.Sheets("Sheet2").Cells(W + 20, “X”).Value = "" then 'only one condition in this example
     dict(k)= true
   else
     dict(k) = false
   end if
   wbToCheck.Close SaveChanges:=False
next

因此您不需要这个额外的数组。

Here a example to use the dictonary to store the missing status (code not testet)

dict.Add sFullPath1, true
dict.Add sFullPath2, true

for each k in dict
   set wbToCheck = Workbooks.Open(k)
   if wbToCheck.Sheets("Sheet2").Cells(W + 20, “X”).Value = "" then 'only one condition in this example
     dict(k)= true
   else
     dict(k) = false
   end if
   wbToCheck.Close SaveChanges:=False
next

so you would not need this additional array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文