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

时间:2020-03-16 08:53:06

标签: 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, 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

1 个答案:

答案 0 :(得分:0)

ive为每次保存添加了一个循环并更改了文件名,只需自定义文件名部分,代码就可以了,如果您想了解其他https://www.youtube.com/watch?v=hfAhmae4iqA,请在我的频道上发表评论{ / p>

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
相关问题