Macro to Find Two Columns

miva0601

New Member
Joined
Mar 31, 2015
Messages
33
I receive a file each month where I need to do a find and replace on two columns.
I highlight both columns and find and replace
find "_GUAR" replace all with nothing
find"," replace all with a space
find "HEALTHMART" replace all with HM
find "HEALTH MART" replace all with HM

Here's what the data looks like when I receive it (before I do the above operations)
Preliminary 202109 TPR.xlsm
BGBH
1Waivers Missed Criteria
78GPR_GUAR,HEALTHMARTGPR
125GPR_GUAR,BPR_GUAR,BPR_GUAR#
136GPR_GUAR,BPR_GUAR,HEALTHMARTBPR
137GPR_GUAR,BPR_GUAR,HEALTHMART#
163BPR_GUARBPR
164GPR_GUAR,BPR_GUAR,HEALTHMARTBPR,GPR
177HEALTHMARTHEALTHMART
254GPR_GUARGPR
269GPR_GUAR,BPR_GUAR,BPR_GUARBPR,GPR
272BPR_GUARBPR
309GPR_GUARGPR
332BPR_GUAR#
345BPR_GUAR#
390BPR_GUAR#
420GPR_GUARGPR
440HEALTHMART,GPR_GUARGPR
451GPR_GUAR,HEALTHMARTGPR
467HEALTHMARTHEALTHMART
490GPR_GUAR,BPR_GUAR,BPR_GUARBPR,GPR,HEALTHMART
494BPR_GUARBPR
576GPR_GUAR,BPR_GUAR,BPR_GUARBPR,GPR
580BPR_GUARBPR
New


After I do the above listed operations, the data looks like the below.
Preliminary 202109 TPR.xlsm
BGBH
1Waivers Missed Criteria
78GPR HMGPR
125GPR BPR BPR#
136GPR BPR HMBPR
137GPR BPR HM#
163BPRBPR
164GPR BPR HMBPR GPR
177 HM HM
254GPRGPR
269GPR BPR BPRBPR GPR
272BPRBPR
309GPRGPR
332BPR#
345BPR#
390BPR#
420GPRGPR
New



At this point, I need to put each cell in alphabetical order and no repeats (not sort the entire column, but the cell, so basically row 269 has GPR BPR BPR and it needs to be BPR GPR), so that ultimately I can add a column with an IF formula that would look at each row and if the two columns match it would say "All Passed")
Here's what final data should look like
Preliminary 202109 TPR.xlsm
BGBH
1Waivers Missed Criteria
78GPR HMGPR
125BPR GPR#
136BPR GPR HMBPR
137GPR BPR HM#
163BPRBPR
164BPR GPR HMBPR GPR
177HMHM
254GPRGPR
269BPR GPRBPR GPR
272BPRBPR
309GPRGPR
332BPR#
345BPR#
390BPR#
420GPRGPR
440GPR HMGPR
451GPR HMGPR
467 HM HM
New


A couple of notes:
The column headers will never change and it will always be column BG and BH
The number of rows does change each month
'#' no action for this, I later just delete this and the cell will be blank
Some months, the Missed Criteria will also have "GCR" which I'd like to put in alphabetical order, so it'd be great if the code allowed for this. Example Missed Criteria column has BPR GPR HM GCR should be BPR GCR GPR HM

Thank you in advance
 
The most time is consumed by using the Replace function on the sheet. I use the replace function but in memory.
Now the execution is immediate:

VBA Code:
Sub replace_on_two_columns_2()
  Dim a As Variant
  Dim i As Long
  Application.ScreenUpdating = False
  With ActiveSheet.Range("BG2:BH" & ActiveSheet.Range("BG" & Rows.Count).End(3).Row)
    a = .Value
      ReDim b(1 To UBound(a, 1), 1 To 2)
      For i = 1 To UBound(a)
        If a(i, 1) <> "" Then
          b(i, 1) = finaldata_2(replacedata(a(i, 1)))
        End If
        If a(i, 2) <> "" Then
          b(i, 2) = finaldata_2(replacedata(a(i, 2)))
        End If
      Next
    .Value = b
  End With
End Sub

Function replacedata(data)
  data = Replace(Replace(data, "_GUAR", "", , , vbTextCompare), ",", " ")
  data = Replace(data, "HEALTHMART", "HM", , , vbTextCompare)
  replacedata = Replace(data, "HEALTH MART", "HM", , , vbTextCompare)
End Function

Function finaldata_2(data)
  Dim a As Variant, itm As Variant
  Dim valores As New Collection
  Dim bex As Boolean
  Dim i As Long

  For Each itm In Split(data, " ")
    bex = False
    For i = 1 To valores.Count
      Select Case StrComp(valores(i), itm, vbTextCompare)
        Case 0
          bex = True
          Exit For
        Case 1
          bex = True
          valores.Add itm, itm, Before:=i
          Exit For
      End Select
    Next
    If bex = False Then valores.Add itm, itm
  Next
  ReDim a(1 To valores.Count)
  For i = 1 To valores.Count
    a(i) = valores(i)
  Next
  finaldata_2 = Join(a, " ")
End Function
You are my hero. I can't thank you enough!
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
The most time is consumed by using the Replace function on the sheet. I use the replace function but in memory.
Now the execution is immediate:

VBA Code:
Sub replace_on_two_columns_2()
  Dim a As Variant
  Dim i As Long
  Application.ScreenUpdating = False
  With ActiveSheet.Range("BG2:BH" & ActiveSheet.Range("BG" & Rows.Count).End(3).Row)
    a = .Value
      ReDim b(1 To UBound(a, 1), 1 To 2)
      For i = 1 To UBound(a)
        If a(i, 1) <> "" Then
          b(i, 1) = finaldata_2(replacedata(a(i, 1)))
        End If
        If a(i, 2) <> "" Then
          b(i, 2) = finaldata_2(replacedata(a(i, 2)))
        End If
      Next
    .Value = b
  End With
End Sub

Function replacedata(data)
  data = Replace(Replace(data, "_GUAR", "", , , vbTextCompare), ",", " ")
  data = Replace(data, "HEALTHMART", "HM", , , vbTextCompare)
  replacedata = Replace(data, "HEALTH MART", "HM", , , vbTextCompare)
End Function

Function finaldata_2(data)
  Dim a As Variant, itm As Variant
  Dim valores As New Collection
  Dim bex As Boolean
  Dim i As Long

  For Each itm In Split(data, " ")
    bex = False
    For i = 1 To valores.Count
      Select Case StrComp(valores(i), itm, vbTextCompare)
        Case 0
          bex = True
          Exit For
        Case 1
          bex = True
          valores.Add itm, itm, Before:=i
          Exit For
      End Select
    Next
    If bex = False Then valores.Add itm, itm
  Next
  ReDim a(1 To valores.Count)
  For i = 1 To valores.Count
    a(i) = valores(i)
  Next
  finaldata_2 = Join(a, " ")
End Function
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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