Consulting

Results 1 to 16 of 16

Thread: Importing data from another workbook causes error 9 message

  1. #1
    VBAX Regular
    Joined
    Jan 2008
    Posts
    75
    Location

    Red face Importing data from another workbook causes error 9 message

    Hi All,

    I have this code which I wrote to import data from one Excel workbook to another. It works for some amd for most it comes up with error 9, out of subscripts range. Please, I urgently need your help to fix this. Here is the code:
    [VBA]
    Sub ImportDataintoExcelTemplate()
    Dim SourceBook As Workbook, NewBook As Workbook
    Dim rnStart As Range, rnData As Range
    Dim i As Long
    Dim T As Boolean
    Set SourceBook = ThisWorkbook
    T = Application.Dialogs(xlDialogOpen).Show("*.xls")
    If T = False Then Exit Sub
    Set NewBook = ActiveWorkbook
    With NewBook.Sheets("Sheet1")
    .Range(.Range("A1:Z6000"), .Cells("65536").End (xlUp)).Copy
    End With
    Application.ScreenUpdating = True
    With SourceBook.Sheets("Sheet1")
    .Activate
    .Range("A1:Z6000").Activate
    .Paste
    End With
    Application.CutCopyMode = False
    NewBook.Close SaveChanges:=False
    End Sub[/VBA]

    Aussiebear: Edited to place code within the tags

    Thanks

    Lucpian

  2. #2
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    You get the error message when there is no "Sheet1" Sheet.

    It would be better, as mdmackillop has already asked you to do, to click on the VBA button and THEN paste the code BETWEEN the two bracketed words that you will see.

    Try it please.
    Last edited by tstav; 04-09-2008 at 01:28 PM.
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  3. #3
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    There is no "Sheet1" Sheet, that's why you get the error message.

    Congrats for the VBA button!!!!

    You double-posted you know, but I guess that happened while you were trying to get the VBA button to work...
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  4. #4
    VBAX Regular
    Joined
    Feb 2008
    Posts
    54
    Location
    lucpian,
    There's a button that says "VBA" and it allows you to add code to your posts so that it displays in VBA editor format. Please start doing this.

    See if this works for you:
    [vba]Sub ImportDataintoExcelTemplate()
    Dim SourceBook As Workbook, NewBook As Workbook
    Dim rnStart As Range, rnData As Range
    Dim i As Long
    Dim T As Boolean

    Set SourceBook = ThisWorkbook
    T = Application.Dialogs(xlDialogOpen).Show("*.xls")
    If T = False Then Exit Sub
    Set NewBook = ActiveWorkbook
    NewBook.Sheets("Sheet1").UsedRange.Copy
    SourceBook.Sheets("Sheet1").Range("A1").Paste
    Application.CutCopyMode = False
    NewBook.Close SaveChanges:=False
    End Sub
    [/vba]

    or if you're having problems with sheet names then:

    [VBA]Sub ImportDataintoExcelTemplate()
    Dim SourceBook As Workbook, NewBook As Workbook
    Dim rnStart As Range, rnData As Range
    Dim i As Long
    Dim T As Boolean

    Set SourceBook = ThisWorkbook
    T = Application.Dialogs(xlDialogOpen).Show("*.xls")
    If T = False Then Exit Sub
    Set NewBook = ActiveWorkbook
    NewBook.Sheets(1).UsedRange.Copy
    SourceBook.Sheets(1).Range("A1").Paste
    Application.CutCopyMode = False
    NewBook.Close SaveChanges:=False
    End Sub
    [/VBA]

  5. #5
    VBAX Regular
    Joined
    Jan 2008
    Posts
    75
    Location

    Unhappy

    [VBA]

    Sub ImportDataintoExcelTemplate()
    Dim SourceBook As Workbook, NewBook As Workbook
    Dim rnStart As Range, rnData As Range
    Dim i As Long
    Dim T As Boolean

    Set SourceBook = ThisWorkbook
    T = Application.Dialogs(xlDialogOpen).Show("*.xls")
    If T = False Then Exit Sub
    Set NewBook = ActiveWorkbook
    NewBook.Sheets("Sheet1").UsedRange.Copy
    SourceBook.Sheets("Sheet1").Range("A1").Paste
    Application.CutCopyMode = False
    NewBook.Close SaveChanges:=False
    End Sub

    [/VBA]

    Thanks so much Mike, but I still have errors at the underlined areas above in the code.

    Lucpian

  6. #6
    VBAX Regular
    Joined
    Feb 2008
    Posts
    54
    Location
    Then you probably don't have sheets named "Sheet1".

  7. #7
    VBAX Regular
    Joined
    Jan 2008
    Posts
    75
    Location
    What should I replace the sheet1 with because it has to copy to sheet1.

    Thanks

    Lucpian

  8. #8
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    If the Sheet you are referring to as "Sheet1" is the first Sheet, use the following in your original code
    [vba]With NewBook.Sheets(1)[/vba]
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  9. #9
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    instead of
    With NewBook.Sheets("Sheet1")
    write
    With NewBook.Sheets(1)
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  10. #10
    VBAX Regular
    Joined
    Jan 2008
    Posts
    75
    Location
    Thanks, it works fine with that line of code changed.

    Lucpian

  11. #11
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This is not selecting a resticted range but 6000 rows.
    [VBA]With NewBook.Sheets("Sheet1")
    .Range(.Range("A1:Y6000"), .Cells("65536").End(xlUp)).Copy
    End With
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Threads merged
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    My thread is merged but no solution provded.

  14. #14
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    sujittalukde,
    if you are referring to the 'Problem with codes on different machines' as not solved, I can see that rory has answered to all your questions.

    In case you need something more, please use your thread and not this one. THIS, is Lucpian's territory...
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  15. #15
    Yes tstav, I was referring the 'Problem with codes on different machines'. you said that rory has answered my queries. But when I open my own link titled 'Problem with codes on different machines' , this thread is opening. Where is the solution then?

  16. #16
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    Your thread works just fine. I just posted a test post to it.
    Please use your thread or tell the administrators if sth puzzles you.
    He didn't know it was impossible, so he did it. (Jean Cocteau)

Posting Permissions

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