Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Replace a value.

0

Which system, VBA or Power Query, will function the simplest at replacing values in a worksheet. I am creating a check register. The category field is not always accurate from the bank, so I want to insert the correct category.

In VBA I have tried to use xlookup and index match. The following code is the latest iteration that I'm using.

VBA Code used:

Sub Sams()

Dim VCode As String

Dim Status As String

Dim StatusText As String

Dim ReplaceText As String

Dim EntityC As Range

Dim EntityText As String

Dim CategoryC As Range

Dim CategoryText As String

Dim DataWS As Worksheet

Dim VCodeWS As Worksheet

Dim WorkBk As Workbook

Dim LRow As Long

Dim ColumnToCheck As String

Dim XX As Long

Dim AmountT As Variant

ColumnToCheck = "A"

LastRow = Cells(Rows.Count, ColumnToCheck).End(xlUp).Row

Set WorkBk = ThisWorkbook

Set DataWS = Sheets("Data (2)")

Set VCodeWS = Sheets("V_Code")

For XX = 2 To LRow

EntityText = Range("B" & XX).Value

CategoryText = Range("D" & XX).Value

StatusText = Range("G" & XX).Value

AmountT = Range("E" & XX).Value

 If EntityText = "samsclu" And StatusText = "Auto" Then Range("D" & XX).Value = "Gas"

 If EntityText = "samsclu" And StatusText = "Food" Then Range("D" & XX).Value = "Groceries"

 If EntityText = "samsclu" And StatusText = "Horses" Then Range("D" & XX).Value = "Fuel"

 If EntityText = "samsclu" And StatusText = "USAA" Then Range("D" & XX).Value = "Groceries"

 If EntityText = "samsclu" And StatusText = "Jim" Then Range("D" & XX).Value = "Groceries"

Next XX

End Sub

In power query I have used:

Power Query code:

samsclu  =Table.ReplaceValue(ronaldj,

        each [Category],

        each if [Entity] = "samsclu" and [Status] = "Auto" then "Gas" else 

             if [Entity] = "samsclu" and [Status] = "Food" then "Groceries" else

             if [Entity] = "samsclu" and [Status] = "USAA" then "Groceries" else

             if [Entity] = "samsclu" and [Status] = "Jim" then "Groceries" else

            if [Entity] = "samsclu" and [Status] = "Horses" then "Fuel" else [Category,

        Replacer.ReplaceValue, {"Category"}),

End Query code.

The "Entity" variable is the code I've created for the Vendor.

The "Status" variable is the name for each checking account.

I have 140 vendors and 41 categories.

I've not been able to get the VBA to succeed and it runs slow.

The query is not consistent and is very slow, like multiminutes to complete the query.

Answer
Discuss

Discussion

Hi Jim,
Have you made the changes I suggested? Is your problem resolved?
If so, please mark my answer as Selected.
WillieD24 (rep: 557) Jan 16, '24 at 12:44 am
Add to Discussion

Answers

0

Hello Jim and welcome to the forum,

Thanks for including your file, this makes diagnosing problems easier. When posting code, you should place it between code tags so it appears as it does in a module. To do so, click "CODE" at the top and it will place the following in your question:  [CODE]Code_Goes_Here[CODE] and then delete Code_Goes_Here and paste in your code.

Columns "C" and "E" in your table refer to external files so I am limited as what I can test.

Reviewing your code I made the following observations: 1) you have declared variables "Status", "ReplaceText", "EntityC", and "CategoryC" but they are not used in your code; 2) variable LRow has not been assigned a value but is used in your code; 3)"ColumnToCheck" is assigned the value of "A" but never changes; 4) your line: "LastRow = Cells(Rows.Count, ColumnToCheck).End(xlUp)" – the variable "LastRow" has not been declared; 5) your line: "For XX = 2 To LRow" – variable LRow has no value assigned; 6) your lines: "Set DataWS = Sheets("Data (2)") and "Set VCodeWS = Sheets("V_Code")" – these reference to worksheets that do not live in this workbook. The references need to be to the external workbook. Both Column "C" and "E" refer to: https://d.docs.live.net/815a62cdad0c3bfc/Spreadsheets/USAA - Exports/Spreadsheets/[USAA 2023.xlsm]V_Code, there is no external reference to "Data(2)".

Changing the line "Dim LRow As Long" to "Dim LastRow As Long" will correct items 2 and 4 above.

Changing the line "For XX = 2 To LRow" to "For XX = 2 To LastRow" will correct item 5 above.

To correct item 6) above, the worksheet references need to be corrected to the external sheets. Ex: "Set VCodeWS = https://d.docs.live.net/815a62cdad0c3bfc/Spreadsheets/USAA - Exports/Spreadsheets/[USAA 2023.xlsm]V_Code. Also, with Set DataWS = Sheets("Data (2)") in your code, your table has no reference to Sheets("Data (2)").

Hope this helps

 If this solves your issue please mark my answer as Slected.

Cheers   :-)

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login