View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Mike Mike is offline
external usenet poster
 
Posts: 3,101
Default If sheet name contains 'A' run macro1, if 'B' run macro 2?

Hi Dr Dan

The code below will cycle through every worksheet in your workbook and if
the last sheet name letter before the .xls is an a or a b it will fire a
message box. You should replace this with the call to the macro you want to
execute.

Mike

Sub dontblameme()
Dim sht As Worksheet
For Each sht In Sheets
Name = sht.Name
Length = Len(Name)
If Mid(Name, Length - 4, 1) = "a" Then
MsgBox (Name) 'or call macro a
ElseIf Mid(Name, Length - 4, 1) = "b" Then
MsgBox (Name) ' or call macro b
End If

Next sht
End Sub

"Dr Dan" wrote:

Hi All,

I have a book with many sheets. The sheets contain two types of data. The
worksheet names contain either an 'A' or a 'B', e.g. 20210A.txt or 20210B.txt

Is it possible to have a macro that will start at the first worksheet, and
do the following:

Does worksheet name contain 'A'? If yes then run macro1.
If no, then does name contain 'B'? If yes run macro2.
Go to next worksheet.
End when no more sheets.

The macros are to plot charts of the data on that sheet, but as the data on
A and B sheets are for different graphs they need different macros.

Any help much appreciated.

Cheers,

Dan