我有多个excel的基本数据,我需要根据列对数据进行整理,而具有唯一值的排序数据和我放入数组的这些唯一值,基于新工作簿必须具有的唯一值创建。
我的问题:
saveas method workbook class failed
我的代码:
sub marcel()
Dim sArray as string
Dim saArray as string
Dim Lastrow_Sub As Integer
Dim Lastrow_Aging As Integer
Dim Array_Sub As Variant
Dim Array_Sub_Aging As Variant
Dim rngFilter_Ws2 as range
Dim Sht6 as worksheet
Dim ColumnsToRemove2 as variant
Dim j2 as integer
Dim vItem2 as variant
Check_date = Format(Date, "yymm")
Sheets("q_Aging_base_data_incl_FDB").Columns("D:D").AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=.Range("AY1"), Unique:=True
Lastrow_Aging = .Cells(.Rows.Count, "AY").End(xlUp).Row
Array_Sub_Aging = Range("AY2:AY" & Lastrow_Aging)
saArray = Array_Sub_Aging(m, 1)
Sheets("BASE qry_Inventory Activation b").Columns("H:H").AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=.Range("AZ1"), Unique:=True
Lastrow_Sub = .Cells(.Rows.Count, "AZ").End(xlUp).Row
Array_Sub = Range("AZ2:AZ" & Lastrow_Sub)
sArray = Array_Sub(k, 1)
If sArray <> "APE" And saArray = "APE" Or sArray <> "XXUMA" And saArray = "XXUMA" Then
Dim NewBook_Sub_Aging As Workbook
Set NewBook_Sub_Aging = Workbooks.Add
With NewBook_Sub_Aging
.Title = saArray
NewBook_Sub_Aging.Worksheets("sheet1").Name = "Aging Inventory"
With rngFilter_Ws2
.AutoFilter Field:=4, Criteria1:=saArray, Operator:=xlFilterValues
.AutoFilter Field:=15, Criteria1:="reporting relevant Location", Operator:=xlFilterValues
.AutoFilter Field:=32, Criteria1:="<>(a) 0 - 360", Operator:=xlFilterValues
Set rngCopyAging_Sub = .SpecialCells(xlCellTypeVisible)
.AutoFilter ' Switch off AutoFilter
End With
rngCopyAging_Sub.Copy Destination:=NewBook_Sub_Aging.Worksheets("Aging Inventory").Cells(1, 1)
' Delete unwanted columns for subregions(APE and XXUMA) Aging
Set Sht6 = NewBook_Sub_Aging.Worksheets("Aging Inventory")
ColumnsToRemove2 = Array("Period", "AP_BU", "Subregion", "Strategic_BU", "Company_Code", "Company_name", "Plant_name", _
"Rep Location", "Storage_Location", "Storage_Location_name", "Date_last_goods_rec", "Stock_type", _
"Stock_type_name", "Kind_of_Material_name", "Supplier_name", "SummevonVEU_OIV1", "Days_since_production", _
"Remaining_shelf_life", "APO_Planner", "S_SCM_or_SVC")
For j2 = LBound(ColumnsToRemove2) To UBound(ColumnsToRemove2) Step 1
vItem6 = Application.Match(ColumnsToRemove2(j2), Sht6.Rows(1), 0)
Debug.Print vItem6
If IsNumeric(vItem6) Then
Sht6.Columns(vItem6).Delete
End If
Next j2
NewBook_Sub_Aging.Worksheets("Aging Inventory").Cells.EntireColumn.AutoFit
NewBook_Sub_Aging.Worksheets("Aging Inventory").Range("A1:P1").AutoFilter
NewBook_Sub_Aging.Worksheets("Aging Inventory").Range("A2:P2").Select
ActiveWindow.FreezePanes = True
.SaveAs Filename:="KPI" & " " & saArray & " " & Check_date & ".xlsx"
Application.DisplayAlerts = False
NewBook_Sub_Aging.Close
End With
End If
end sub
任何人都可以帮我解决这个问题。
答案 0 :(得分:0)
sArray和saArray似乎都没有被定义为实际数组。数组的定义如下:
Dim myArray() as Integer
分配给任何数组位置的任何值都需要以这种方式放置:
myArray(i) = 815 'i being a position, like 0 or 5
当您想要将值保持在某个位置时,您也会引用该位置。所以,如果我想在may数组的第4个位置输入值,我就
myArray(3) 'Arrays are zero-index based.
所以当你这样做时:
.SaveAs Filename:="KPI" & " " & saArray & " " & Check_date & ".xlsx"
您为Filename参数提供了一个可能不包含任何内容的变量变量,因此该行会引发错误。
我假设saArray和Check_date是来自不同Sub的全局变量,并且创建了数组并在那里分配了它的值;它仍然会抛出一个错误,因为你给SaveAs模块一个数组,而不是一个字符串。你必须给它一个数组中的值(它必须是一个字符串,因为文件名必须是一个字符串)。