excel vba if function

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Excel VBA if or function

Excel VBA if or function

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 or function

Hi I am trying to do the if function with criteria to detect the exist of worksheets. I want to do: if either “worksheet1” or “worksheet2” or “worksheet3” exists in the workbook then do A else do B.
Have tried below codes but got error msg: “Subscript out of range”

Code 1
If Len(Sheets("worksheet1").Name Or Sheets("worksheet2").Name Or Sheets("worksheet3").Name) > 0 Then

Code 2
If WorksSheets(worksheet2).Name Or WorkSheets(worksheet2).Name Or WorkSheets(worksheet2).Name <>”” then

Is there any way to work something like it?
Thanks
Bin

RE: Excel VBA if or function

Hi Bin,

Try something like the code below.

If Len(Sheets(1).Name) > 0 Or Len(Sheets(2).Name) > 0 Or Len(Sheets(3).Name) > 0 Then

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

RE: Excel VBA if or function

Hi Jens
Thanks a lot for your prompt reply. I have just tried

If Len(Sheets("CanxData").Name) > 0 Or
Len(Sheets("DefiniteData").Name) > 0 Or
Len(Sheets("MastersData").Name) > 0
Then

But the same error msg happened again.
Is there any other way?
Please let me know
Thanks
Bin

RE: Excel VBA if or function

Hi Bin,

Sorry I misunderstood the question but try the code below

Sub Test()
Dim sh As Worksheet
For Each sh In Worksheets
If sh.Name Like "CanxData" Or sh.Name Like "DefiniteData" Or sh.Name Like "MastersData" Then
MsgBox "yes"

End If
Next


End Sub

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

RE: Excel VBA if or function

Thank you!
Bin

RE: Excel VBA if or function

Hi Jens,
Your code above does work but I am trying to cover both situations as listed below:

1) if any one of the three worksheets does exist copy data from the 4th worksheet to the one.
2) if any one of the three does not exist, create a new sheet (with the sheet name) and then copy the data over

I can use If len(sheets("SheetName").name)>0 to deal with the existing pages out of these three. But I am struggling to cover both situation

Please let me know if anyway can be done
THanks
Bin

RE: Excel VBA if or function

Hi Jens,
Your code above does work but I am trying to cover both situations as listed below:

1) if any one of the three worksheets does exist copy data from the 4th worksheet to the one.
2) if any one of the three does not exist, create a new sheet (with the sheet name) and then copy the data over

I can use If len(sheets("SheetName").name)>0 to deal with the existing pages out of these three. But I am struggling to cover both situation

Please let me know if anyway can be done
THanks
Bin

RE: Excel VBA if or function

Hi Jens,
Your code above does work but I am trying to cover both situations as listed below:

1) if any one of the three worksheets does exist copy data from the 4th worksheet to the one.
2) if any one of the three does not exist, create a new sheet (with the sheet name) and then copy the data over

I can use If len(sheets("SheetName").name)>0 to deal with the existing pages out of these three. But I am struggling to cover both situation

Please let me know if anyway can be done
THanks
Bin

RE: Excel VBA if or function

Hi Bin,

Have a look at the code below:

Sub test()
Dim sh As Worksheet
For Each sh In Worksheets
If sh.Name Like "CanxData" Then

'add the copy and paste code

ElseIf sh.Name Like "DefiniteData" Then

'add the copy and paste code

ElseIf sh.Name Like "MastersData" Then
'add the copy and paste code

Else

Sheets.Add.Name = "mastersData" 'or another name
'add the copy and paste code

End If
Next


End Sub
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

RE: Excel VBA if or function

Hi Jens, thanks for your reply. Does the if function work when you put one if within another? Like:
If sheets("rawdata").activecell.value="Canx" then
select the data area.
Dim sh As Worksheet
For Each sh In Worksheets
If sh.Name Like "CanxData" Then
copy the data over to CanxData

Else: create a new data tab and name as "CanxData"

Elseif sheets("rawdata").activecell value="Definite" then
select the data area
Dim sh2 As Worksheet
For Each sh2 In Worksheets
If sh2.Name Like "DefiniteData" Then
copy the data over to DefiniteData

Else: create a new data tab and name as "DefiniteData"

Elseif for Masters etc...

I have tried this way however the macro only reads the else part and directly creates a new tab and name is as CanxData while this tab already existed.
Please let me know thanks
Bin

RE: Excel VBA if or function

Sorry just adjusted as below. Have added "_" to help differ the layers of the double IFs

If sheets("rawdata").activecell.value="Canx" then
select the data area.
______Dim sh As Worksheet
______For Each sh In Worksheets

______If sh.Name Like "CanxData" Then
______copy the data over to CanxData

______Else: create a new data tab and name ______as "CanxData" then copy the data over

______End if
______Next

Elseif sheets("rawdata").activecell value="Definite" then
select the data area
______Dim sh2 As Worksheet
______For Each sh2 In Worksheets

______If sh2.Name Like "DefiniteData" Then
______copy the data over to DefiniteData

______Else: create a new data tab and name ______as "DefiniteData" then copy the data over

______End if
______Next

Elseif for "Masters" etc...

End if
Next

Read more: https://www.stl-training.co.uk/post-35279-excel-vba-if-function.html #ixzz3CKh9EYS1

RE: Excel VBA if or function

Hi Bin,

Yes you can nest as many if decision codes you need and add as many loops you need around the decision codes you need. If your code return from ELSE and shouldn't you have to look at your tests.


Sub test()

If (test1) Then
If (test2) Then
If (test3) Then
'your code
End If
ElseIf (test22) Then
'your code
End If
ElseIf (test11) Then
If (test4) Then
If (test5) Then
'your code
End If
ElseIf (test22) Then
'your code
End If
Else
If (test2) Then
If (test3) Then
'your code
End If
ElseIf (test22) Then
'your code
End If
End If




End Sub

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

RE: Excel VBA if or function

Hi Jens, I think i did it in the right order however i couldn't figure out why it goes to the else first. Can you have a look my codes when you have anytime?
I have emailed it to info inbox
Thanks
Bin

RE: Excel VBA if or function

Please read the file from the comment: '#1 IF for searching Canx data and then copy the data area to a new sheet"CanxData"
Thanks
Bin

RE: Excel VBA if or function

Hi Bin,

I can have a look at your working code for you but it is worth clarifying that this sort of query takes us beyond the scope of the forum.

In these cases we look at your work and if we can identify a solution we will let you know scope, duration and associated costs.

If you would like to look into this further please do contact our inquiries team.





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

RE: Excel VBA if or function

Hi Jens,
I think the issue with my code was because that i have built in the For Loop statements within the nested IF statement, and i am not sure if i have place the For Loop statement at the right place. this could possibly caused the issue above. This only happened in very short part of my page long code.

I want the macro to read the one column on RawData page (For Loop to read through each cell in the column) then copy the correct data area over to the relevant page (For Loop to check each page on the workbook). if the worksheet does not exist then create one.

I can take the example i posted on 4th Sep to explain. I firstly used Dim to variant three "sh", "sh2", "sh3" to stand for the three worksheets and place them out of the whole nested IF statement (here is for the For Loop later to recognise the sheets). and then i place the For Loop " For Each sh In Worksheets" before the second level IF. It seems that it jumped the copy over part and moved to create a sheet directly. Any help would be much appreciated.
Thanks
Bin


Dim sh As Worksheet
Dim sh2 As Worksheet
Dim sh3 As Worksheet
Dim i2 As Integer

For i2 = 1 To 10000

If sheets("rawdata").activecell.value="Canx" then
select the data area.

______For Each sh In Worksheets (For Loop)

______If sh.Name Like "CanxData" Then
______copy the data over to CanxData

______Else: create a new data tab and name ______as "CanxData" then copy the data over

______End if
______Next

Elseif sheets("rawdata").activecell value="Definite" then
select the data area

______For Each sh2 In Worksheets (For Loop)

repeat above ...

End if
Next

RE: Excel VBA if or function

Hi Bin,

If the macro jumps to ELSE then it is because the IF and ELSEIF are false. You are looping through all the worksheets in the workbook. If you have more worksheets in the workbook than you have in your tests you will get the ELSE each time you have a sheet you do not have in the ifs.

I have tested the above code in a workbook with 2 worksheets. One with the name CanxData and One with the name rawdata and it was working fine.

If you have more questions please raise a new forum question. I will not be available the next week.



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

RE: Excel VBA if or function

Ok Jens Thanks for your explaination.
Bin

 

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:

Changing Excel file and worksheet defaults

The appearance of any new Excel files or any new worksheets that are inserted into a file are controlled by two template files, Book.xlt and Sheet.xlt.

By opening, modifying and saving these templates you can change the default settings for all new files and/or all newly inserted sheets.

Use Book.xlt to make change to defaults for new workbooks; and Sheet.xlt to change defaults for sheets.

If you can't find either of these files on your computer, you can create and save them yourself.

You can do this simply by creating a new workbook with the setting you want to use as your defaults; then saving them with the appropriate name in the XLStart folder. If you are using Office 2003, this is usually found in C:\Program Files\Microsoft Office\Office11\XLStart.

View all Excel hints and tips


Server loaded in 0.08 secs.