VBA:如何修改VBAmacros,使其为每个文件工作? 请看下面
所以我有一个macros适用于一个特定的文件。 我如何使它适用于我的所有文件? 我从来没有使用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
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
将YourFileNameHerereplace为您要用来保存文件的名称。
或者,如果您想简单地保存活动工作簿中的更改
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
让我知道它是否工作,因为我不能testing没有您的文件夹和数据。 但它应该工作。
看看这个链接
我想你想要做的是replace:="D:\WayneCSV\cwapp5_MemCPU-Date-Mem.xlsx"
与ActiveWorkbook.FullName
ActiveWorkbook.SaveAs
线
在哪里写的
文件名:= “d:\ WayneCSV \ cwapp5_MemCPU最新-Mem.xlsx”
这是你的文件名,你可以用ThisWorkbook.FullNamereplace它应该工作
所以它应该给你这样的东西。
Filename:=ThisWorkbook.FullName,
另外我不明白这一部分
ChDir "D:\WayneCSV"
为什么把path,然后全名
我更像一个访问程序员而不是Excel,所以我可能是错的。
根据我所知,您的macros是根据某些数据添加图表并保存工作表。
这是一个非常专业化的macros,但是您需要将列表中的其他目录更改为本地驱动器上的目录,但是还需要找出正在创build图表的数据的存储位置。
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重命名为您希望将其保存为的位置以及位置。