Use Cell Reference as new sheet name

micko1

Board Regular
Joined
Feb 10, 2010
Messages
80
Yet another stumbling block to solve. The following code enters data into the spreadsheet in the next vacant row. Then copies a template worksheet. What I am trying to do is name the new copied worksheet after a reference created in the leadup code. Sheetname needs to be referenced from ".Cells(lRow, 3).Value = Me.staff_name.Value"
Thanks heaps for taking the time to take a look.
Mick
Code:
Private Sub cmdAdd_Click()
Dim lRow As Long
Dim lPart As Long
Dim ws As Worksheet
'worksheet to copy data to
Set ws = Worksheets("Staff")
'find  first empty row in database
lRow = ws.Cells(Rows.Count, 1) _
  .End(xlUp).Offset(1, 0).Row
 
lPart = Me.cboPart.ListIndex
 
If Trim(Me.cboPart.Value) = "" Then
  Me.cboPart.SetFocus
  MsgBox "Please enter Managers name"
  Exit Sub
End If
'copy the data to the database
With ws
  .Cells(lRow, 1).Value = Me.cboPart.Value
  .Cells(lRow, 2).Value = Me.cboLocation.Value
  [COLOR=red].Cells(lRow, 3).Value = Me.staff_name.Value[/COLOR]
  .Cells(lRow, 4).Value = Me.txtQty.Value
  .Cells(lRow, 5).Value = Me.Lic_due.Value
End With
[COLOR=red]'Not sure what to do here if this is the right spot to do it.[/COLOR]
MySheetName=.Cells(lrow, 3).value
Sheets("Staff Template").Activate
    ActiveSheet.Copy Before:=Sheets(3)
    Sheets("Staff Template (2)").Name = "MySheetName"
'clear the data
Me.cboPart.Value = ""
Me.cboLocation.Value = ""
Me.staff_name.Value = ""
Me.txtQty.Value = ""
Me.Lic_due.Value = ""
Me.cboPart.SetFocus
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Consider this line:
Code:
MySheetName=.Cells(lrow, 3).value
Which worksheet is it referring to? Try:
Code:
MySheetName=[COLOR="Red"]ws[/COLOR].Cells(lrow, 3).value

But do you really need this variable? You already have this value stored in a control, Me.staff_name.Value.

Why not use the control to name the new worksheet?
Code:
[color=green]'mysheetname = .Cells(lRow, 3).Value[/color]
Sheets("Staff Template").Copy Before:=Sheets(3)
ActiveSheet.Name = Me.staff_name.Value
 
Upvote 0
Bertie
Last example works fine. I had created a workaround by calling another macro to find last row and then reference the required cell. It worked but not as well as your suggestion. Thanks for the input it is greatly appreciated.

Mick
 
Upvote 0

Forum statistics

Threads
1,224,561
Messages
6,179,521
Members
452,923
Latest member
JackiG

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