简体   繁体   中英

VBA Rename spreadsheet based on specific cell value

I would like to rename my spreadsheet based on specific cell value. For example my cell A1 for the different spreadsheet are as follow:

  • Spreadsheet 1: Invoice 120894, Att.:LVM & TPM , ATM Username
  • Spreadsheet 2: Invoice 120896, Att: TAM TAM, ATM Username

I'd like to give my spreadsheet the name in cell A1 with for instance LVM & TPM and TAM TAM.

Could someone please help?

I have found a code which would help renaming but I am not sure how to ignore the values and special characters around :

   Sub RenameSheet()

 'VARIABLES DECLARATION
  Dim rs As Worksheet
  Dim new_name As String, tmp_new_name As String
  Dim counter As Integer: counter = 0
  Dim counter1 As Integer: counter1 = 1
  Dim allNames As Object

  'CODE
  Set allNames = CreateObject("Scripting.Dictionary")

  For Each rs In Sheets
'FIRST, LET'S PARSE THE NAME "LAST NAME" + ", " + "NAME INITIAL" + "."
new_name = Split(rs.Range("A1"), " ")(1) 
'CHECK IF IT EXISTS
If allNames.Exists(new_name) Then
    'ADD A COUNTER "(N)" UNTIL IT DOESN'T EXIST ANYMORE
    tmp_new_name = new_name
    Do While allNames.Exists(tmp_new_name) <> False
        tmp_new_name = new_name & " (" & counter1 & ")"
        counter1 = counter1 + 1
    Loop
    counter1 = 1
    new_name = tmp_new_name
End If
'RENAME
rs.Name = new_name
counter = counter + 1
'KEEP THE NAME STORED IN MEMORY (INTO THE DICTIONARY)
allNames.Add rs.Name, counter
Next rs
End Sub

Thank you !

Here's a quick sub routine to extract the details you need from the strings you start with, providing they're always in the same format, I'm sorry I can't elaborate more at this time since I'm leaving work, I hope you can apply it to your code.

Sub f()

Dim x As String
Dim n As String

    x = "Invoice 120894, Att.:LVM & TPM , ATM Username"
    n = Left(x, Len(x) - InStr(1, x, ","))
    n = Trim(Right(n, Len(n) - InStr(1, n, ":")))

    MsgBox n

End Sub

Here is a good example. With string Hack&Slash you always have to keep in mind the formatting of the strings, and sometimes have to play around with it to get it just right.

Private Sub this()
    Dim this$: this = "Invoice 120894, Att.:LVM & TPM , ATM Username"
    this = Mid(this, InStr(1, this, ":") + 1, Len(this) - InStrRev(this, ",") - 3)
    Debug.Print ; this
End Sub

b/ci get the feeling you're super new to this, I figure id expand on my example so that you can make sense of what it is doing

Private Sub this()
    Dim this$: this = "Invoice 120894, Att.:LVM & TPM , ATM Username"
    '                  123456789012314567890123456789012345678901234
    this = Trim(this)
        'String to use       starting point           Length of extracted string
    this = Mid(this, InStr(1, this, ":") + 1, Len(this) - InStrRev(this, ",") - 3)
    '                start at position 21          to position 29
    Debug.Print ; this
End Sub

See if this helps - you need to be able to tell the compiler where to find the value you want to manipulate.

Private Sub this()
    Dim this$
    this = ThisWorkbook.worksheets("yourworksheetname").Range("A1").Value
    this = Mid(this, InStr(1, this, ":") + 1, Len(this) - InStrRev(this, ",") - 3)
    Debug.Print ; this
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM