+ Reply to Thread
Results 1 to 7 of 7

Open file-Copy data from and XLSX to xls document

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-17-2015
    Location
    Roanoke, Virginia
    MS-Off Ver
    2010
    Posts
    173

    Open file-Copy data from and XLSX to xls document

    Need a macro that
    opens a closed workbook
    file name: Zip to county to Region.xlsx
    file path C:\test\data

    I need to copy the worksheet "Zip to Region" to an open workbook named like Excessive*.xls
    Trying to copy the worksheet from xlsx to xls errors because of size diff. Changing xls to xlsx is not an option.
    The data on worksheet ZIp to county to Region is A1:C1500

    A new worksheet needs to be added to Excessive*.xls and the data copied onto it.
    The ZIp to county to Region.xlsx should remain open

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Open file-Copy data from and XLSX to xls document

    Try:
    Sub GetZip()
        Const sSrcFileName As String = "C:\Test\Data\Zip to county to Region.xlsx" 'Source filename (full path)
        Const sWSName As String = "Zip to Region" 'Source worksheet name
        Const bCloseSRC As Boolean = False 'change to True to close the source file after copying data
        
        Dim wbSrc As Workbook
        Dim wbTgt As Workbook
        Dim ws As Worksheet
        
        On Error GoTo Terminate
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        Set wbTgt = ThisWorkbook
        
        If WSExists(wbTgt, sWSName) Then
            If MsgBox("Worksheet " & sWSName & " already exists. Replace contents?", vbQuestion + vbYesNo) = vbNo Then
                GoTo Terminate
            Else
                Set ws = wbTgt.Worksheets(sWSName)
                ws.UsedRange.Clear
            End If
        Else
            With wbTgt
                Set ws = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
                ws.Name = sWSName
            End With
        End If
        
        Set wbSrc = Workbooks.Open(sSrcFileName)
        If wbSrc Is Nothing Then
            Err.Raise -1001, , "Unable to open source workbook"
        Else
            wbSrc.Worksheets(sWSName).UsedRange.Copy ws.Range("A1")
        End If
        
        If bCloseSRC Then wbSrc.Close savechanges:=False
        
    Terminate:
        If Err Then
            MsgBox "Error " & Err.Number & " - " & Err.Description, vbCritical + vbOKOnly
            Err.Clear
        End If
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    
    Function WSExists(ByRef wb As Workbook, ByRef sSheetName As String) As Boolean
        Dim ws As Worksheet
        For Each ws In wb.Worksheets
            If ws.Name = sSheetName Then WSExists = True
        Next ws
    End Function
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Contributor
    Join Date
    07-17-2015
    Location
    Roanoke, Virginia
    MS-Off Ver
    2010
    Posts
    173

    Re: Open file-Copy data from and XLSX to xls document

    Opens the workbook but doesn't copy data to open workbook named
    like "Excessive*.xls"

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Open file-Copy data from and XLSX to xls document

    Quote Originally Posted by 3345james View Post
    Opens the workbook but doesn't copy data to open workbook named
    like "Excessive*.xls"
    Are you putting (and running) the code in the "Excessive*" workbook, or in another workbook?

    If it's in another workbook, then we need to change this line:
        Set wbTgt = ThisWorkbook
    You said the workbook is "named like Excessive*.xls"

    So try changing the code to:
    Sub GetZip()
        Const sSrcFileName As String = "C:\Test\Data\Zip to county to Region.xlsx" 'Source filename (full path)
        Const sTgtFileNameContains As String = "Excessive"
        Const sWSName As String = "Zip to Region" 'Source worksheet name
        Const bCloseSRC As Boolean = False 'change to True to close the source file after copying data
        
        Dim wbSrc As Workbook
        Dim wbTgt As Workbook
        Dim ws As Worksheet
        
        On Error GoTo Terminate
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        Set wbTgt = TargetWB(sTgtFileNameContains)
        If wbTgt Is Nothing Then Err.Raise -1002, , "No open workbook named '*" & sTgtFileNameContains & "'*"
        
        If WSExists(wbTgt, sWSName) Then
            If MsgBox("Worksheet " & sWSName & " already exists. Replace contents?", vbQuestion + vbYesNo) = vbNo Then
                GoTo Terminate
            Else
                Set ws = wbTgt.Worksheets(sWSName)
                ws.UsedRange.Clear
            End If
        Else
            With wbTgt
                Set ws = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
                ws.Name = sWSName
            End With
        End If
        
        Set wbSrc = Workbooks.Open(sSrcFileName)
        If wbSrc Is Nothing Then
            Err.Raise -1001, , "Unable to open source workbook"
        Else
            wbSrc.Worksheets(sWSName).UsedRange.Copy ws.Range("A1")
        End If
        
        If bCloseSRC Then wbSrc.Close savechanges:=False
        
    Terminate:
        If Err Then
            MsgBox "Error " & Err.Number & " - " & Err.Description, vbCritical + vbOKOnly
            Err.Clear
        End If
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    
    Function WSExists(ByRef wb As Workbook, ByRef sSheetName As String) As Boolean
        Dim ws As Worksheet
        For Each ws In wb.Worksheets
            If ws.Name = sSheetName Then WSExists = True
        Next ws
    End Function
    
    Public Function TargetWB(ByVal sName As String) As Workbook
        Dim wb As Workbook
        For Each wb In Application.Workbooks
            If InStr(wb.Name, sName) > 0 And wb.FileFormat = 56 Then Set TargetWB = wb
        Next wb
    End Function
    I've highlighted the changes in red.

  5. #5
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Open file-Copy data from and XLSX to xls document

    Hopefully, Olly's suggestion works for you.
    But here is a way to auto-convert your xlsx file to xls
    ChosenName does not require an extension.
    Amend myPath and myFile to match your requirements


    1. put code below in a general module in a workbook
    2. open file "Zip to county to Region.xlsx"
    3. run the code
    Sub SaveWorkBookAsXLX()
        Dim myPath As String, myFile As String, zFile As String
        
        zFile = "Zip to county to Region.xlsx"
        myPath = ActiveWorkbook.Path & "\"      'must finish path with trailing "\"
        myFile = "ChosenName"
        Application.Windows(zFile).Activate
        ActiveWorkbook.SaveAs Filename:=myPath & myFile, FileFormat:=xlExcel8
    End Sub
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  6. #6
    Forum Contributor
    Join Date
    07-17-2015
    Location
    Roanoke, Virginia
    MS-Off Ver
    2010
    Posts
    173

    Re: Open file-Copy data from and XLSX to xls document

    Says no open file named Excessive
    I attached the file I'm using less data and also the Zip file
    The name on the zip changed but I changed in the macro before running

    Thanks for bearing with me

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Open file-Copy data from and XLSX to xls document

    No attachment?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 01-05-2014, 05:50 AM
  2. Replies: 4
    Last Post: 10-23-2013, 12:54 AM
  3. [SOLVED] Open an xlsx with most recent date in filename, run my data pulling macro, then move file
    By alanwu07 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-31-2013, 05:24 AM
  4. [SOLVED] open variably named file, copy/paste data into consolidation file, open next file in list
    By sllawrence1968 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-29-2012, 09:49 PM
  5. How to add the columns data of several xlsx files of a folder in another xlsx file
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-25-2012, 04:29 AM
  6. VBA for excel 2003. Open file dialog box, open 2007 file xlsx, continue with code
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2011, 12:12 AM
  7. Help: Open all xlsx files in dir, copy data from cells and paste in 1 master xlsm
    By dgibson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-29-2011, 04:17 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1