简体   繁体   中英

Error ` saveas method workbook failed` in vba Excel

I have basic data with multiple excels which i need to sorted out the data based on the columns whereas the sorted data which has a unique values and these uniques values i put in array ,based on unique values a new workbook has to be create.

My problem:

  1. When im excuting the macro sometimes its showing the error as saveas method workbook class failed
  2. How can i freeze a column?

My code:

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

can anyone please help me out how to resolve this issue.

Both sArray and saArray don't seem to be defined as an actual array. Arrays are defined like this:

Dim myArray() as Integer

Any value assigned to any of the array positions need to be put in this way:

myArray(i) = 815 'i being a position, like 0 or 5

When you want to get the value held in a certain position, you would also reference that position. So, if I wanted the value in the 4th position of may array, I'd do

myArray(3) 'Arrays are zero-index based.

so when you do:

.SaveAs Filename:="KPI" & " " & saArray & " " & Check_date & ".xlsx"

You're giving the Filename parameter a variant variable that may contain nothing, so the line would throw an error.

I'll assume saArray and Check_date are global variables coming from a different Sub and the array is created and its values assigned over there; it would still throw an error, since you're giving the SaveAs module an array, and not a string. You HAVE to give it one of the values IN the array (And it has to be a string, since Filename has to be a string).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM