如何保存正在打开的文件?

时间:2013-04-09 19:54:25

标签: excel vba

我有一个适用于某个特定文件的宏。

如何让它适用于我的所有文件?具体来说,我如何更改文件的名称,以便保存正在打开的文件?

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+m
'
    Range("A:A,B:B").Select
    Range("B1").Activate
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLineStacked
    ActiveChart.SetSourceData Source:=Range( _
        "'cwapp5_MemCPU-Date-Mem'!$A:$A,'cwapp5_MemCPU-Date-Mem'!$B:$B")
    ChDir "D:\WayneCSV"
    ActiveWorkbook.SaveAs Filename:="D:\WayneCSV\cwapp5_MemCPU-Date-Mem.xlsx", _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub

4 个答案:

答案 0 :(得分:3)

With ActiveSheet.Shapes.AddChart
    .ChartType = xlLineStacked
    .SetSourceData Source:=Range( _
    "'cwapp5_MemCPU-Date-Mem'!$A:$A,'cwapp5_MemCPU-Date-Mem'!$B:$B")
End With

ChDir "D:\WayneCSV"
ActiveWorkbook.SaveAs Filename:="D:\WayneCSV\" & *YourFileNameHere* &".xlsx", _
    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

YourFileNameHere 替换为您要保存文件的名称。

或者,如果您只想使用更改

保存活动工作簿
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.FullName, _
    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

如果你想循环遍历“D:\ WayneCSV”中所有可能的工作簿或文件,请告诉我你所谓的make it work for all my files?天气是什么意思,这意味着打开Excel工作表或工作簿,或者扩展名为* .xlsx里面的“D:\ WayneCSV”

修改

Dim StrFile As String

StrFile = Dir("D:\WayneCSV\*.CSV") ' Looks up each file with CSV extension

Do While Len(StrFile) > 0 ' While the file name is greater then nothing
     Workbooks.Open Filename:= "D:\WayneCSV\" & StrFile ' Open current workbook

 ActiveSheet.Shapes.AddChart.Select ' Add a chart
 ActiveChart.ChartType = xlLineStacked ' Add a chart type
 ActiveChart.SetSourceData Source:=Range("$A1:$B1", Range("$A1:$B1").End(xlDown)) ' Set the source range to be the used cells in A:B on the open worksheet 
 With ActiveChart.Parent
     .Height = .Height*1.5 'Increase Height by 50% 
     .Width = .Width*1.5   'Increase Width by 50%
 End With 

'Note the setting of the source will only work while there are no skipped blank if you 
'have empty rows in the source data please tell me and i can provide you with another
' way to get the information 


ActiveWorkbook.SaveAs Filename:="D:\WayneCSV\" & StrFile & ".xlsx", _
    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False  ' Save file as excel xlsx with current files name 

ActiveWorkbook.Close ' Close when finished before opening next file this can be removed if you'd like to keep all open for review at the end of loop.

StrFile = Dir ' Next File in Dir
Loop

让我知道它是否有效,因为我没有您的文件夹和数据无法测试。但它应该工作。

答案 1 :(得分:0)

写的地方

  

文件名:= “d:\ WayneCSV \ cwapp5_MemCPU - 日期 - Mem.xlsx”

这是你的文件名,你可以使用ThisWorkbook替换它。它应该可以工作。

所以它应该给你这样的东西。

Filename:=ThisWorkbook.FullName,

我也不明白那部分

ChDir "D:\WayneCSV"

为什么将路径设为全名

我更像是一个访问程序员,而不是一个优秀的程序员,所以我可能错了。

答案 2 :(得分:0)

查看this SO link

我认为您要做的是替换:="D:\WayneCSV\cwapp5_MemCPU-Date-Mem.xlsx"ActiveWorkbook.FullName

中使用ActiveWorkbook.SaveAs

答案 3 :(得分:0)

根据我的判断,你的宏是根据一些数据添加图表并保存表格。

这是一个非常专业的宏,但您需要将目录列表更改为本地驱动器上的目录,但您还需要找到存储创建图表的数据的位置。

Sub Macro1()
' ' Macro1 Macro ' ' Keyboard Shortcut: Ctrl+m '  '<<< This is the naming convention and hotkey if you goto Tools > Macros in Excel 2003 or Developer > Macros in 2010

'Range("A:A,B:B").Select      '<<< This is where you are selecting all data in columns A and B and is not needed so I commented it out
'Range("B1").Activate   '<<< This code is 'activating' a cell, but not sure why, so I commented it out as it should not be needed
ActiveSheet.Shapes.AddChart.Select    '<<< You are adding a chart here
ActiveChart.ChartType = xlLineStacked    '<<<Defining a chart type
ActiveChart.SetSourceData Source:=Range( _ "'cwapp5_MemCPU-Date-Mem'!$A:$A,'cwapp5_MemCPU-Date-Mem'!$B:$B") '<<< Setting it's source data from a worksheet called 'cwapp5_MemCPU-Date-Mem' with header information from Column B and Data from Column A
ChDir "D:\WayneCSV" 
ActiveWorkbook.SaveAs Filename:="D:\WayneCSV\cwapp5_MemCPU-Date-Mem.xlsx", _         
    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False   '<<< You are saving a copy of your workbook
End Sub

要使其适用于其他工作簿,您需要将所有范围重命名为数据所在的位置,将选项卡重命名为您的选项卡命名,并将WorkBook重命名为您希望保存的位置和位置。 / p>