使用VB和.cmd删除Excel工作表中的额外行

时间:2013-07-12 15:56:35

标签: excel batch-file vb6 runtime

我有一张大型Excel工作表,其中包含大约30000个条目的日志。

我之前的程序员创建了一个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

2 个答案:

答案 0 :(得分:0)

你有这些线到Sub的末尾:

oXL.Quit

Handler:
oXL.Quit

第二次Quit调用失败,产生错误。您需要在Handler之前退出该过程(只有在出现错误时才会调用):

oXL.Quit
Exit Sub

Handler:
oXL.Quit

答案 1 :(得分:0)

这是因为代码'落后'到line-label Handler。{。} 因此,当您的Handler尝试调用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

希望这有帮助!

修改
似乎我通过chat帮助提问者的原始问题已被删除,之后又重新发布(我假设获得一些新的页面浏览量)。
尽管Andy G已经回答了这个重新发布的帖子,但我还是不想让我的回答浪费​​并发布它,希望解释和参考可以帮助未来的读者。