Better Way Than Using Dictionaries?

Snort

New Member
Joined
Sep 15, 2023
Messages
12
Office Version
  1. 365
Platform
  1. Windows
I've got a macro for the purpose of standardizing column headings on data received from numerous different sources. I'm using a different dictionary for each relevant column, and it works great, but I'm curious if there's a better / different way of doing it?

Here's a snippet of the macro capped at 5 entries per dictionary for this example. There's many more dictionaries and entries in the dictionaries than displayed here - I'm just looking to see if there's a better way.

VBA Code:
'**********************************************
Dim AgencyName As New Scripting.Dictionary
AgencyName.CompareMode = TextCompare

AgencyName.Add "ProvName", 1
AgencyName.Add "FACIL_NM", 1
AgencyName.Add "Svc Prov Org Name", 1
AgencyName.Add "Serv Group Practice Name", 1
AgencyName.Add "Billing Prov LName", 1
'***********************************************
'*********************************************
Dim AgencyProviderNumber As New Scripting.Dictionary
AgencyProviderNumber.CompareMode = TextCompare

AgencyProviderNumber.Add "PayToProviderNumber", 1
AgencyProviderNumber.Add "clinic_npi", 1

'**********************************************
'**********************************************
Dim BilledAmount As New Scripting.Dictionary
BilledAmount.CompareMode = TextCompare

BilledAmount.Add "SUBMITTED", 1
BilledAmount.Add "Submitted Charge", 1
BilledAmount.Add "billed_amt", 1
BilledAmount.Add "Billed Amt", 1
BilledAmount.Add "BILLED AMOUNT", 1
'***********************************************

Range("A1").Select

For X = 1 To LastColumn

    Z = Cells(1, X)
        
    If xRecipID.Exists(Z) Then
        ActiveCell.Value = "Recipient ID"
            
    ElseIf AgencyName.Exists(Z) Then
        ActiveCell.Value = "Agency Name"
        
    ElseIf AgencyProviderNumber.Exists(Z) Then
        ActiveCell.Value = "Agency Provider Number"
        
    ElseIf AgencyType.Exists(Z) Then
        ActiveCell.Value = "Agency Type"
        
    ElseIf BilledAmount.Exists(Z) Then
        ActiveCell.Value = "Billed Amount"
        
    End If
Next X
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Not tested
VBA Code:
    AgencyName = "ProvName$!FACIL_NM$!Svc Prov Org Name$!Serv Group Practice Name$!Billing Prov LName$!"
    AgencyProviderNumber = "PayToProviderNumber$!clinic_npi$!"
    BilledAmount = "SUBMITTED$!Submitted Charge$!billed_amt$!Billed Amt$!BILLED AMOUNT$!"

    Range("A1").Select

    For X = 1 To LastColumn
        Z = Cells(1, X)

        If InStr(AgencyName, Z & "$!") > 0 Then
            ActiveCell.Value = "Agency Name"

        ElseIf InStr(AgencyProviderNumber, Z & "$!") > 0 Then
            ActiveCell.Value = "Agency Provider Number"

        ElseIf InStr(BilledAmount, Z & "$!") > 0 Then
            ActiveCell.Value = "Billed Amount"
        Else
            ActiveCell.Value = "n/a"
        End If
    Next X
 
Upvote 0
My suggestion is to put the data in a sheet instead of in the macros, like this:
Book1
ABC
1AgencyNameAgencyProviderNumberBilledAmount
2ProvNamePayToProviderNumberSUBMITTED
3FACIL_NMclinic_npiSubmitted Charge
4Svc Prov Org Namebilled_amt
5Serv Group Practice NameBilled Amt
6Billing Prov LNameBILLED AMOUNT
Sheet2

Then, use the Find method to retrieve the correct header. I can help you with the code, but I don't understand your code in this part:
Range("A1").Select .. and then in the loop ... ActiveCell.Value = ....
but you didn't activate any other cell in the process, so Range("A1") is always the ActiveCell.
Could you please provide an example along with the expected result?
 
Upvote 0
Solution
I don't understand your code in this part:
Range("A1").Select .. and then in the loop ... ActiveCell.Value = ....
but you didn't activate any other cell in the process, so Range("A1") is always the ActiveCell.
The Range("A1").Select is to ensure that the active cell was A1. Sounds like it's not necessary.

The ActiveCell.Value stuff is a whoops on my end. The code was a Do Until loop where I activated each cell, and just yesterday I learned how to change this into a For Loop. Hadn't fully updated the code when I copy pasted as you can tell from my post.

Thanks for your idea about the sheet instead of dictionaries. I'll give it some thought and experiment some on my end to see which way I like better - dictionaries or the sheet.

Have a great day!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0
I don't see anything inherently bad or inefficient about using dictionaries. You could also just change your existing code minimally to load the dictionaries from the worksheet location.

VBA Code:
    Dim rng As Range
    Dim rngCol As Range
    Dim RC As Range, RR As Range
    
    'Populate dictionaries from worksheet definitions.
    With Worksheets("Sheet2") 'per @Akuini's example
        Set rngCol = .Range(.Cells(1, 1), .Cells(1, .Columns.Count).End(xlToLeft))
        For Each RC In rngCol
            Set rng = .Range(.Cells(2, RC.Column), .Cells(.Rows.Count, RC.Column).End(xlUp))
            For Each RR In rng
                Select Case RC.Value
                    Case "AgencyName"
                        AgencyName.Add RR.Value, 1
                    Case "AgencyProviderNumber"
                        AgencyProviderNumber.Add RR.Value, 1
                    Case "BilledAmount"
                        BilledAmount.Add RR.Value, 1
                End Select
            Next RR
        Next RC
    End With
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,040
Members
449,092
Latest member
ikke

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