Find/Replace based on table NOT WORKING - in SOME cases? Not sure why....?

jwb1012

Board Regular
Joined
Oct 17, 2016
Messages
167
Hello, the code below seems to break at the occurrence of the special character "_" on the headings with "TXN"?? Or that have "_" and "two_words" vs. a "word_number". Any thoughts why it cant handle that / how I can fix it?



ORIGINAL DATA SET

DMC_1NSN_1DateQuantityUnit PriceTotal Price
AA00000115-Mar-2017208.01 $ 160.20
BB00000215-Mar-20172040.04 $ 800.80

<tbody>
</tbody><colgroup><col span="6"></colgroup>


TABLE AFTER MACRO (BELOW IS RUN):
DMCNSNTXN DateTXN QuantityTXN Unit PriceTXN Total Price
AA00000115-Mar-2017208.01 $ 160.20
BB00000215-Mar-20172040.04 $ 800.80

<tbody>
</tbody><colgroup><col span="6"></colgroup>


LOOKUP TABLE
Import HeadingsReport Headings
DMCDMC_1
NSNNSN_1
TXN_DateDate
TXN_QuantityQuantity
TXN_Unit_PriceUnit Price
TXN_Total_PriceTotal Price

<tbody>
</tbody><colgroup><col span="8"></colgroup>


CODE TO REPLACE COLUM HEADINGS BASED ON LOOKUP TABLE:

Code:
Sub Replace_Column_Headings()
    
    Dim rngData     As Range
    Dim rngLookup   As Range
    Dim Lookup      As Range
    
    With Sheets("2 - Report")
        Set rngData = .Range("$A$1", "$H$1")
    End With
    
    With Sheets("1 - Workbook Details")
        Set rngLookup = .Range("$K$25", "$K$30")
    End With
    
    For Each Lookup In rngLookup
        If Lookup.Value <> "" Then
            rngData.Replace What:=Lookup.Value, _
                            Replacement:=Lookup.Offset(0, -4).Value, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByColumns, _
                            MatchCase:=False
        End If
    Next Lookup
    
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,191,178
Messages
5,985,145
Members
439,942
Latest member
bkexcel11230

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
Top