Results 1 to 4 of 4

Thread: Open a workbook & copy data using

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    wanted to prompt the user to point to the file...

    used this...

    Public Sub fileopener()
        Dim myFileName As Variant
        Dim SourceWkbk As Workbook
        Dim CurrentWkbk As Workbook
        Dim testWks As Worksheet
        Dim DestCell As Range
        myFileName = Application.GetOpenFilename("Excel files,*.xls")
        If myFileName = False Then
            Exit Sub 'user hit cancel
        End If
        Set CurrentWkbk = ActiveWorkbook
        Set SourceWkbk = Workbooks.Open(Filename:=myFileName)
        Set testWks = Nothing
        On Error Resume Next
        Set testWks = SourceWkbk.Worksheets("sheet1")
        On Error GoTo 0
        If testWks Is Nothing Then
            MsgBox "Missing the worksheet!"
        Else
            With CurrentWkbk.Worksheets("Test")
                Set DestCell = .Cells(.Rows.Count, "a").End(xlUp).Offset(1, 0)
            End With
            With testWks
                'choose one of these
                .Range("a1", .Cells.SpecialCells(xlCellTypeLastCell)).Copy
                DestCell.PasteSpecial Paste:=xlPasteAll
                Application.CutCopyMode = False
            End With
        End If
        SourceWkbk.Close savechanges:=False
    End Sub
    does the trick... think it must just have been a 'brain phart' :-) as i've not done any coding 'in anger' for a while...
    Last edited by Aussiebear; 07-01-2024 at 03:28 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •