VBA - 保存工作簿后对工作簿2的操作1

时间:2016-05-02 12:34:02

标签: excel vba excel-vba

所以,这是我的问题: 我在VBA中有一个宏删除了我的工作表中的所有隐藏列。它运作正常。

Sub Test()
Dim F As Integer, C As Integer
For F = 1 To Sheets.Count
    ActiveSheet.Select
    For C = 15 To 2 Step -1
        ActiveSheet.Columns(C).Select
        Selection.End(xlDown).Select
        derniereligne = ActiveCell.Row
        If ActiveSheet.Columns(C).Hidden = True Then
            ActiveSheet.Columns(C).Delete
        End If
    Next C
Next F
End Sub

但是现在,我的项目进化了,我必须将我的工作簿保存在副本中,所以我这样做了:

Sub SaveXL()

Dim Nom2 As String
Dim Jour2 As String
Dim FPath2 As String
Jour2 = Format(Now(), "yyyymmdd - h\hmm'")
Nom2 = Jour2 & " Pricelist"
FPath2 = Sheets("PARAM").Range("B33").Value
On Error GoTo fin4
fichier = Application.GetSaveAsFilename(FPath2 & Nom2, "Fichiers Excel (*.xls), *.xls")
ActiveWorkbook.SaveCopyAs fichier
Exit Sub
fin4:MsgBox "La création de l'excel a échoué"
End Sub

它创建了我的副本,好的。但是当我使用第二个宏(SaveXL)保存时,我想在我的第一个工作簿(工作簿2)的副本上使用第一个宏(Test)。

有可能吗? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

  

在SaveXL中调用Test并传递复制的工作簿的名称:

fichier = Application.GetSaveAsFilename(FPath2 & Nom2, "Fichiers Excel (*.xls), *.xls")
ActiveWorkbook.SaveCopyAs fichier
Test Nom2
  

在测试中检查目标工作簿是否已打开。如果未打开则打开,运行宏并使用等于True的保存更改将其关闭:

Sub Test(targetWorkbookName As String)
    Dim F As Integer, C As Integer, derniereligne

    Dim targetWorkbook As Workbook
    On Error Resume Next
    Set targetWorkbook = Workbooks(targetWorkbookName)
    On Error GoTo 0
    If (targetWorkbook Is Nothing) Then _
        Set targetWorkbook = Workbooks.Open(targetWorkbookName)

    For F = 1 To Sheets.Count
        ActiveSheet.Select
        For C = 15 To 2 Step -1
            ActiveSheet.Columns(C).Select
            Selection.End(xlDown).Select
            derniereligne = ActiveCell.Row
            If ActiveSheet.Columns(C).Hidden = True Then
                ActiveSheet.Columns(C).Delete
            End If
        Next C
    Next F

    targetWorkbook.Close savechanges:=True
End Sub

希望我理解正确。 HTH

答案 1 :(得分:0)

您可以将工作簿作为参数传递给子Test()。因此,在保存工作簿之后,在新工作簿中运行Test子。 查看我在Test()代码中所做的修改:

'Here I insert a parameter to your sub
Sub Test(myWorkBook as Workbook)
    Dim F As Integer, C As Integer
    For F = 1 To myWorkBook.worksheets.Count
        'ActiveSheet.Select
        For C = 15 To 2 Step -1
            myWorkBook.worksheets(f).Columns(C).Select
            Selection.End(xlDown).Select
            derniereligne = ActiveCell.Row
            If myWorkBook.worksheets(f).Columns(C).Hidden = True Then
                myWorkBook.worksheets(f).Columns(C).Delete
            End If
        Next C
    Next F
End Sub

现在你的sub saveXL()

Sub SaveXL()
    Dim Nom2 As String
    Dim Jour2 As String
    Dim FPath2 As String
    Jour2 = Format(Now(), "yyyymmdd - h\hmm'")
    Nom2 = Jour2 & " Pricelist"
    FPath2 = Sheets("PARAM").Range("B33").Value
    On Error GoTo fin4
    fichier = Application.GetSaveAsFilename(FPath2 & Nom2, "Fichiers Excel (*.xls), *.xls")
    ActiveWorkbook.SaveCopyAs fichier
    call test(ActiveWorkbook) '<-- here you use your Test() sub
    Exit Sub
    fin4:MsgBox "La création de l'excel a échoué"
End Sub

P.S。:对不起我的英文