Rename a variable and update it in the rest of the project

Asked

Viewed 136 times

0

Have some easy or automatic way to update the rest of your code when renaming a particular variable on the line Dim?

What do I have:

Dim ABC As String
If Range("A2").Value = 1 Then
    ABC = Range("C2") & ".xls"
ElseIf Range("A2").Value = 2 Then
    ABC = Range("C2") & ".xlsx"
ElseIf Range("A2").Value = 3 Then
    ABC = Range("C2") & ".xlsm"
Else
    ABC = Range("C2") & ".txt"
    End If

What I want:

Dim XYZ As String   'renomear ABC por XYZ
If Range("A2").Value = 1 Then
    XYZ = Range("C2") & ".xls"
ElseIf Range("A2").Value = 2 Then
    XYZ = Range("C2") & ".xlsx"
ElseIf Range("A2").Value = 3 Then
    XYZ = Range("C2") & ".xlsm"
Else
    XYZ = Range("C2") & ".txt"
    End If

1 answer

0


there is no specific function or tool for this, but the most common is to use the famous "Find/Replace" (Ctrl + F). When you have the VBA open, click on Ctrl+F and then on the button Substituir/Replace (or directly in the shortcut Ctrl+H to the window of Substituir/Replace).

Type the old text, the new one, select where you want to search and replace the value (very important) and then on Substituir tudo/Replace all.

Tela para substituir textos

The negative point, variables with fewer characters risk being confused with characters in the middle of other words, like ng (present in Range) or loose letters (such as i, j, x, `y...)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.