+ Reply to Thread
Results 1 to 3 of 3

Thread: Importing worksheets if certain condition is fulfilled

  1. #1
    Registered User
    Join Date
    05-30-2011
    Location
    Iowa
    MS-Off Ver
    Excel 2010
    Posts
    23

    Importing worksheets if certain condition is fulfilled

    Hi

    I need help with this, I am trying to import worksheets into my workbook if certain condition is fulfilled. I am able to do it the first time, but the coding would not import the second, third and etc times. Please help!

    Thanks in advance
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2003-2010
    Posts
    377

    Re: Importing worksheets if certain condition is fulfilled

    it is because when you insert the first sheet, the active sheet is changed and "Example" (where the Button 1 is) is no longer the active one. You have to reselect the original sheet for your code to work.
    Like this: (make sure the names are the ones you have in your workbooks)

    Sub Button1_Click()
    
    If Range("H29").Value = 1 Then Workbooks("PleaseWork2.xlsx").Sheets("ExampleH").Copy After:=Workbooks("PleaseWork(1).xlsm").Sheets("Example")
    
    Workbooks("PleaseWork(1).xlsm").Activate
    Sheets("Example").Select
    If Range("H30").Value = 1 Then Workbooks("PleaseWork2.xlsx").Sheets("Something").Copy After:=Workbooks("PleaseWork(1).xlsm").Sheets("Example")
    
    End Sub
    Last edited by royUK; 07-07-2011 at 07:52 AM.

  3. #3
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Importing worksheets if certain condition is fulfilled

    Make sure that the range being checked refers to the correct sheet, you don't need to select the sheet

    Sub Button1_Click()
    With Sheets("Example")
    If .Range("H29").Value = 1 Then _
        Workbooks("PleaseWork2.xlsx").Sheets("ExampleH").Copy _
        After:=Workbooks("PleaseWork.xlsm").Sheets("Example ")
    If .Range("H30").Value = 1 Then _
        Workbooks("PleaseWork2.xlsx").Sheets("Something").Copy _
        After:=Workbooks("PleaseWork.xlsm").Sheets("Example ")
    End With
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0