代码之家  ›  专栏  ›  技术社区  ›  Doug Fresh

将Excel Book中的一个工作表中的单元格值另存为多个工作表

  •  1
  • Doug Fresh  · 技术社区  · 7 年前

    Sub RecFilter()
    ActiveSheet.Range("$A$2:$H$159").AutoFilter Field:=7, Criteria1:=Array( _
        "(1)", "(112)", "(113)", "(126)", "(14)", "(144)", "(216)", "(3,274)", "(448)", "(468)", _
        "(5)", "(65)", "(72)", "(80)", "(900)", "(960)", "106", "14", "2", "2,880", "3,420", "504" _
        , "513", "56", "665", "72", "845", "9,814", "900"), Operator:=xlFilterValues
    Cells.Select
    Selection.Copy
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Paste
    ActiveSheet.Columns("A:H").EntireColumn.AutoFit
    ActiveSheet.Rows("1:2").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
    ActiveSheet.Range("J1").Select
    ActiveCell.FormulaR1C1 = "Start Date"
    ActiveSheet.Range("J2").Select
    ActiveCell.FormulaR1C1 = "End Date"
    ActiveSheet.Range("K1").Select
    ActiveCell.FormulaR1C1 = "1/1/2000"
    ActiveSheet.Range("K2").Select
    ActiveCell.FormulaR1C1 = "1/7/2000"
    ActiveSheet.Columns("K:K").Select
    Selection.NumberFormat = "m/d/yyyy"
    
    Application.DisplayAlerts = False
    Dim Path As String
    Dim filename As String
    Path = "C:\Users\TEST\Desktop\"
    
    'THIS IS WHERE IS FAILS
    filename =  ActiveSheet.Range("K2")
    
    ActiveWorkbook.SaveAs filename:=Path & filename & ".xlsx", 
    FileFormat:=xlOpenXMLWorkbook
    End Sub
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   BoogieMan2718    7 年前

    filename = ActiveSheet.Range("K2").Value ActiveWorkbook.SaveAs filename: 哪里 Range("K2").Value 包含不能包含在保存的文件名中的正斜杠字符。 ActiveSheet.Range("K2").Select ActiveCell.FormulaR1C1 = "1/7/2000"

    C:\Users\TEST\Desktop\1/7/2000.xlsx

    尝试更改 范围(“K2”)。值 到带连字符的日期数据类型。

        2
  •  1
  •   Azaria    7 年前

    K2包含对文件名无效的字符。

    你可以在K2中替换它,但这会破坏它作为日期的有用性。

    更好的选择是只替换文件名。

    Filename = Replace(ActiveSheet.Range("K2"), "/", "_")
    
        3
  •  0
  •   ERT    7 年前

    更改为:

    filename =  ActiveSheet.Cells(2, 11).Value