使用VBA从主工作簿创建副本

时间:2016-11-08 16:25:40

标签: excel vba excel-vba macros

我有两个文件,一个是我要在其中运行宏的文件,另一个是外部文件。

在运行宏的文件中(以下称为" master"文件),有类似的内容:

enter image description here

到目前为止我的代码是:

Sub test()
For i = 1 To 3
    If Not Range("B" & i).Value = "X" Then

        Range("C2").Value = Range("A" & i).Value
        Calculate 'updates the formula
        Range("B" & i).Value = "X" 'update the check

        Range("D2").Copy 'this is the tricky part - this is what is needed. The formula links needs to be broken so that only the values remain
        Range("D2").PasteSpecial xlPasteValues

        ActiveWorkbook.SaveCopyAs "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm" 'the problem with SaveCopyAs is that the formula originally is now overwritten.
        'thus I need some way to refer back to the 'master' workbook, the one where the formula has not yet been overwritten

    End If
Next i
End Sub

我想要实现的是宏将遍历并检查是否已使用列A中的名称创建工作簿。然后,它将更新" C2"中的值。最后,保存副本 - 并将公式覆盖为其值,而不是保留公式。这是一个难点,因为我不能简单地保存工作簿的副本 - 在运行宏之后公式会被覆盖。

这是在运行宏之后在Type3.xlsm中发生的事情。正如您所看到的," D2"是1,而它应该是3。

enter image description here

我也考虑过这种方法:

Sub test2()
For i = 1 To 3
    If Not Range("B" & i).Value = "X" Then

        Range("C2").Value = Range("A" & i).Value
        Calculate 'updates the formula
        Range("B" & i).Value = "X" 'update the check

        Set wboor = ActiveWorkbook
        fileaddress = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name

        Range("D2").Copy 'this is the tricky part - this is what is needed. The formula links needs to be broken so that only the values remain
        Range("D2").PasteSpecial xlPasteValues

        wboor.SaveCopyAs "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm" 'Perhaps I can save a copy first? Then close the workbook, so the formula is preserved
        wboor.Close
        Workbooks.Open Filename:=fileaddress 'but then, how do I call the original file, and then loop the macro to run again?

    End If
Next i
End Sub

任何建议/帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

您可以保存公式的副本,并且每次都将其放回工作簿中。即:

Option Explicit
Sub test()
With ThisWorkbook.ActiveSheet
    Dim formulaText As String
    formulaText = .Range("D2").Formula
    Dim i As Long
    For i = 1 To 3
        If Not .Range("B" & i).Value = "X" Then
            .Range("C2").Value = Range("A" & i).Value
            Calculate 'updates the formula
            .Range("B" & i).Value = "X" 'update the check

            .Range("D2").Copy
            .Range("D2").PasteSpecial xlPasteValues

            ActiveWorkbook.SaveCopyAs  "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm" 
            .Range("D2").Formula = formulaText
        End If
    Next i
End With
End Sub

答案 1 :(得分:0)

不确定是否有必要开始一个新问题,但无论如何都要尝试这个。

Sub test()

Dim wb As Workbook, s As String, i As Long

For i = 1 To 3
    If Not Range("B" & i).Value = "X" Then
        Range("C2").Value = Range("A" & i).Value
        Calculate 'updates the formula
        Range("B" & i).Value = "X" 'update the check
        s = "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm"
        ActiveWorkbook.SaveCopyAs s
        Set wb = Workbooks.Open(s)
        wb.Sheets(1).UsedRange.Value = wb.Sheets(1).UsedRange.Value
        wb.Close True
    End If
Next i

End Sub