VBA add cell value if worksheet is included in workbook

bbalch

Board Regular
Joined
Feb 23, 2015
Messages
61
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"
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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
 
Upvote 0
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
 
Upvote 0
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).
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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