VBA Code throwing Compile Error: Expected Array
VBA Code throwing Compile Error: Expected Array
我有一个 CSV 文件,在一个工作表中包含很多 table。我在一个过程中编写了部分代码,以检查它是否按照我对每个 table 的预期进行。我在一个过程中粘贴的最终结果现在我在代码的开头,在“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
你有
Dim Rows As Long
然后
Rows("1:3").EntireRow.Delete
我不会使用 Rows
作为变量名,并且还会对任何和所有对 Rows/Range/Cells/Columns
的调用使用特定的工作表限定符
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Rows("1:3").EntireRow.Delete
'etc
我有一个 CSV 文件,在一个工作表中包含很多 table。我在一个过程中编写了部分代码,以检查它是否按照我对每个 table 的预期进行。我在一个过程中粘贴的最终结果现在我在代码的开头,在“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
你有
Dim Rows As Long
然后
Rows("1:3").EntireRow.Delete
我不会使用 Rows
作为变量名,并且还会对任何和所有对 Rows/Range/Cells/Columns
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Rows("1:3").EntireRow.Delete
'etc