Name Sheet Based on Results of the formula

BIGTONE559

Active Member
Joined
Apr 20, 2011
Messages
336
I'm looking to name sheets based on the results of a vlookup in a cell.

I have a Vlookup formula in cell "C4". I run a series of code that will determine whether or not the sheet will be renamed to the value in C4 or the Sheet index number. However, my code errors out because it's referring to the formula and that it cannot name the sheet the formula because it's too long.

Please Help
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this:
Code:
FResult = sheet(1).range("C3").value
sheet(1).name = FResult

Still No Luck. Says invalid name.

Value in "C3"=IF(C4="","",VLOOKUP(C4,Table_EMPLOYEE,2,FALSE))

Code:
Code:
Sub AMaccro()
Dim i As Integer
Dim aSh As Worksheet

Set aSh = ActiveSheet

i = ActiveSheet.Index

FResult = Sheets(i).Range("C3").Value
MsgBox (FResult)

Worksheets(i).Name = FResult

End Sub
 
Upvote 0
run the macro step by step (with F8) and check what is the name that is causing the error. Chances are you are trying to rename the sheet with an invalid name or a name with invalis char, like . or ,
 
Upvote 0
run the macro step by step (with F8) and check what is the name that is causing the error. Chances are you are trying to rename the sheet with an invalid name or a name with invalis char, like . or ,

I'm thinking that it's trying to name the sheet with the formula located in C3. I'm wondering if i can store the result as a string? after some testing i did notice that the fact that C3:F3 had been merged was an issue.
 
Upvote 0
You are storing the result as a string here:
Code:
FResult = Sheets(i).Range("C3").Value
There's even a message box to display the value.

Check to see if the value is a valid sheet name.
 
Upvote 0
You are storing the result as a string here:
Code:
FResult = Sheets(i).Range("C3").Value
There's even a message box to display the value.

Check to see if the value is a valid sheet name.


the returned value is "Mr. Potatohead" so i don't see how that is a problem.

when the message box pops up it displays the value in C3 perfectly. I'm wondering is it a problem that i have the code being ran via "ThisWorkbook" as a private sub????
 
Upvote 0
Hello,

Maybe it helps.
The value in a1 = the sheet name:

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
On Error GoTo Out
If Target = Range("A1") Then ActiveSheet.Name = Range("A1").Value
Out:
End Sub
 
Upvote 0
What do you mean the code is being run via ThisWorkbook?

This code should be in a standard module, not a workbook or worksheet module.
 
Upvote 0
Hello,

Maybe it helps.
The value in a1 = the sheet name:

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
On Error GoTo Out
If Target = Range("A1") Then ActiveSheet.Name = Range("A1").Value
Out:
End Sub


Found the problem!!!!

seems there was a default field size on the field that was being referenced to from access. I trimmed the appropriate field text and it works wonderfully!!

I appreciate all the feedback!
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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