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
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.
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)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks