Flexible code for changing sheet name

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Morning,
I use the code below to allow the user to add any extra info on my database before printing the final copy.

The line in Red is where im stuck.
Currently the sheet name "INV" is my problem, Yes it works fine if the sheet will ALWAYS be called INV but this isnt the case

The sheet can be any name as its a copy from the original, So one minute its TOM JONES then it could be BRUCE SMITH

Can you advise what / how i should name it as it cant be a fixed name, it needs to be flexible
If it help this code is run from the sheet in question at the time THEN the user is taken to my database to make the edit



Rich (BB code):
Private Sub PrintGeneratedSheet_Click()
Dim rng As Range
Dim answer As Integer

    answer = MsgBox("ANY INFO NEED ADDING TO INVOICE LIKE VIN ETC ?" & vbNewLine & vbNewLine & "BEFORE PRINTING ??", vbInformation + vbYesNo, "ANY INFO PRINT OK MESSAGE")
  
  If answer = vbNo Then
      ActiveWindow.SelectedSheets.PrintOut copies:=1
      Remove_Sht  ' THIS WILL DELETE THE GENERATED WORKSHEET THEN ONCE DELETED RETURN TO WORKSHEET INV
  Else
  
  MsgBox "ADD THE EXTRA INFO THEN PRINT MY COPY"
  
  With Sheets("DATABASE")
      Worksheets("DATABASE").Activate
  End With
    
  Set rng = ActiveSheet.Columns("A:A")
      findString = Worksheets("INV").Range("G13").Value 
  Set cell = rng.Find(What:=findString, LookIn:=xlFormulas, _
      LookAt:=xlWhole, MatchCase:=False) ' CUSTOMER FOUND IN COLUMN A
    
  If cell Is Nothing Then
      MsgBox "NO CUSTOMER WAS FOUND"
  Else
  With Sheets("DATABASE")
      cell.Select
      ActiveCell.Offset(0, 15).Select ' CUSTOMERS CELL IN COLUMN P NOW SELECTED
  End With
  End If
  
  End If
  
  End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Some more info for you.
This will not work
Rich (BB code):
LastSheetName = Sheets(Sheets.Count).Name

Sometimes there might be 3 sheets TOM JONES BRUCE SMITH **** TURPIN

So the code will need to take on board the worksheet name that the user was currently on
 
Upvote 0
If the code is in the Sheet then you should be able to use the Me keyword. i.e. - findString = Me.Range("G13").Value
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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