我有一个代码,它可以帮助我在单元格中插入一些关键字,并设置一些打印参数。每天我都有许多新文件,我必须这样做。 我在excel面板上用宏创建了一个按钮,但是当我点击它时,打开一个文件,我在第一次写宏。如果没有打开旧文件,我该怎么做呢?
以下是代码:
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
答案 0 :(得分:1)
在编写宏的工作簿中,试试这个: 单击左上角的Windows按钮。选择Excel选项。单击左侧的“自定义”。在左上角的下拉列表中,选择&#34;宏&#34;。单击所需的宏,然后将其移到右侧。确保右上角的下拉列表显示&#34; For All Documents&#34;。这应该会给你一个按钮,它将在任何excel文档上运行宏。