我如何使整数只出现在最终的工作簿中

问题描述 投票:0回答:1

嗨,我有这段代码可以计算“ T”出现在各种工作簿中的时间,其中每个工作簿大约有10至12张纸。主要目的是使最终工作簿获得T遍历文件时出现的总次数。

Private Sub CommandButton3_Click()

    Dim ws As Worksheet
    Dim wb As Workbook
    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String
    Dim FldrPicker As FileDialog
    Dim x As Integer
    Dim wash_count As Integer

    'Optimize Macro Speed
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    'Retrieve Target Folder Path From User
    Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
        .Title = "Select A Target Folder"
        .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

'In Case of Cancel
NextCode:
    myPath = myPath
    If myPath = "" Then GoTo ResetSettings

    'Target File Extension (must include wildcard "*")
    myExtension = "*.xls*"

    'Target Path with Ending Extention
    myFile = Dir(myPath & myExtension)

    'Loop through each Excel file in folder
    Do While myFile <> ""
        'Set variable equal to opened workbook
        Set wb = Workbooks.Open(Filename:=myPath & myFile)

        'Ensure Workbook has opened before moving on to next line of code
        DoEvents

        For Each ws In wb.Sheets

            If ws.Name <> "Summary" Then

                For x = 5 To 74
                    If StrConv(ws.Cells(x, 2).Value, vbProperCase) = "Wash" And StrConv(ws.Cells(x, 4).Value, vbProperCase) = "T" Then 'added the StrConv to make sure you don't loose a case just because it was written in capital or lowercase letters
                        wash_count = wash_count + 1
                    End If
                Next x
             End If
        Next ws

        Sheets("Summary").Range("D6") = wash_count

        'Save and Close Workbook
        wb.Close SaveChanges:=True

        'Ensure Workbook has closed before moving on to next line of code
        DoEvents

        'Get next file name
        myFile = Dir

    Loop

    'Message Box when tasks are completed
    MsgBox "Task Complete!"

ResetSettings:

    'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

现在的问题是,以前的工作簿(其工作表也称为“摘要”)也获得了计数器输入。现在,代码将在最后一个工作簿中获得最终计数,但是如何避免将其显示在先前工作簿的摘要页面中,我想将所有计数仅定向到最终工作簿中。在此先感谢

excel vba count worksheet
1个回答
0
投票

您可以执行以下操作:

Dim ColFiles As New Collection, i As Long

'...
'...
'...

'collect the files first
myFile = Dir(myPath & myExtension)
Do While myFile <> ""
    ColFiles.Add myPath & myFile
    myFile = Dir()
Loop

'now loop over the collection
For i = 1 To ColFiles.Count

    Set wb = Workbooks.Open(ColFiles(i))
    'count things in wb

    'is this the last file?
    If i = ColFiles.Count Then
        wb.Sheets("Summary").Range("D6") = wash_count
        wb.Close True  'save
    Else
        wb.Close False 'no save
    End If

Next i
© www.soinside.com 2019 - 2024. All rights reserved.