遍历Excel宏

时间:2018-10-18 20:32:37

标签: excel vba

您好,我录制了这个简单的宏,它基本上将excel工作表的副本保存到一个文件夹中。

我要保存不同的工作表,所以我返回代码并将数字从1更改为2 ..etc,我希望它在最后一个数字上加一个并更改要保存的文件名以及然后保存然后返回1,更改文件名等。我该怎么做?

Sub Macro1()
'
' Macro1 Macro

Windows("FORM 16 - 2018 (FINAL)-test jo.xlsx").Activate
ActiveCell.FormulaR1C1 = "8"
Sheets("Sheet1").Select
Sheets("Sheet1").Copy
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:= _
    "\C:\Storedfiles\08-Form 16.xlsx", _
    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

End Sub

2 个答案:

答案 0 :(得分:1)

我只是在脑子里写了这个,但是您可以创建一个从X到Z的循环,然后将变量号与文件名字符串组合在一起。看起来像这样。

<android.support.constraint.ConstraintLayout

答案 1 :(得分:0)

'多么悲伤@pheeper和
'如果您不知道必须复制的文件数量,并且想要继续增加
'第二天的数字,您可以使用两个输入数字说出您要复制多少个文件,并从中复制
'您想注视哪个号码。我希望这段代码对您有帮助...

Dim n1, n2 As Variant   
Dim numberOk As Boolean 

numberOk = False    

'insert the number where do you want to start   
n1 = Application.inputbox("number where do you want to start")  

Do  
    If Not IsNumeric(n1) Then   

        n1 = Application.inputbox("number where do you want to start")  

    Else    

        numberOk = True ' input is a number 
        n1 = CInt(n1) ' convert the number in integer   
    End If  
Loop Until numberOk = True  

numberOk = False    

'insert number where do you want to start   
n2 = Application.inputbox("numbers of copy that you want to do")    

Do  
    If Not IsNumeric(n2) Then   

        n1 = Application.inputbox("number where do you want to start")  

    Else    

        numberOk = True ' input is a number 
        n2 = CInt(n2) ' convert the number in integer   
    End If  
Loop Until numberOk = True  

'change the for in for i= n1 to n2+n1   
'your code...