Add new sheet/change properties

Zanatos1986

New Member
Joined
Apr 15, 2010
Messages
48
I am writing code from a drop down box to add a new sheet to my workbook, expand upon a name and change properties of the new sheet.

I am on a sheet labeled "GTS Addition", on this page I have a dropdown list and a blank box to inset my info.

Dropdown List ($N$10) Options - X, Y
Blank Box ($N$12)

My code currently adds a new sheet with the name of the blank box. I need to change the color of the tab based upon the name of the dropdown list, I also need to add the name in the blank box to a predefined list on the main page "GTS Library"

I have listed my code below, I have tried several things but I am just not getting anywhere. All help is appreciated


Code:
' Add Company

Private Sub CommandButton2_Click()

Dim AddAsLastWorksheet()
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = Range("$N$12").Value

If Range($n$10).Value = X Then
    With ActiveWorkbook.Sheets("Test").Tab
        .Color = 10498160
        .TintAndShade = 0
    End With
End If

If Range($n$10).Value = Y Then
    With ActiveWorkbook.Sheets("Test").Tab
        .Color = 5287936
        .TintAndShade = 0
    End With
End If

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Couple of things that I see with the code right off the bat is that your Xs and Ys in your if/then statements need to be in quotes (ie If Range($n$10).Value = "X")

Second thing is that the With statements are referring to "Test" sheet, and not the sheet you added in the Worksheets.Add statement.
 
Upvote 0
I added the quotes.
As far as the "test" sheet, I'm not sure how to re-access the sheet I just created after going back to the sheet that determines the properties of the new sheet... I am aware that is wrong, and that is where I am stuck
 
Upvote 0
You can either set up the sheet name as a string variable or add the new worksheet as a worksheet variable. Either one of these should work:

By the way - Not sure what you are trying to do with this line of code: "Dim AddAsLastWorksheet()"


Rich (BB code):
'Add Company
 
Private Sub CommandButton2_Click()
 
 
Dim AddAsLastWorksheet()
Dim SheetName as String
 
SheetName = Range("$N$12").Value
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = SheetName
 
If Range($n$10).Value = "X" Then
    With ActiveWorkbook.Sheets(SheetName).Tab
        .Color = 10498160
        .TintAndShade = 0
    End With
End If
 
If Range($n$10).Value = "Y" Then
    With ActiveWorkbook.Sheets(SheetName).Tab
        .Color = 5287936
        .TintAndShade = 0
    End With
End If
 
End Sub

or:

Rich (BB code):
Private Sub CommandButton2_Click()
 
 
Dim AddAsLastWorksheet()
Dim WS as Worksheet
 
SheetName = Range("$N$12").Value
WS.Add(After:=Worksheets(Worksheets.Count)).Name = Range("$N$12").Value
 
If Range($n$10).Value = "X" Then
With WS.Tab
.Color = 10498160
.TintAndShade = 0
End With
End If
 
If Range($n$10).Value = "Y" Then
With WS.Tab
.Color = 5287936
.TintAndShade = 0
End With
End If
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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