Last active
July 25, 2021 20:30
-
-
Save erikerhardt/8deebaf5a87ea306f877a1afaebfb183 to your computer and use it in GitHub Desktop.
MS Excel BVA to replace a list of text strings with another set of strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# =Lookup() is for whole cells https://support.microsoft.com/en-us/office/lookup-function-446d94af-663b-451d-8251-369d5e3864cb | |
# below is to replace a text string inside a longer string | |
# Original code: https://www.howtoexcel.org/vba/how-to-find-and-replace-multiple-text-strings-within-a-text-string/ | |
# How to use: https://www.howtoexcel.org/vba/how-to-use-the-vba-code-you-find-online/ | |
# Step 1: Enable the Developer tab in the Ribbon. | |
# Go to the File tab. | |
# Click Options section. | |
# Click the Customize Ribbon section. | |
# Check the Developer box. | |
# Press the OK button. | |
# Step 2: Open the visual basic editor. (Alt + F11) | |
# Go to the Developer tab. | |
# Press the Visual Basic button in the code section. | |
# Step 3: Insert a module into the current workbook. | |
# If you don’t see the project explorer, go to View > Project Explorer or press Ctrl + R. | |
# In the Project Explorer, find the workbook you want to use the VBA code in and right click on it. | |
# Click Insert from the menu. | |
# Click Module from the sub-menu. | |
# Click on Module1. | |
# Paste your code in the module. | |
# Step 4: Run your code. | |
# THIS STEP IS NOT NECESSARY | |
# (Alternatively, to open the Macro window you can use the Alt + F8 shortcut.) | |
# Go to the Developer tab. | |
# Press the Macros button from the Code section. | |
# Select your code from the Macro window. | |
# Press the Run button | |
# Create a sheet called "VariableNameSubstitutions", put variables in column A and labels in Column B | |
# Use function like this to create a new column with substitutions: | |
# =replacetexts(L3, VariableNameSubstitutions!$A$4:$A$21, VariableNameSubstitutions!$B$4:$B$21) | |
Function REPLACETEXTS(strInput As String, rngFind As Range, rngReplace As Range) As String | |
Dim strTemp As String | |
Dim strFind As String | |
Dim strReplace As String | |
Dim cellFind As Range | |
Dim lngColFind As Long | |
Dim lngRowFind As Long | |
Dim lngRowReplace As Long | |
Dim lngColReplace As Long | |
lngColFind = rngFind.Columns.Count | |
lngRowFind = rngFind.Rows.Count | |
lngColReplace = rngFind.Columns.Count | |
lngRowReplace = rngFind.Rows.Count | |
strTemp = strInput | |
If Not ((lngColFind = lngColReplace) And (lngRowFind = lngRowReplace)) Then | |
REPLACETEXTS = CVErr(xlErrNA) | |
Exit Function | |
End If | |
For Each cellFind In rngFind | |
strFind = cellFind.Value | |
strReplace = rngReplace(cellFind.Row - rngFind.Row + 1, cellFind.Column - rngFind.Column + 1).Value | |
strTemp = Replace(strTemp, strFind, strReplace) | |
Next cellFind | |
REPLACETEXTS = strTemp | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment