excel 添加GetOpenFilename后VBA代码无法工作

7jmck4yq  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(166)

我需要帮助来找出问题所在。我编写了一段很长的代码,基本上是通过在单独的工作表上分离和组合信息来重新设置工作簿的格式,然后将每个工作表单独保存为CSV格式。下面是我的代码的开头:

Sub All()
Dim Bottom As Long
Dim Header As Long

> 'A. CHECK DATE

If ThisWorkbook.Sheets("ACH PULL").Range("C1") <> Date Then
MsgBox "ERROR" & Chr(10) & "Date on file is different than today's date" & Chr(13) & "Ask client for corrected file"
Exit Sub
Else

> '1. OUTGOING CHECKS

Sheets("OUTGOING CHECKS").Select
Bottom = WorksheetFunction.Match((Cells(Rows.Count, 1).End(xlUp)), Range("A:A"), 0)
Header = WorksheetFunction.Match("Account*", Range("A:A"), 0)
If Bottom <> Header Then
MsgBox "ERROR" & Chr(10) & "The batch contains outgoing checks" & Chr(13) & "Ask client for corrected file"
Exit Sub

Bottom和Header分别用于查找范围的标题和最后一行。我在我的代码中多次在单独的工作表上使用它。
当我从需要修改的文件运行代码时,代码运行得很好。但是我需要将它分配给另一个电子表格的按钮,以便通过VBA打开要修改的文件,然后应用代码。因此,我添加了以下代码:

Sub All()
Dim FileToOpen As Variant
Dim NewBatch As Workbook
Dim Bottom As Integer
Dim Header As Integer



FileToOpen = Application.GetOpenFilename(Title:="Find batch file")
If FileToOpen <> False Then
Set NewBatch = Application.Workbooks.Open(FileToOpen)
End If


'A. CHECK DATE

If Sheets("ACH PULL").Range("C1") <> Date Then
MsgBox "ERROR" & Chr(10) & "Date on file is different than today's date" & Chr(13) & "Ask client for corrected file"
Exit Sub
Else

'1. OUTGOING CHECKS


Sheets("OUTGOING CHECKS").Select

Bottom = WorksheetFunction.Match((Cells(Rows.Count, 1).End(xlUp)), Range("A:A"), 0)
Header = WorksheetFunction.Match("Account*", Range("A:A"), 0)
End If
If Bottom <> Header Then
MsgBox "ERROR" & Chr(10) & "The batch contains outgoing checks" & Chr(13) & "Ask client for corrected file"
Exit Sub
.. The rest of the code

现在,当我试着运行它时,它运行得很好,直到行:
Bottom =工作表函数.匹配((单元格(行.计数,1).结束(xlUp)),区域(“A:A”),0)
我要么得到1004或400错误。我调暗了我需要使用的两个整数,我试图作出多个更改,包括激活工作簿,但仍然不确定是什么导致的问题,在这部分的代码。
我有两个部分(VBA打开工作簿和重新格式化代码)分别工作,但我不能将它们结合起来!
我在使用它们之前对我需要使用的两个整数进行了标注。
NewBatch.Activate
但实际上并没有什么不同,因为打开的工作簿已经被激活了。我试着设置底部和标题的值,但也不起作用。
谢谢你!

mwg9r5ms

mwg9r5ms1#

可能是这样的:

Sub All()
    
    Dim FileToOpen As Variant
    Dim NewBatch As Workbook
    Dim Bottom As Long, Header As Variant 'not Long
    
    FileToOpen = Application.GetOpenFilename(Title:="Find batch file")
    If FileToOpen = False Then Exit Sub 'user cancelled open
    
    Set NewBatch = Application.Workbooks.Open(FileToOpen)
    
    'A. CHECK DATE
    If NewBatch.Sheets("ACH PULL").Range("C1").Value <> Date Then
        ProblemMsg "Date on file is different than today's date." & _
                    vbLf & "Ask client for corrected file"
        Exit Sub
    End If
    
    '1. OUTGOING CHECKS
    With NewBatch.Sheets("OUTGOING CHECKS")
        Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row 'last entry in Col A
        Header = Application.Match("Account*", .Range("A:A"), 0) 'not WorksheetFunction.Match

        If IsError(Header) Then 'make sure we located "Account*"
            ProblemMsg "'Account*' not found in ColA on sheet '" & .Name & "'"
        Else
            If Bottom <> Header Then
                ProblemMsg "The batch contains outgoing checks." & vbLf & _
                           "Ask client for corrected file."
                Exit Sub
            End If
        End If
    End With
    
    '...
    '...
End Sub

'Utility sub for displaying error messages
Sub ProblemMsg(msg As String)
    MsgBox "ERROR" & vbLf & msg, vbExclamation, "Please review"
End Sub
fv2wmkja

fv2wmkja2#

我发现通过定义工作表和引用,而不是依赖选择或活动工作表,可以获得更可靠的性能。请尝试在引用范围()和单元格()之前定义和引用此行所在的工作表,看看是否有帮助。
尺寸ws为工作表集ws =工作表(“出库支票”)
底部=工作表函数.匹配((ws.单元格(行.计数,1).结束(xlUp)),ws.范围(“A:A”),0)

相关问题