Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

if worksheet name contains

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » If Worksheet name contains X then do Y

If Worksheet name contains X then do Y

ResolvedVersion 2010

Daniel has attended:
Excel Advanced course
Excel VBA Intro Intermediate course
Excel VBA Advanced course

If Worksheet name contains X then do Y

Hi,

I'm trying to write a procedure that will consolidate the information from a number sheets. The sheets are all within the same workbook and there is a mix of names. However the sheets I want to consolidate all start with the word holdings. How can I write some code which has a loop which looks at the worksheet name, determines if it starts with holdings, if it does then copies the data to a master sheet and if not moves onto the next sheet?

Any help would be much appreciated.

Many thanks.

RE: If Worksheet name contains X then do Y

Hi Daniel

One way to copy only for the holding sheets is to use an IF statement such as:

If Left(mysheet.Name, 8) = "holdings" Then
Cells.Select
Selection.Copy
...
End if

The Left function picks up the first 8 characters of the sheet name.

This would be within a For loop that cycles through all the sheets adding the data to the master sheet.

I've attached an example where 3 holding sheets are consolidated with Copy/Paste Special Add.

You didn't say how your macro did the consolidation but let me know if I'm on the right lines.

Thanks
Doug Dunn
Best STL Training

Attached files...

Consolidate holdings.xls

Fri 17 Feb 2012: 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:

Customize the toolbar in Excel 2010

You can create your own toolbar which contains your favourite or most used tools. This will make using Excel much more efficient. To do this, you need to click on View, then select Customize Quick access Toolbars and then select Customize. A list of tools will then appear on the screen of which you can add or remove them as you please.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.09 secs.