Using LEN and LEFT together

viper

Active Member
Joined
Feb 15, 2002
Messages
382
How can I use Len and Left together to ensure a workbook name begins with Order?

I've tried using Left("Order", 5) and didn't work.

Len(Activeworkbook.Name, 5) didn't work either.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I'm not sure what the problem is? Are you trying to change the existing workbook name to
Code:
"Order" & ThisWorkbook.Name

or does this help?
Code:
If Left(ActiveWorkbook.Name, 5) = "Order" Then
    MsgBox "starts with Order"
Else
    MsgBox "starts with something else"
End If
 
Upvote 0
Thank you to both!!! Sorry so vague in my need. I have a macro that I only want to work when a certain workbook is opened and it has to begin with the word order.

So the code:
Code:
IF LEFT(Activeworkbook.Name, 5) <> "Order" THEN 
    EXIT SUB
END IF

worked perfect! Thank you..
 
Upvote 0
BTW, that can be done on one line:

Code:
If Left(ActiveWorkbook.Name, 5) <> "Order" Then Exit Sub
 
Upvote 0
Thanks!! I thought u could do it that way but i added an else and continuing code below.
 
Upvote 0
You shouldn't need an Else
If it doesn't meet the requirement and Exit Sub, the code will continue anyway.
 
Upvote 0
Does it work better to use several one line ifs other than just use if, else, end if? It's working so far the way I have it but I'm always looking for ways to improve the way I write code.

Right now I have it:

Code:
If Left(ActiveWorkbook.Name, 5) <> "Order" Then
   Exit Sub
Else
   If Sheets.Count > 1 Then
         Sheets("Sheet2").Delete
         Sheets("Sheet3").Delete
   End If
End If
 
Upvote 0
There's really no difference.

In fact I would favour the complete If End If structure.

To me it makes the code easier to follow and I've seen plenty of times here that code has failed because something has been missed due to a one-line if.
 
Upvote 0
What it will do is make your code look cleaner, it will be easier to read and follow. For example, it's better not to type 3 lines of code when 1 will do.



Code:
If Left(ActiveWorkbook.Name, 5) <> "Order" Then Exit Sub
If Sheets.Count > 1 Then
    Sheets("Sheet2").Delete
    Sheets("Sheet3").Delete
End If

See what I mean?
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top