2016-04-18 154 views
0

我有多個excel的基本數據,我需要根據列來整理數據,而排序後的數據具有唯一值和這些唯一值,我把它們放在數組中,基於獨特的價值觀,必須創建一個新的工作簿。錯誤saveas方法工作簿失敗在Excel中

我的問題:

  1. 當IM excuting宏觀有時它的顯示誤差 saveas method workbook class failed
  2. 我如何凍結列?

我的代碼:

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

應該有'sArray'和'saArray'變量嗎?這一行是正確的:'sArray <>「APE」和saArray =「APE」或者sArray <>「XXUMA」並且saArray =「XXUMA」'或者它應該是'(sArray <>「APE」and saArray =「APE」 )或(sArray <>「XXUMA」和saArray =「XXUMA)」'? 'saArray'和'Check_date'的價值是什麼? 'saArray'是您在文件名中使用的值的數組嗎? –

+0

我假設名字'check_date'是一個日期,可能包含正斜槓。文件名不能包含以下字符:\ /:*? 「| – LiamH

+0

@ DarrenBartrup-Cook sArray and saArray contains string variables.ex APE,APED,APEA etc'sArray <>」APE「and saArray =」APE「or sArray <>」XXUMA「and saArray =」XXUMA「''是正確的行。「Check_date」包含當前月份。saArray不是它的字符串。文件名必須像這樣創建'KPI APE 1604.xlsx' –

回答

0

sArray和saArray都沒有被定義爲實際的數組。數組是這樣定義的:

Dim myArray() as Integer 

分配給任何陣列位置需要放置在這樣的任意值:

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

當你想在某個位置上的價值,你也會參考那個位置。所以,如果我想在5陣列的四號位的價值,我會做

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

所以當你:

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

你給的文件名參數Variant變量可能不包含任何內容,因此該行會引發錯誤。

我會假設saArray和Check_date是來自不同Sub的全局變量,並且該數組已創建且其值在那裏分配;它仍然會拋出一個錯誤,因爲您將SaveAs模塊作爲一個數組,而不是一個字符串。你必須給它一個數組中的值(它必須是一個字符串,因爲Filename必須是一個字符串)。

+0

sArray和saArray包含字符串variables.ex'APE,APED,APEA'等。 Check_date'包含當前月份。 saArray不是它的一個字符串的值。文件名必須像這樣創建'KPI APE 1604.xlsx' –

相關問題