VBA代码投掷编译错误:预期数组
我有一个CSV文件,其中一个工作表中包含许多表。我在一个过程中编写代码的部分,以检查它是否在做我对每个表的期望。我在一个过程中粘贴的最终结果,现在我在代码开头的“ sub ip_vba()”中立即遇到了一个编译错误,我不知道该如何解决,有人可以帮助我吗? 如果我部分运行它,它可以做我想要的,但是当我将所有代码放在一起时。
Sub IP_VBA()
' Format IP table
Dim LR1 As Long, LR2 As Long, LR3 As Long, LR4 As Long, LR5 As Long
Dim FR1 As Long, FR2 As Long, FR3 As Long, FR4 As Long
Dim Rows As Long, i As Long
Dim Data As Range
'Delete tables 1, 2 and 3
Rows("1:3").EntireRow.Delete
Rows("1:1").Select
Range(Selection, Selection.End(xlDown).Offset(1, 0)).Delete Shift:=xlUp
Rows("1:2").EntireRow.Delete
Columns("A:I").EntireColumn.Delete
'Get necessary data from table 4
LR1 = ActiveSheet.Range("A1").CurrentRegion.Rows.Count
Range("B1:B" & LR1 & "," & "D1:E" & LR1 & "," _
& "G1:O" & LR1).Delete Shift:=xlToLeft
'Delete table 5
Rows(LR1 + 2).Select
Range(Selection, Selection.End(xlDown).Offset(1, 0)).Delete Shift:=xlUp
'Get necessary data from table 6
FR1 = Range("A" & LR1).Offset(2, 0).Row
Rows(FR1).EntireRow.Delete
LR2 = ActiveSheet.Range("A" & FR1).CurrentRegion.Rows.Count + LR1 + 1
Range("A" & FR1 & ":B" & LR2 & "," & "E" & FR1 & ":J" & LR2 & "," & _
"L" & FR1 & ":T" & LR2).Delete Shift:=xlToLeft
'Get necessary data from tables 7 and 8
FR2 = Range("A" & LR2).Offset(2, 0).Row
Rows(FR2).EntireRow.Delete
LR3 = ActiveSheet.Range("A" & FR2).CurrentRegion.Rows.Count + LR2 + 1
FR3 = Range("A" & LR3).Offset(2, 0).Row
Rows(FR3).EntireRow.Delete
LR3 = ActiveSheet.Range("A" & FR3).CurrentRegion.Rows.Count + LR3 + 1
Range("A" & FR2 & ":D" & LR3 & "," & "G" & FR2 & ":L" & LR3 & "," & _
"N" & FR2 & ":V" & LR3).Delete Shift:=xlToLeft
'Delete table 9
FR4 = Range("A" & LR3).Offset(2, 0).Row
LR4 = ActiveSheet.Range("A" & FR4).CurrentRegion.Rows.Count + LR3 + 1
Range("A" & FR4 & ":W" & LR4).Delete Shift:=xlToLeft
'Delete Empty Rows
LR5 = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Set Data = ActiveSheet.Range("A1:C" & LR5)
Rows = Data.Rows.Count
For i = Rows To 1 Step (-1)
If WorksheetFunction.CountA(Data.Rows(i)) = 0 Then Data.Rows(i).Delete
Next
'Rename Columns
Range("A1").Formula = "Part Number"
Range("B1").Formula = "IP Qty"
Range("C1").Formula = "IP Value"
End Sub
I have a CSV file that contains lots of tables inside of one worksheet. I was writing parts of the code in one procedure to check if it was doing what I expected for each table. The final result I pasted in one single procedure and now I'm getting a Compile Error right in the beginning of the code, in "Sub IP_VBA()", and I have no idea how to solve it, can someone help me?
If I run it part by part, it does what I want, but not when I put all the code together.
Sub IP_VBA()
' Format IP table
Dim LR1 As Long, LR2 As Long, LR3 As Long, LR4 As Long, LR5 As Long
Dim FR1 As Long, FR2 As Long, FR3 As Long, FR4 As Long
Dim Rows As Long, i As Long
Dim Data As Range
'Delete tables 1, 2 and 3
Rows("1:3").EntireRow.Delete
Rows("1:1").Select
Range(Selection, Selection.End(xlDown).Offset(1, 0)).Delete Shift:=xlUp
Rows("1:2").EntireRow.Delete
Columns("A:I").EntireColumn.Delete
'Get necessary data from table 4
LR1 = ActiveSheet.Range("A1").CurrentRegion.Rows.Count
Range("B1:B" & LR1 & "," & "D1:E" & LR1 & "," _
& "G1:O" & LR1).Delete Shift:=xlToLeft
'Delete table 5
Rows(LR1 + 2).Select
Range(Selection, Selection.End(xlDown).Offset(1, 0)).Delete Shift:=xlUp
'Get necessary data from table 6
FR1 = Range("A" & LR1).Offset(2, 0).Row
Rows(FR1).EntireRow.Delete
LR2 = ActiveSheet.Range("A" & FR1).CurrentRegion.Rows.Count + LR1 + 1
Range("A" & FR1 & ":B" & LR2 & "," & "E" & FR1 & ":J" & LR2 & "," & _
"L" & FR1 & ":T" & LR2).Delete Shift:=xlToLeft
'Get necessary data from tables 7 and 8
FR2 = Range("A" & LR2).Offset(2, 0).Row
Rows(FR2).EntireRow.Delete
LR3 = ActiveSheet.Range("A" & FR2).CurrentRegion.Rows.Count + LR2 + 1
FR3 = Range("A" & LR3).Offset(2, 0).Row
Rows(FR3).EntireRow.Delete
LR3 = ActiveSheet.Range("A" & FR3).CurrentRegion.Rows.Count + LR3 + 1
Range("A" & FR2 & ":D" & LR3 & "," & "G" & FR2 & ":L" & LR3 & "," & _
"N" & FR2 & ":V" & LR3).Delete Shift:=xlToLeft
'Delete table 9
FR4 = Range("A" & LR3).Offset(2, 0).Row
LR4 = ActiveSheet.Range("A" & FR4).CurrentRegion.Rows.Count + LR3 + 1
Range("A" & FR4 & ":W" & LR4).Delete Shift:=xlToLeft
'Delete Empty Rows
LR5 = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Set Data = ActiveSheet.Range("A1:C" & LR5)
Rows = Data.Rows.Count
For i = Rows To 1 Step (-1)
If WorksheetFunction.CountA(Data.Rows(i)) = 0 Then Data.Rows(i).Delete
Next
'Rename Columns
Range("A1").Formula = "Part Number"
Range("B1").Formula = "IP Qty"
Range("C1").Formula = "IP Value"
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
昏暗的行只要长
,然后
rows(“ 1:3”)。eleasterow.delete
我不会使用
rows
作为变量名称,并且还使用特定的工作表预选赛,以适用于行/范围/单元/列的所有呼叫You have
Dim Rows As Long
but then
Rows("1:3").EntireRow.Delete
I would not use
Rows
as a variable name, and also use a specific worksheet qualifier for any and all calls to Rows/Range/Cells/Columns