VBA add cell value if worksheet is included in workbook

bbalch

Board Regular
Joined
Feb 23, 2015
Messages
53
Hello. I have a system that generates reports and names the worksheets based on a numerical sequence xx-xxx (department number). I would like to add a report title in cell A3 with the department's name based on the worksheet name (department number). The code below works as long as all of the listed sheets are in the workbook. However, if the Worksheet("00-120") is not included the A3 value in Worksheet(00-100) will be "F&A" instead of "General".

Any suggestions on how to modify so that the Range("A3").Value only inserts a value if the Worksheet referenced in the line above it is included in the workbook? This is example lists four department codes but in reality there are approximately 95 worksheets.

Code:
On Error Resume Next

Worksheets("00-100").Activate
Range("A3").Value = "General"


Worksheets("00-120").Activate
Range("A3").Value = "F&A"


Worksheets("00-130").Activate
Range("A3").Value = "BA"


Worksheets("00-140").Activate
Range("A3").Value = "CMO"
 

Baziwan

New Member
Joined
Sep 4, 2018
Messages
32
Try this

Code:
On Error Resume Next
Worksheets("00-100").Activate
If Worksheets("00-100").Activate = True Then
Sheets("00-100").Range("A3").Value = "General"
End If

Worksheets("00-110").Activate
If Worksheets("00-110").Activate = True Then
Sheets("00-110").Range("A3").Value = "F&A"
End If
Worksheets("00-120").Activate
If Worksheets("00-120").Activate = True Then
Sheets("00-120").Range("A3").Value = "F&A"
End If

Worksheets("00-130").Activate
If Worksheets("00-130").Activate = True Then
Sheets("00-130").Range("A3").Value = "BA"
End If

Worksheets("00-140").Activate
If Worksheets("00-140").Activate = True Then
Sheets("00-140").Range("A3").Value = "CMO"
End If
Regards

Baziwan
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,315
Office Version
365
Platform
Windows
remove the Activate like
Code:
On Error Resume Next
Worksheets("00-100").Range("A3").Value = "General"

Worksheets("00-120").Range("A3").Value = "F&A"

Worksheets("00-130").Range("A3").Value = "BA"

Worksheets("00-140").Range("A3").Value = "CMO"
On Error GoTo 0
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,493
Office Version
365
Platform
Windows
You say that you have 95 sheets?

It might work a little better to create a two-column table with your sheet names, and corresponding value to put in cell A3.
Then you can cycle through this table with little VBA code (instead of having to write 95 different blocks in VBA).
 

Forum statistics

Threads
1,082,259
Messages
5,364,099
Members
400,779
Latest member
lumers

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top