if worksheet name contains
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

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

resolvedResolved · Medium Priority · Version 2010

replyReply Wed 8 Feb 2012, 22:16Delegate Daniel said...

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.

For upcoming training course dates see: Pricing & availability

replyReply Fri 10 Feb 2012, 14:11Trainer Doug said...

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.

 

Please browse our web site to find out more about
microsoft excel 2003 vba courses and other Microsoft training courses.

Excel tip:

New Normal Worksheet

Do you want all your worksheets to confirm to a certain look? Then change the Defaults!!!
1. Press Shift+F11 to create a new worksheet
2. Press Ctrl+A to select (higlight) all cells, Press Ctrl+1, make any formatting changes then click OK.
3. Press F12 (Function 12 key) click in the Save As Type, drop down, then select Template (*.xlt)
4. Click in the Save in drop-down, then find the folder; c:_program files_microsoft office_office_start. (For the underscores shown use backslash)
Name your templete sheet.xlt, then press Enter.
Sheet.xlt is used when you insert a new worksheet (Shift+F11)

Note: These changes are permanent changes on your PC.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard