Consulting

Results 1 to 5 of 5

Thread: Switching worksheets in another workbook

  1. #1

    Switching worksheets in another workbook

    I am trying to retrieve data from worksheets of another workbook to the current "this" workbook.

    [vba]
    Dim ThisWB As Workbook
    Dim ThisWS As Worksheet

    Dim AnotherWB As Workbook
    Dim AnotherWS As Worksheet

    Set ThisWB = ThisWorkbook
    Set ThisWS = ThisWB.Worksheets("TargetSheet")

    Set AnotherWB = Workbooks.Open(filename:="C:\test1.xlsm", IgnoreReadOnlyRecommended:=True)

    Set AnotherWS = AnotherWB.Worksheets("SheetAA")
    ThisWS.Range("A1").value = AnotherWS.Range("A1").value

    '****This is where system crashes
    Set AnotherWS = AnotherWB.Worksheets("SheetBB")
    ThisWS.Range("A2").value = AnotherWS.Range("A1").value

    Set AnotherWS = AnotherWB.Worksheets("SheetCC")
    ThisWS.Range("A3").value = AnotherWS.Range("A1").value

    [/vba]

    Any ideas? I have tried to use the whole qualified Workbook.Worksheet.Range and it is still crashed.
    Thanks.
    Last edited by Bob Phillips; 08-30-2012 at 03:35 PM. Reason: Added VBA tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Does this give you the same problem?

    [VBA]Dim ThisWB As Workbook
    Dim ThisWS As Worksheet

    Dim AnotherWB As Workbook
    Dim AnotherWS As Worksheet

    Set ThisWS = ThisWorkbook.Worksheets("TargetSheet")

    Set AnotherWB = Workbooks.Open(Filename:="C:\test1.xlsm", IgnoreReadOnlyRecommended:=True)

    AnotherWB.Worksheets("SheetAA").Range("A1").Copy ThisWS.Range("A1")

    AnotherWB.Worksheets("SheetBB").Range("A1").Copy ThisWS.Range("A2")

    AnotherWB.Worksheets("SheetCC").Range("A1").Copy ThisWS.Range("A3")
    [/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    The Lord,

    Thank you for the suggestion.
    Actually the code is a loop to read a range of this worksheet into a variant.
    If you could show me with the application of your suggestion, it would be great.
    And I might have found the problem, which is one of the worksheet is "Sheet_Name1" (where it crashes) has an underscore. When I replaced it with a "-", it works.
    Is there such a limitation?
    Gracia!

  4. #4
    You can use:

    [vba]
    Sub snb()
    With getobject("C:\test1.xlsm")
    for j=1 to 3
    thisworkbook.sheets("Targetsheet").cells(j,1)=.sheets(choose(j,"AA","BB","C C")).cells(1).value
    next
    .close False
    end with
    end sub
    [/vba]
    Assuming
    - the workbook that contains this code also has a sheet named 'targetsheet'
    - the workbook C:\test1.xlsm" contains the sheets 'AA', 'BB' and 'CC'

  5. #5
    Thanks.

Posting Permissions

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