Results 1 to 13 of 13

script out of range error when copying from excel to excel

  1. #1
    samiam07$ is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2019
    Posts
    7

    script out of range error when copying from excel to excel

    Code:
     oSheet.copy after:=workbooks(strWorkbookName).Sheets("tabname")
    this is where I get the out of range error.
    these are the lines before I hit the error.

    Code:
    Set oWB1 = oXL.Workbooks.Open(strRFile)     sSheet = "Sheet1"  
        Set oSheet = oWB1.Sheets(sSheet) 
        oSheet.Copy After:=Workbooks(strWorkbookName).Sheets("tabname")


  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,253
    Need an object for the destination workbook.

    Set oWB2 = oXL.Workbooks.Open("other workbook")
    oSheet.Copy After:=oWB2.Worksheets("tabname")
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    samiam07$ is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2019
    Posts
    7
    Set oWb = oXL.Workbooks.Open(strWBName) is what i have as my destination right before Set oWB1 = oXL.Workbooks.Open(strRFile)
    oSheet.Copy After:=oWb.Worksheets("tabnameofdestinationworbook ")

    i have already opened the destination workbook and I have opened the workbook I want to copy sheet1 from. I changed my code to yours and set the destination and I am still getting the script out of range.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,253
    Tested and works for me. Perhaps you should post entire procedure.

    Is sheet name actually "tabnameofdestinationworbook"?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    samiam07$ is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2019
    Posts
    7
    No that is not the actual tab name. I did get it to work. I am working out of access 2010. Here is the line that I changed it to,

    oSheet1.Copy After:=Workbooks("Commercial week of 2019-09-22.xlsb").Sheets(6)

    now the only other issue I have is the this part Workbooks("Commercial week of 2019-09-22.xlsb"). The name of the workbook changes weekly and I wanted to make it a variable but when I put it in i get the script out of range error again. Any ideas?

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,253
    Then your initial code should work with a real sheet name instead of 'tabname'.

    So you are not using a workbook object variable for destination workbook - is workbook manually opened before code executes?

    How is strWorkbookName variable set?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    samiam07$ is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2019
    Posts
    7
    This is how I am using strWorkbookName


    dim strWorkbookName as string
    strWorkbookName = [Forms]![FrmMain]![Teams].[Value] & " week of " & Format(DateAdd("w", -1, Date), "yyyy-mm-dd") & ".xlsb"

    I am not manually opening the workbook it is automated.

    Set oWB1 = oXL.Workbooks.Open(strRFile) opens the workbook.
    Last edited by samiam07$; 09-26-2019 at 10:48 AM. Reason: additional info

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,253
    I asked if destination workbook is opened. It is not opened in any way?

    I have never seen .xlsb extension.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    samiam07$ is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2019
    Posts
    7
    yes the workbook is open.

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,253
    Post your entire procedure.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    samiam07$ is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2019
    Posts
    7
    Unfortunately, I work with sensitive info and can not attach the information you are asking. The .xlsb is a binary extension. Does that mean you can not help?

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    48,253
    You cannot post complete code even though you have posted pieces?

    I just created xlsb file, seems can manipulate same as xlsx.

    The following simple example works for me.

    Code:
    Dim xl As Excel.Application, xlw1 As Excel.Workbook, xlw2 As Excel.Workbook
    Dim strWB As String
    strWB = "Condos.xlsb"
    Set xl = CreateObject("Excel.Application")
    Set xlw1 = xl.Workbooks.Open("C:\Users\June\Condos.xlsx")
    Set xlw2 = xl.Workbooks.Open("C:\Users\June\" & strWB)
    xlw1.Sheets(1).Copy After:=xlw2.Sheets(xlw2.Sheets.Count)
    

    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    samiam07$ is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2019
    Posts
    7
    I figured putting in the pieces I had an issue with would be fine. I ran what you had and it worked, Thank you. I was not using my object correctly. Thank you for the second pair of eyes. Your amazing.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Workbooks Activate - script out of range error
    By johnseito in forum Programming
    Replies: 1
    Last Post: 10-27-2017, 02:46 AM
  2. Convert VBA script from Excel to Access Table
    By Darth_Elicious in forum Access
    Replies: 6
    Last Post: 04-21-2017, 10:46 PM
  3. Date out of range error on Excel Export
    By RCG in forum Import/Export Data
    Replies: 1
    Last Post: 05-09-2016, 05:05 PM
  4. help with copying to excel
    By sambucaman in forum Programming
    Replies: 1
    Last Post: 04-19-2012, 10:41 AM
  5. Error Copying & Pasting from Excel
    By kristyspdx in forum Access
    Replies: 1
    Last Post: 02-03-2012, 08:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Tech Forums: Microsoft Office Forums