vba代码改成vbs
我有一段写在Excel中的VBA代码,现在需要从Excel独立,写成vbs。
用vbs浏览文件名,打开文件的代码已经写好。现在只需要把vba代码中的on error resume next,goto等语句换掉。
原VBA代码(可用):
Private Sub CommandButton1_Click()
On Error Resume Next
Dim fill()
ReDim fill(1 To ActiveSheet.UsedRange.Columns.Count)
For i = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
DoEvents
insert_count = 0
last_insert = 0
For y = 1 To ActiveSheet.UsedRange.Columns.Count
n = 0
If Len(Cells(i, y)) < 1 Then GoTo g_next1
If last_insert <> y Then i_count = insert_count
s_string = 0
s_string = WorksheetFunction.Find( "||| ", Cells(i, y), 1)
If s_string = 0 And Len(Cells(i, y).Value) > 0 Then fill(y) = True Else fill(y) = False
Do While True
s_string = 0
s_string = WorksheetFunction.Find( "||| ", Cells(i, y), 1)
If s_string = 0 Then GoTo g_next1
If last_insert = 0 Then
Rows(i + n + 1).Insert
insert_count = insert_count + 1
last_insert = y
Else
If last_insert = y Then
Rows(i + n + 1).Insert
insert_count = insert_count + 1
last_insert = y
Else
If i_count > = 1 Then
i_count = i_count - 1
Else
Rows(i + n + 1).Insert
insert_count = insert_count + 1
last_insert = y
End If
End If
End If
n = n + 1
If s_string = 1 Then Cells(i + n, y).Value = "--- " Else Cells(i + n, y).Value = Left(Cells(i, y), WorksheetFunction.Find( "||| ", Cells(i, y), 1) - 1)
Cells(i, y).Value = Right(Cells(i, y), Len(Cells(i, y)) - WorksheetFunction.Find( "||| ", Cells(i, y), 1) - 2)
Loop
g_next1: Next
If i_count > 0 Then
Rows(i + i_count + 1).Insert
For u = 1 To ActiveSheet.UsedRange.Columns.Count
Cells(i + i_count + 1, u).Value = Cells(i, u)
If fill(u) = True Then
For r = 1 To i_count
Cells(i + r, u).Value = Cells(i, u)
Next
End If
Next
Rows(i).Delete
End If
Next
End Sub
修改之后的代码(会报错)
Dim fill()
ReDim fill(1 To Sheets(0).UsedRange.Columns.Count)
For i = Sheets(0).UsedRange.Rows.Count To 1 Step -1
DoEvents
insert_count = 0
last_insert = 0
For y = 1 To Sheets(0).UsedRange.Columns.Count
n = 0
If Len(Cells(i, y)) > = 1 Then
If last_insert <> y Then i_count = insert_count
s_string = 0
s_string = WorksheetFunction.Find( "||| ", Cells(i, y), 1)
If s_string = 0 And Len(Cells(i, y).Value) > 0 Then fill(y) = True Else fill(y) = False
Do While True
s_string = 0
s_string = WorksheetFunction.Find( "||| ", Cells(i, y), 1)
If s_string <> 0 Then
If last_insert = 0 Then
Rows(i + n + 1).Insert
insert_count = insert_count + 1
last_insert = y
Else
If last_insert = y Then
Rows(i + n + 1).Insert
insert_count = insert_count + 1
last_insert = y
Else
If i_count > = 1 Then
i_count = i_count - 1
Else
Rows(i + n + 1).Insert
insert_count = insert_count + 1
last_insert = y
End If
End If
End If
'End If
n = n + 1
If s_string = 1 Then Cells(i + n, y).Value = "--- " Else Cells(i + n, y).Value = Left(Cells(i, y), WorksheetFunction.Find( "||| ", Cells(i, y), 1) - 1)
Cells(i, y).Value = Right(Cells(i, y), Len(Cells(i, y)) - WorksheetFunction.Find( "||| ", Cells(i, y), 1) - 2)
End If
Loop
End If
Next
If i_count > 0 Then
Rows(i + i_count + 1).Insert
For u = 1 To ActiveSheet.UsedRange.Columns.Count
Cells(i + i_count + 1, u).Value = Cells(i, u)
If fill(u) = True Then
For r = 1 To i_count
Cells(i + r, u).Value = Cells(i, u)
Next
End If
Next
Rows(i).Delete
End If
Next
能不能请高人看看修改之后的代码问题出来哪里?这段代码的作用,是有的单元格中含有多条记录,中间用|||分开,运行代码之后,可以把多条记录拆散,并且插入到后面的行中。
[解决办法]
在VBA中ActiveSheet就是Thisworkbook的活动sheet,
而在vbs中,你Sheets(0)是什么对象?谁知道?
你必须显式定义excel对象,如:
Dim ex 'As Excel.Application
Set ex = CreateObject( "Excel.Application ")
Dim wb 'As Excel.Workbook
Set wb = ex.Workbooks.Add
Dim sh 'As Excel.Worksheet
Set sh = ex.Worksheets.Add
sh.Cells(1, 1) = "aaaaaaaaaaaaaaa "
ex.Visible = True
上面代码是VBA,如果把注释号 '去掉,即VB代码!当然不去注释号 ',在VB也可以运行,知道为什么吗?