excel vba if function

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Excel VBA: IF function and For loop

Excel VBA: IF function and For loop

resolvedResolved · Urgent Priority · Version 2010

Bin has attended:
Excel VBA Intro Intermediate course
Access Introduction course
Access Intermediate course
Access Advanced course
Excel VBA Advanced course

Excel VBA: IF function and For loop

Hi, I wonder if you could help below:
I want the macros to do a For Loop and read through one column on the RawData page. then use if function to extract data onto three pages: 1 / 2 / 3.
1 for the data remarked with X,
2 with D, and
3 with M

The spreadsheets starts with only the RawData page. so when macro meets X page 1 will be created then D/2 and 3/M. When i log new data the macro will also refill the three pages with more X/D/M data.

I have tried below codes. But i have one issue. When the page of 1/2/3 was there, the nested IF jumps to else directly to create one rather than refill on the existed page
Can you please give me some help?
THanks Bin

Dim i As Integer
For i = 1 to 1000
'Loop the RawData page range of the number of rows in the column

'macro starts on the RawData page
IF activecell.value = "X" then
'loop all the pages in the workbook and look for sh/sh1/sh2
For Each sh in worksheets
-------IF sh.Name Like "1" Then 'detect if "1" was available in the workbook
-------Copy the data from RawData page over to "1"
-------Else sheets.add.name="X" and then copy the data over and go back to the RawData page
-------EndIF
-------Next

'then repeat the above codes in below for the Ds and Ms

ElseIf ActiveCell.Value = "D" then ...
For Each sh1 in worksheets
-------IF sh1.NameLike "2" then...

ElseIf ActiveCell.Value = "M" then ...
For Each sh2 in worksheets
-------IF sh2.NameLike "3" then...

End IF
Next





RE: Excel VBA: IF function and For loop

Hi Do you have any updates on this query?
Thanks
Bin

RE: Excel VBA: IF function and For loop

Hi Bin,

You have to have your tests correct in your decision code. Excel will only do the "else" if nothing is true.

May I suggest that you in a new workbook have one sheet named "1". Add your own code below

For Each sh in worksheets
-------IF sh.Name Like "1" Then 'detect if "1" was available in the workbook
-------Copy the data from RawData page over to "1"
-------Else sheets.add.name="X" and then copy the data over and go back to the RawData page
-------EndIF
-------Next


You will find out it is working (if of course the code inside the decision is correct). Than add a new layer and test it and correct it if not. Keep on adding the next layers to the macro. There is nothing in the decision code you sent me which shouldn't work under the right circumstances.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Thu 18 Sep 2014: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Create a unique items table from a duplicating table

1. Ensure that your list has column headings
2. Select the entire list
3. From the menu bar, select DATA, FILTER, ADVANCED FILTER
4. Select "Filter the list, in place", and tick the "Unique Records Only" box
5. Click OK, filtered list appears.

View all Excel hints and tips


Server loaded in 0.07 secs.