vba 运行时错误 = '5':无效的过程调用或参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21568116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:44:48  来源:igfitidea点击:

Run-time error = '5' : invalid procedure call or argument

excelexcel-vbavba

提问by taka305

I am importing a .txtfile and reformatted the data to remove commas. When I try to save it, it give me the run time error. How can I fix this? Any help would greatly be appreciated!

我正在导入一个.txt文件并重新格式化数据以删除逗号。当我尝试保存它时,它给了我运行时错误。我怎样才能解决这个问题?任何帮助将不胜感激!

Sub SaveNewFile()

ActiveCell.SpecialCells(xlLastCell).Select
endrow = Selection.Row
Cells(1, 1).Select

If TopSide = 1 Then
    BoardName = BoardName & "_TOP"
Else
    BoardName = BoardName & "_BOT"
End If
filesavename = Application.GetSaveAsFilename( _
    InitialFilename:=BoardName, FileFilter:="YTV CAD Files (*.ycd), *.ycd")
    If filesavename = "False" Then End

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile(filesavename, True)
    For i = 1 To 17
        For j = 1 To 2
            a.write (Cells(i, j) & "     ")
        Next j
        a.writeline
    Next i
    For i = 18 To endrow
        For j = 1 To 7
            a.write (Cells(i, j) & "     ")
        Next j
        a.writeline
    Next i
    a.Close

    Application.DisplayAlerts = False
    'ActiveWorkbook.SaveAs Filename:= _
    '    filesavename, _
    '    FileFormat:=xlTextPrinter, CreateBackup:=False
    jmessage = MsgBox(filesavename & " has been generated successfully!", vbOKOnly, "YCD Export")
    Call YCDFileLocationIni
    'Application.DisplayAlerts = True
    Workbooks.Open (CADFileName)
    Workbooks(ShortCADFileName).Activate
    ActiveWorkbook.Close
End Sub

回答by Siddharth Rout

First things first

第一件事

Change

改变

filesavename = Application.GetSaveAsFilename( _
                                            InitialFileName:=BoardName, _
                                            FileFilter:="YTV CAD Files (*.ycd), *.ycd")
If filesavename = "False" Then End

to

Dim filesavename As Variant

filesavename = Application.GetSaveAsFilename( _
                                            InitialFileName:=BoardName, _
                                            FileFilter:="YTV CAD Files (*.ycd), *.ycd")
If filesavename = False Then Exit Sub

You should never use End. Reason is quite simple. It's like Switching Off your Computer using the POWER OFF button. The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Also the Object references held (if any) by other programs are invalidated.

你永远不应该使用End. 原因很简单。这就像使用 POWER OFF 按钮关闭计算机一样。End 语句会突然停止代码执行,而不调用 Unload、QueryUnload 或 Terminate 事件或任何其他 Visual Basic 代码。其他程序持有的对象引用(如果有的话)也会失效。

Next, please avoid the use of .Select/.ActivateINTERESTING READ

接下来,请避免使用.Select/.ActivateINTERESTING READ

The other things that don't make sense (but will not generate an error unless you have Option Explicit On) is BoardName. Where are you setting it's previous value? Same goes with TopSide. Use Option Explicitand major part of your problem will go away.

其他没有意义的事情(但不会产生错误,除非你有 Option Explicit On)是BoardName. 你在哪里设置它的先前值?也一样TopSide。使用Option Explicit和您的问题的主要部分将消失。

This definitely doesn't make any sense to me

这对我来说绝对没有任何意义

Workbooks.Open (CADFileName)
Workbooks(ShortCADFileName).Activate

What is the value of CADFileNameor ShortCADFileName

CADFileName或的价值是什么ShortCADFileName

Also did you step through the code of YCDFileLocationIni?

您是否也逐步完成了YCDFileLocationIni?

All in all, Once I made the above changes to your code plus few small changes like declaring CADFileName, ShortCADFileName, your code ran without any errors.

总而言之,一旦我对您的代码进行了上述更改,再加上声明CADFileName,等一些小的更改ShortCADFileName,您的代码就会运行而没有任何错误。