无法从VBA的上一个循环中清除vArray值

时间:2018-11-15 00:48:10

标签: vba excel-vba loops

我有一些vArray尚未清除。宏的目的是在具有30个以上标签的原始数据标签上工作,每个标签都包含特定办公室,001-New York等的信息。该宏应该选择x个标签(基于参考)文件),将它们复制并保存到新的工作簿中。问题在于,与其从原始数据文件中复制和保存,不如保存参考文件。 For ... Next循环用于确定从原始数据文件中选择并复制的选项卡/办公室。 varray位于循环内部,并包含办公室的名称。当代码遇到vArray时,当循环回绕时,不会清除varray的值。

示例:

“对于1”引用一个值为“ 8”的单元格,因此它将填充8个不同的vArray值(在这种情况下为办公室)。 “ For 2”的参考号为5,应该填充5个vArray值。它可以正确执行此操作,因为我可以在vArray(1)至vArray(5)下的locals窗口中看到5个新值,但是,vArray 6至8显示的是前一个循环的值,而不是'Empty'。宏循环时,不会清除vArray值。

sMasterListWBName是参考文件,它告诉宏从原始数据文件复制哪些选项卡以及将新创建的工作簿移至何处。该子对象还在循环的某些迭代中复制,保存和分发参考文件而不是原始数据文件(第二个问题-我将尽量避免拆分线程主题)。

在此先感谢所有尝试回答此问题的人。

Option Explicit
Dim iYear As Integer, iMonth As Integer, iVer As Integer, icount As Integer, iCount2 As Integer
Dim iLetter As String, iReport As String
Dim sMonth As String, sDate As String, sVer As String, sAnswer As String
Dim sFolderName As String, sManagerInitials  As String
Dim iManagerNumber As Integer, iManagerStart As Integer, iTabNumber As Integer, iTabStart As Integer
Dim sMasterListWBName As String, sConsolidatedWBName As String, sExists As String
Dim oSheet As Object, oDistList As Object
Dim vArray(300) As Variant
Dim wbDistList As Workbook
Dim wsAgentListSheet As Worksheet, wsMain As Worksheet
Dim rCell As Range, rCell2 As Range, rCellTotal As Range
Public sFINorAgent As String


Sub Agent_Distribute()
On Error Resume Next

iYear = frm_fin_rep_main_distribute.txt_year
iMonth = frm_fin_rep_main_distribute.txt_month
iVer = frm_fin_rep_main_distribute.txt_version

sMonth = Right("0" & iMonth, 2)
sDate = iYear & "." & sMonth

sVer = "V" & iVer
sAnswer = MsgBox("Is the following information correct?" & vbNewLine & vbNewLine & _
                    "Report - " & frm_fin_rep_main.sLetter & vbNewLine & _
                    "Year - " & iYear & vbNewLine & _
                    "Month - " & sMonth & vbNewLine & _
                    "Name - " & frm_fin_rep_main.sReport & vbNewLine & _
                    "Version - " & sVer, vbYesNo + vbInformation, "Please verify...")
If sAnswer <> vbYes Then
    Exit Sub
End If

Unload frm_fin_rep_main_distribute
frm_agent.Hide
Form_Progress

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

sConsolidatedWBName = ActiveWorkbook.Name
sMasterListWBName = "Dist Master List Final.xls"
If Not IsFileOpen(sMasterListWBName) Then
    Workbooks.Open FileName:= _
    "W:\Addins\01 GL - Distribution\" & sMasterListWBName, Password:="password"
    Workbooks(sConsolidatedWBName).Activate
End If

Set oDistList = Workbooks(sMasterListWBName).Worksheets("Agent")

With oDistList

    iManagerNumber = .Range("ManNumber2") 'range value = 66

    For iManagerStart = 2 To iManagerNumber '2 to 66
        If .Range("A" & iManagerStart) = "x" Then

            iTabNumber = .Range("E" & iManagerStart) 'E2 to E66
            sFolderName = .Range("F" & iManagerStart) 'F2 to F66
            sManagerInitials = .Range("G" & iManagerStart) 'G2 to G66

                For iTabStart = 1 To iTabNumber
                    vArray(iTabStart) = .Range("G" & iManagerStart).Offset(0, iTabStart)
                Next iTabStart

                If iTabNumber = 1 Then
                    Sheets(vArray(1)).Select
                Else
                    Sheets(vArray(1)).Select
                    For iTabStart = 2 To iTabNumber
                        Sheets(vArray(iTabStart)).Select False
                    Next iTabStart
                End If

                ActiveWindow.SelectedSheets.Copy

                ' *** the following code is optional, remove preceding apostrophes from the following four lines to enable password protection ***
                'For Each oSheet In ActiveWorkbook.Sheets
                    'oSheet.Protect "password"
                    'oSheet.EnableSelection = xlNoSelection
                'Next

                ActiveWorkbook.SaveAs FileName:= _
                "W:\Financials\" & iYear & "\" & sDate & "\Report to Distribute Electronically\Department Reports\" _
                & sFolderName & "\Current Year Financials" & "\" & "Y" & ") " & iYear & "-" & sMonth & " Agent Report Card " & sVer & " - " & sManagerInitials & ".xls"
                ActiveWorkbook.Close
        End If

        iPercent = iManagerStart / iManagerNumber * 95
        Task_Progress (iPercent)
    Next iManagerStart
End With

Workbooks(sMasterListWBName).Close False

Task_Progress (100)
Unload frm_progress

Set oDistList = Nothing

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

Message_Done

frm_agent.Show (vbModeless)

End Sub

1 个答案:

答案 0 :(得分:0)

我修复了它。我刚刚在循环末尾添加了“ Workbooks(sWbName).activate”,以确保焦点重新回到原始数据文件上。现在,所有文件都以正确的格式和位置保存。案例关闭,除非有人要添加其他内容。也许有人知道宏忽略其活动工作表的原因(保存参考文件而不是原始数据文件)。谢谢。