0

我有一个包含大约 30000 个条目的日志的大型 Excel 表。

我之前的程序员创建了一个removeline.cmd文件,以删除 excel 文件某一列中所有多余的空白行。

的代码RemoveLine.cmd

   cls
   cd\ 
   SET vbfile=newlinetest.exe
   K:
   cd "IPM - CompOps\Ops Docs\avail-stats\Originals"
   %vbfile%
   exit

该文件运行正确,但最后它显示此错误,这基本上是我想要摆脱的:

   Run-time error '1004';

   Method '~' of object '~' failed

编辑:
该程序newlinetest.exe是用 VB6 编写的(我可以在我的机器上访问它)。
完整的源代码newline.frm是:

VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 4500
ClientLeft = 3435
ClientTop = 3585
ClientWidth = 5175
LinkTopic = "Form1"
ScaleHeight = 4500
ScaleWidth = 5175
Begin VB.CommandButton Command1
Caption = "Excel"
Height = 495
Left = 1800
TabIndex = 0
Top = 3720
Width = 855
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Private Sub Command1_Click()
Dim oXL As Object ' Excel application
Dim oBook As Object ' Excel workbook
Dim oSheet As Object ' Excel Worksheet
Dim oChart As Object ' Excel Chart

Dim year As String

Dim i As Long

Dim MyRowNumber As Long

Dim Row As Long

Dim comment As String, newline As String

Dim curDate As String

Open "K:\IPM - CompOps\Ops Docs\avail-stats\Originals\Inputavailfile.txt" For Input As #1

Input #1, Data

Close #1




'Start Excel and create a new workbook
Set oXL = CreateObject("Excel.application")
Set oBook = oXL.Workbooks.Add
Set oSheet = oBook.Worksheets.Item(1)
oXL.Visible = True
oXL.UserControl = True

year = Format(Now, "yyyy")

curDate = Date - 3

curDate = Format(curDate, "m/d/yyyy")

Application.DisplayAlerts = False

Workbooks.Open FileName:="K:\IPM - CompOps\Ops Docs\avail-stats\Originals\" + Data

Myfile = "K:\IPM - CompOps\Ops Docs\avail-stats\Originals\" + Data

On Error GoTo Handler
vOurResult = Cells.Find(What:=curDate, LookAt:=xlWhole).Select

If (vOurResult = True) Then

MyRowNumber = ActiveCell.Row

Set ExcelLastCell = ActiveSheet.Cells.SpecialCells(xlLastCell)

'MsgBox vOurResult

Row = ExcelLastCell.Row
col = ExcelLastCell.Column

' MsgBox curDate

Cells(ActiveCell.Row, ActiveCell.Column + 6).Select



comment = ActiveCell.Text

newline = Replace(comment, Chr(10), " ")

ActiveCell.Value = newline


For i = MyRowNumber To Row - 1

comment = ""
newline = ""

Cells(ActiveCell.Row + 1, ActiveCell.Column).Select

comment = ActiveCell.Text

newline = Replace(comment, Chr(10), " ")

ActiveCell.Value = newline

Next i

'MsgBox curDate


ActiveWorkbook.SaveAs FileName:=Myfile, FileFormat:=xlNormal

End If
oXL.Quit

Handler:
oXL.Quit

End Sub

Private Sub Form_Load()
Command1_Click





End
End Sub

Private Sub Label1_Click()

End Sub
4

2 回答 2

0

那是因为代码“落入”到您line-label被调用的Handler.
因此,当您Handlerthen 尝试调用Method 'Quit' of object 'oXL'时,将会失败,因为 oXL 已经退出。

显而易见的解决方案是Exit Sub在它到达之前Handler

Sub 的一般布局(来自MSDN):

Sub InitializeMatrix(Var1, Var2, Var3, Var4)
   On Error GoTo ErrorHandler
   . . .
   Exit Sub
ErrorHandler:
   . . .
   Resume Next
End Sub

希望这可以帮助!

编辑:
似乎我通过聊天帮助提问者的原始问题已被删除,后来重新发布(我假设会获得一些新的页面浏览量)。
虽然 Andy G 已经回复了这篇转发,但我还是想着不要让我的答案浪费掉,还是把它贴出来了,希望解释和参考可以帮助未来的读者。

于 2013-07-12T20:39:07.287 回答
0

您在 Sub 的末尾有这些行:

oXL.Quit

Handler:
oXL.Quit

第二次Quit调用失败,产生错误。您需要在处理程序之前退出该过程(仅在发生错误时调用):

oXL.Quit
Exit Sub

Handler:
oXL.Quit
于 2013-07-12T16:29:12.743 回答