试图打开和关闭工作簿

时间:2017-08-04 21:05:04

标签: excel vba excel-vba

我希望在从中复制数据并粘贴到合并文件中的文件后打开和关闭进纸器文件。我希望合并文件中的VBA代码和模块在运行代码后保持打开状态。希望这很清楚,如果不是,我可以尝试更具体。

该文件上还有一个密码,这个例子的密码是Marrin18,我无法打开密码,所以我没有用密码打开所需的密码。

tuple(np.random.randint(256, size=3))

3 个答案:

答案 0 :(得分:0)

Sub consolDirectors()
    Const FPATH As String = "T:\Planning\FY Budget\2018 Budget\" & _ 
                            "Director Templates\With updates\"
    Const RNG_COPY As String = "A1:AH227"
    Dim wb As Workbook

    'you should use the full path here....
    Set wb = Workbooks.Open( _
           FileName:= FPATH & "2018 Budget PL_HC_CAP - MarrinV1.xlsx", _
           Password:= "passHere" )

    'add the file extension here - I'm guessing "xlsx" ?
    Workbooks("2018 Budget PL_HC_CAP - Total 802.xlsx" _
        ).Worksheets("Marrin").Range(RNG_COPY).Value = 
        wb.Worksheets("Summary").Range(RNG_COPY).Value

    'If this file is the one with the macro you can use ThisWorkbook instead
    'ThisWorkbook.Worksheets("Marrin").Range(RNG_COPY).Value = _
    '         wb.Worksheets("Summary").Range(RNG_COPY).Value

    wb.Close False

End Sub

答案 1 :(得分:0)

您无需打开工作簿即可阅读内容。但是你需要逐个单元地写内容,所以这是缺点。如果你可以使用5秒宏,那就是:

Option Explicit

Sub Fd()
    Dim FilePath$
    Dim i As Long, j As Long
    Const FileName$ = "2018 Budget PL_HC_CAP - MarrinV1.xlsx"
    Const SheetName$ = "Summary"
    FilePath = "C:\mypath\" ' path to feeder file 2018 Budget PL_HC_CAP - MarrinV1.xlsx

    DoEvents
    Application.ScreenUpdating = False
    If Dir(FilePath & FileName) = Empty Then
        MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist"
        Exit Sub
    End If
    For i = 1 To 227
        For j = 1 To 34
            Cells(i, j) = GetData(FilePath, FileName, SheetName, Cells(i, j))
        Next j
    Next i
    ActiveWindow.DisplayZeros = False
End Sub

Private Function GetData(Path, File, Sheet, Rng)
    Dim Data$
    Data = "'" & Path & "[" & File & "]" & Sheet & "'!" & Rng.Address(, , xlR1C1)
    GetData = ExecuteExcel4Macro(Data)
End Function

答案 2 :(得分:0)

@tim williams @Tehscript这是我用完的代码,不是很先进,但是同时适用于你的

Sub FeedFiles()
    Workbooks.Open Filename:= "T:\Planning\FY Budget\2018 Budget\Director Templates\With updates\2018 Budget PL_HC_CAP - MarrinV1.xlsx", Password:="Marrin18"
    Workbooks("2018 Budget PL_HC_CAP - MarrinV1.xlsx").Worksheets("Summary").Activate
    Workbooks("2018 Budget PL_HC_CAP - MarrinV1.xlsx").Worksheets("Summary").Range("A1:AH227").Copy
    Workbooks("2018 Budget PL_HC_CAP - Total 802.xlsm").Worksheets("Marrin").Range("A1:AH227").PasteSpecial xlPasteValues
    Workbooks("2018 Budget PL_HC_CAP - MarrinV1.xlsx").Close 
End Sub