如何在Excel文档中添加宏?

发布于 2025-02-11 15:39:13 字数 1483 浏览 2 评论 0原文

我最近开始与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风吹 2025-02-18 15:39:13

将宏添加到模块中,然后导出模块。导出模块后,您可以使用此Python代码添加/导入并运行它。

xl = win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Add
wb = xl.ActiveWorkbook
wb.VBProject.VBComponents.Import "Full Path\VBA_Exported_Macro.bin"
xl.Application.Run("Module1.Delphi_macro")
xl.DisplayAlerts = False
wb.DoNotPromptForConvert = True
wb.CheckCompatibility = False
wb.SaveAs('final_outfile.xlsx', FileFormat=51, ConflictResolution=2) # Macro disapears here
xl.Application.Quit()
del xl
xl = None

在上面的代码中,这是创建一个新鲜的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.

xl = win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Add
wb = xl.ActiveWorkbook
wb.VBProject.VBComponents.Import "Full Path\VBA_Exported_Macro.bin"
xl.Application.Run("Module1.Delphi_macro")
xl.DisplayAlerts = False
wb.DoNotPromptForConvert = True
wb.CheckCompatibility = False
wb.SaveAs('final_outfile.xlsx', FileFormat=51, ConflictResolution=2) # Macro disapears here
xl.Application.Quit()
del xl
xl = None

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文