让macrosbutton与任何Excel文件中工作,而没有打开文件,我写了macros

我有一个代码,它可以帮助我用一些关键字在单元格中插入图像,并设置一些打印参数。 我每天都有很多新的文件,我必须做。 我在excel面板上用macros做了一个button,但是当我点击它的时候打开了一个文件,在那里我第一次写macros。 我应该怎么做,让它没有打开旧文件的工作?

这里是代码:

Sub Macro2() With ActiveSheet.PageSetup .Zoom = False .FitToPagesWide = 1 End With Set ff = ActiveSheet.Cells.Find("forpicture") MyTop = [ff].Top MyLeft = [ff].Left InsertPicture "C:\Users\Public\222222.png", _ Range(ff.Address), True, True Dim f As Range With ActiveSheet.PageSetup .LeftMargin = Application.InchesToPoints(0.5) .RightMargin = Application.InchesToPoints(0.2) .TopMargin = Application.InchesToPoints(0.5) .BottomMargin = Application.InchesToPoints(0.2) End With End Sub Sub InsertPicture(PictureFileName As String, TargetCell As Range, _ CenterH As Boolean, CenterV As Boolean) ' inserts a picture at the top left position of TargetCell ' the picture can be centered horizontally and/or vertically Dim p As Object, t As Double, l As Double, w As Double, h As Double If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub If Dir(PictureFileName) = "" Then Exit Sub ' import picture Set p = ActiveSheet.Pictures.Insert(PictureFileName) ' determine positions With TargetCell t = .Top l = .Left End With ' position picture With p .Top = t .Left = l .Height = 150 End With Set p = Nothing End Sub 

在您编写macros的工作簿中,请尝试以下操作:单击左上angular的Windowsbutton。 selectExcel选项。 点击左侧的自定义。 在左上angular的下拉列表中,select“macros”。 点击你想要的macros,然后将其移到右侧。 确保右上angular的下拉菜单中显示“For All Documents”。 这应该给你一个button,将在任何Excel文档上运行macros。