+ Reply to Thread
Results 1 to 4 of 4

VBA Loop not working correctly

  1. #1
    Registered User
    Join Date
    08-01-2022
    Location
    Kansas
    MS-Off Ver
    365
    Posts
    2

    VBA Loop not working correctly

    I have an issue pulling in multiple sheets from one workbook into another workbook in Excel. I have been around the posts, and can't seem to find a good solution. I have two-subs that work together. The first is set to a button that the end user will get the source file with the raw data (this file name will be different every time). This part seems to work just fine:
    Please Login or Register  to view this content.
    Once this source file is open, there will be between 10-20 sheets with data that I need to pull into the other workbook "18AWM036_IMG_BondEdge_ReportBook_WM111_manual.xlsm"
    Those sheets in the source file will always have changing names and the number of sheets will vary, so this will need to work given those criteria. Each sheet in the source file will also vary, so I made the 'Copy' range vast. Once copied I need those source file sheets copied into the other workbook at the very end. Here is the code, which doesn't work quite right. Trying to see where my issue is (I have a background in Java, but not VBA):
    [CODE]
    Sub readExcelData(sTheSourceFile)

    Application.ScreenUpdating = False

    Dim src As Workbook, x As Long
    Set src = Workbooks.Open(sTheSourceFile, False, True)
    For x = 1 To Sheets.Count
    src.Sheets(x).Select
    Sheets(x).Range("A1", "ZZ500").Copy
    Workbooks.Open ("18AWM036_IMG_BondEdge_ReportBook_WM111_manual.xlsm")
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Paste
    src.Activate
    Next x

    End Sub
    Last edited by jwarner0297; 08-01-2022 at 03:46 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,792

    Re: VBA Loop not working correctly

    If you want to copy each sheet wholesale, the better way to do it is to copy the sheet object rather than the sheet's content.

    You don't want to open the file each pass in the loop.

    You should type all variables, even Sub parameters.

    It is a best practice in VBA to explicitly qualify object references, with few exceptions.

    The biggest difference between Java and VBA is that VBA incorporates an object model specific to the application that is running the code.

    I have not tested this code due to the need to replicate your environment but please try this and let me know how it works.

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    08-01-2022
    Location
    Kansas
    MS-Off Ver
    365
    Posts
    2

    Re: VBA Loop not working correctly

    Two things, apologies for the code tagging, and thank you for linking me to the rules (13 rules with most as common knowledge are helpful).

    2nd, this appears to have worked as desired and I thank you for the quick reply.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,792

    Re: VBA Loop not working correctly

    Glad to help!

    If a member helped you solve your problem, consider adding to their reputation by clicking Add Reputation below their name.

+ 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: 1
    Last Post: 08-30-2017, 02:32 AM
  2. [SOLVED] for all ws loop not working correctly
    By Lsxtrkiller in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-28-2017, 01:18 PM
  3. Loop not working correctly
    By hlg0035 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-15-2016, 01:43 PM
  4. For loop within loop not working correctly
    By HalPlz in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-25-2015, 12:06 AM
  5. [SOLVED] Can't get nested Loop to copy range values from 2 tables working correctly
    By gtol in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-05-2013, 12:01 PM
  6. Replies: 9
    Last Post: 03-08-2013, 11:50 AM
  7. Run time Error 91 - End Loop not working correctly
    By rowing190 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-05-2009, 05:18 PM

Tags for this Thread

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