在VBA循环中保存excel文件

时间:2015-10-30 16:38:05

标签: excel-vba loops save vba excel

我创建了一个循环遍历国家/地区列表的宏,并在“Dash_eng”上更新了这个数字。我可以让循环工作但是循环的一部分调用另一个宏来复制并保存“Dash_Eng”)作为excel文件

这两个宏在彼此独立运行时只会将它们组合在一起

宏循环通过国家/地区并在excel宏中调用保存

Sub ReportUpdate()    
Dim i As Integer, numrows As Integer
    Sheets("Text").Select        
    numrows = Range("O2", Range("O2").End(xlDown)).Rows.Count

     Range("O1").Select
     i = 1
     Do While i <= numrows
         ActiveCell.Offset(rowOffset:=1, columnoffset:=4) = "X"
         ActiveCell.Offset(1, 0).Select
         Range("M1") = ActiveCell
         Call Send_to_PDF
         i = i + 1
    Loop
    MsgBox "Dear Sir / Madam your base data has been refreshed and all other relivant formatting macros have been run"
End Sub

保存宏

Sub Send_to_PDF()

Dim Ref As String

Dim St As String
Dim En As String
Dim Ex_Ref As String

FilePath = "P:\Hyundai\Hyundai GLOBAL\Country folders\EUROPE\A3 Ops reports"

Sheets("Dashboard - ENG").Select
Sheets("Dashboard - ENG").Copy


Cells.Select
Range("C1").Activate
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False

Ex_Ref = Sheets("Dashboard - Eng").Range("L1")
St = FilePath + "\Dashboard - ENG"
Ref = Format(DateAdd("m", -1, Now()), "yyyymm")
En = ".xlsx"


  ChDir FilePath
    ActiveWorkbook.SaveAs Filename:=St & " " & " " & Ex_Ref & " " & Ref & En

  ActiveWorkbook.Theme.ThemeColorScheme.Load ( _
        "C:\Program Files (x86)\Microsoft Office\Document Themes 15\Theme Colors\Office 2007 - 2010.xml" _
        )


  ActiveWindow.Close

PS:我知道它没有发送到PDF

任何想法?

2 个答案:

答案 0 :(得分:0)

你可以只是PDF表格,你不必复制它。

例如

// From SO solution: http://stackoverflow.com/a/901144/715597
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var _lang = getParameterByName('lang');
document.write('<script src="https://maps.googleapis.com/maps/api/js?language='+_lang+'">\x3C/script>');

答案 1 :(得分:0)

非常感谢我帮助解决了这个问题。

不是使用保存宏而是使用循环宏

Sub ReportUpdate()

Dim i As Integer,numrows As Integer

    **Sheets("Text").Select (**

    numrows = Range("O2", Range("O2").End(xlDown)).Rows.Count

    Range("O1").Select

    i = 1

     Do While i <= numrows

     Sheets("text").Select
     ActiveCell.Offset(rowOffset:=1, columnoffset:=4) = "X"
     ActiveCell.Offset(1, 0).Select
     Range("M1") = ActiveCell

              Save_2_Excel

    i = i + 1

    Loop


    MsgBox "Dear Sir / Madam your base data has been refreshed and all other relivant formatting macros have been run"

End Sub

我添加的所有行都是粗体,因为片材raw中的活动单元需要在循环开始之前重新激活

PS:如果你要复制它以循环通过在excel中保存文件添加到excel保存宏

ActiveWindow.Close False

这将关闭新工作表而不保存很酷,因为这是最后一条指令并且工作表已经保存