Excel 每次重复宏并以不同的文件名保存

Excel 每次重复宏并以不同的文件名保存,excel,vba,Excel,Vba,我有下面的代码来自动执行某个操作并保存文件。但是我需要重复这个操作,每次都用不同的文件名保存它。具体为3次 我不知道最好的方法是什么。请帮忙 Sub Proof() Dim i As Long, Di As Long, Bi As Long Const Dfirstrow As Double = 222 Const Bfirstrow As Double = 222 Dim Dlastrow As Long, Blastrow As Long Dim Dmyvalue As Double, Bm

我有下面的代码来自动执行某个操作并保存文件。但是我需要重复这个操作,每次都用不同的文件名保存它。具体为3次

我不知道最好的方法是什么。请帮忙

Sub Proof()
Dim i As Long, Di As Long, Bi As Long
Const Dfirstrow As Double = 222
Const Bfirstrow As Double = 222
Dim Dlastrow As Long, Blastrow As Long
Dim Dmyvalue As Double, Bmyvalue As Double
Range("A2").Value = "Date / Time: " & Format(Now, "dd.mm.yyyy / hh:nn:ss")
Dlastrow = Range("BE" & Dfirstrow).End(xlDown).Row
For Di = Dfirstrow To Dlastrow
Dmyvalue = Range("BE" & Di).Value
If Dmyvalue < 100 Then Range("BE" & Di).Value = -9 + Rnd * -3
Next Di
Blastrow = Range("AD" & Bfirstrow).End(xlDown).Row
For Bi = Bfirstrow To Blastrow
Bmyvalue = Range("AD" & Bi).Value
If Bmyvalue < 100 Then Range("AD" & Bi).Value = 46 + Rnd * 3
Next Bi
Sheets.Select
Cells.Copy
Cells.PasteSpecial xlPasteValues
Application.DisplayAlerts = False
ThisWorkbook.SaveAs "C:\Users\sgfancyj\Desktop\Profile_Macros\NEW\A", FileFormat:=Excel.xlOpenXMLWorkbook
Application.DisplayAlerts = True
End Sub
Sub-Proof()
暗我如长,迪如长,碧如长
常数Dfirstrow为双精度=222
常数Bfirstrow为双精度=222
昏暗的街道和长的一样,黑色的街道和长的一样
Dim Dmyvalue为双精度,Bmyvalue为双精度
范围(“A2”).Value=“日期/时间:”&格式(现在为“dd.mm.yyyy/hh:nn:ss”)
Dlastrow=范围(“BE”和Dfirstrow).End(xlDown).Row
对于Di=Dfirstrow到Dlastrow
Dmyvalue=范围(“BE”&Di).值
如果Dmyvalue<100,则范围(“BE”&Di)。值=-9+Rnd*-3
下一个Di
Blastrow=范围(“AD”&Bfirstrow).End(xlDown).Row
对于Bi=B第一次到Blastrow
Bmyvalue=范围(“AD”和Bi).值
如果BMY值<100,则范围(“AD”和Bi)。值=46+Rnd*3
下一个Bi
工作表。选择
细胞,复制
Cells.paste特殊XLPaste值
Application.DisplayAlerts=False
ThisWorkbook.SaveAs“C:\Users\sgfancyj\Desktop\Profile\u Macros\NEW\A”,文件格式:=Excel.xlOpenXMLWorkbook
Application.DisplayAlerts=True
端接头

我为每次保存添加了一个循环并更改了文件名,只需自定义文件名部分,代码就可以了,如果您想了解其他信息,请在我的频道上发表评论

Sub-Proof()
暗x等长
暗我如长,迪如长,碧如长
对于x=1到3',无论您希望运行多少次
常数Dfirstrow为双精度=222
常数Bfirstrow为双精度=222
昏暗的街道和长的一样,黑色的街道和长的一样
Dim Dmyvalue为双精度,Bmyvalue为双精度
范围(“A2”).Value=“日期/时间:”&格式(现在为“dd.mm.yyyy/hh:nn:ss”)
Dlastrow=范围(“BE”和Dfirstrow).End(xlDown).Row
对于Di=Dfirstrow到Dlastrow
Dmyvalue=范围(“BE”&Di).值
如果Dmyvalue<100,则范围(“BE”&Di)。值=-9+Rnd*-3
下一个Di
Blastrow=范围(“AD”&Bfirstrow).End(xlDown).Row
对于Bi=B第一次到Blastrow
Bmyvalue=范围(“AD”和Bi).值
如果BMY值<100,则范围(“AD”和Bi)。值=46+Rnd*3
下一个Bi
工作表。选择
细胞,复制
Cells.paste特殊XLPaste值
Application.DisplayAlerts=False
ActiveWorkbook.SaveAs文件名:=St&&&Ex_Ref&&Ref&&Ref&'每次文件都会有不同的名称
EnFileFormat:=Excel.xlOpenXMLWorkbook
Application.DisplayAlerts=True
Debug.Print x&“.file saved.”这会在即时窗口中显示此消息
下一个x
端接头

只需添加一个从1到3的循环即可。或者您可以重复SaveAs行。我想在每次保存之前运行上面的所有代码。如果我使用循环重复saveas行,它将只运行代码一次,并保存代码3次,对吗?然后将其全部放入循环中。谢谢您的回复。我尝试更改文件名部分,但不起作用。不知道我做错了什么。该代码是否可以在excel 97-2003中工作?它可能无法在旧版本中工作。您是否在vba引用中打开了internet控件?“不工作”不是有用的反馈,请详细说明。
Sub Proof()



    Dim x as long
    Dim i As Long, Di As Long, Bi As Long



    For x = 1 to 3 ‘however many times you would want to run it 

    Const Dfirstrow As Double = 222
    Const Bfirstrow As Double = 222
    Dim Dlastrow As Long, Blastrow As Long
    Dim Dmyvalue As Double, Bmyvalue As Double
    Range("A2").Value = "Date / Time: " & Format(Now, "dd.mm.yyyy / hh:nn:ss")
    Dlastrow = Range("BE" & Dfirstrow).End(xlDown).Row
    For Di = Dfirstrow To Dlastrow
    Dmyvalue = Range("BE" & Di).Value
    If Dmyvalue < 100 Then Range("BE" & Di).Value = -9 + Rnd * -3
    Next Di
    Blastrow = Range("AD" & Bfirstrow).End(xlDown).Row
    For Bi = Bfirstrow To Blastrow
    Bmyvalue = Range("AD" & Bi).Value
    If Bmyvalue < 100 Then Range("AD" & Bi).Value = 46 + Rnd * 3
    Next Bi
    Sheets.Select
    Cells.Copy
    Cells.PasteSpecial xlPasteValues
    Application.DisplayAlerts = False
      ActiveWorkbook.SaveAs Filename:=St & " " & " " & Ex_Ref & " " & Ref & ‘the file will have say save as different names each time           
    EnFileFormat:=Excel.xlOpenXMLWorkbook
    Application.DisplayAlerts = True

      Debug.Print x & ".file saved."    ‘this displays this message in immediate window 
      Next x




    End Sub