2

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

我怎样才能使它适用于我的所有文件?具体来说,如何更改文件名以保存正在打开的文件?

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

4 回答 4

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 工作表或工作簿,或“D: \韦恩CSV"

编辑:

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

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

于 2013-04-09T20:02:07.147 回答
0

写在哪里

文件名:="D:\WayneCSV\cwapp5_MemCPU-Date-Mem.xlsx"

那是您的文件名,您可以将其替换为 ThisWorkbook.FullName 它应该可以工作

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

Filename:=ThisWorkbook.FullName,

我也不明白那部分

ChDir "D:\WayneCSV"

为什么要放路径然后是全名

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

于 2013-04-09T20:00:22.607 回答
0

看看这个 SO 链接

我认为你想要做的是:="D:\WayneCSV\cwapp5_MemCPU-Date-Mem.xlsx"ActiveWorkbook.FullNamein ActiveWorkbook.SaveAsline替换

于 2013-04-09T20:02:43.907 回答
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

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

于 2013-04-09T20:26:51.487 回答