如何在Excel文档中添加宏?
我最近开始与Macros和VBA合作工作,但是我的知识目前非常有限,所以请忍受我。我最近写了一个宏,以将简单的标准化格式应用于我发送给他人的所有Excel文档。当我从Excel内部应用宏时,宏一直在工作,但是我还使用Python发送了一些自动报告,并且我想在这些报告上使用此宏来格式化它们。
我的问题本质上是,如何将宏添加到现有的Excel文档中,然后如何从Python中执行该宏,然后保存所得的Excel文档?
我在使用VBA代码或使用Python进行互动方面没有太多经验,因此我不太确定从哪里开始,因此对任何指导都将不胜感激。
以下是我想要使用的宏
Sub Delphi_macro()
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Cells.Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveSheet.ListObjects.Add(xlSrcRange, Range("a1").CurrentRegion, , xlYes).Name = _
"Table1"
Range("Table1[#All]").Select
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight9"
ActiveWindow.DisplayGridlines = False
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
ActiveWindow.SmallScroll Down:=0
Cells.Select
Cells.EntireColumn.AutoFit
End Sub
I recently began working with macros and VBA for work however my knowledge is currently very limited so please bear with me. I recently wrote a macro to apply a simple standardized format to all excel documents I send out to others. The macro has been working when I apply the macro from inside of excel, however I also have some automated reports I send out using python and I would like to use this macro on these reports as well to format them prior to sending.
My question is essentially, how do I add a macro to an existing excel document, how do I then execute that macro from python, and then save the resulting excel document?
I do not have much experience in using VBA code or interacting with this using python so I am not too sure where to start so any guidance would be much appreciated.
Below is the Macro I am looking to use
Sub Delphi_macro()
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Cells.Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveSheet.ListObjects.Add(xlSrcRange, Range("a1").CurrentRegion, , xlYes).Name = _
"Table1"
Range("Table1[#All]").Select
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight9"
ActiveWindow.DisplayGridlines = False
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
ActiveWindow.SmallScroll Down:=0
Cells.Select
Cells.EntireColumn.AutoFit
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将宏添加到模块中,然后导出模块。导出模块后,您可以使用此Python代码添加/导入并运行它。
在上面的代码中,这是创建一个新鲜的Excel工作簿,添加宏,运行它,然后在没有宏的情况下保存文件。您可以更改它,而是打开现有的工作簿,并保存为Macro启用Excel以保留宏。
Add your Macro to a Module, and then Export the Module. After exporting the module you can use this python code to add it/import it and run it.
In the code above this is creating a fresh Excel Workbook, adding the macro, running it and then Saves the file WITHOUT the macro. You can change that and instead open an existing workbook and save as Macro enabled excel to retain the macro.