Replace old content with new content base on Cell

trekker1218

Board Regular
Joined
Feb 15, 2018
Messages
86
Office Version
  1. 2019
Platform
  1. Windows
Sheet 1
ABCDEFGHI
1Old
A/C Condensing Units Residential
BTUVoltage SizeHeating
2New
A/C Condensing Units Residential​
Cooling BTUVoltageHeating BTU
3Old
A/C Unit Parts (OEM Only)
BTUHeatingAmperageVoltage Size
4New
A/C Unit Parts (OEM Only)
AC COOLING BTUHeating AreaAMPSAC VOLTAGE
5

Sheet 2
abcdefghi
1A/C Condensing Units ResidentialBTUVoltage SizeHeating
2A/C Unit Parts (OEM Only)BTUVoltage Size
Heating
3

I need a VBA or other solution for this:
I need to match the category in sheet1 B1 to Sheet2 A1 Then replace the old data in Sheet2 C1 with the new data in Sheet1 C2. It has to match the Category in Sheet 2 ColA because the old and new data does not always match the columns between sheets. But the OLD and NEW data will always be rows 1 & 2 and 3 & 4 etc...
I have a sheet2 with 50K lines of data that I need to find and replace old data with new data.

Result like this would be great.
abcdef
1A/C Condensing Units ResidentialCooling BTUVoltageHeating BTU
2A/C Unit Parts (OEM Only)AC COOLING BTUHeating AreaAMPSAC VOLTAGE
3
4

Even if it has to write an entire NEW sheet to move the data. That would work. I would just copy and paste the results to the file I need.

Thanks,
Tino
 
The reason columns N and P in sheet2 don't change is because the values in those columns do not correspond to the OLD values for Attributes 7 & and 8 in sheet1. There is another problem that I can see. You may recall I asked you (Posts 32 and 33) if the attribute values in sheet2 for any Sub Category will always be the same for all the items in that category and you responded that they were all the same. However, in sheet2 I can see that in most columns the attributes for any category are not the same. It's hard to get a working solution when the parameters keep changing.
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi,
I do apologize. I will have all the attributes sorted to show in same column like the NEW ATTACHED file Here:

The attributes will be the same PER Category however the attribute can be in different columns based on that category.
for instance. VOLTAGE Rating in in COL P for 1st catagory but is in COL N for NEXT...
 
Upvote 0
Before I make any changes to the macro, I want to confirm something with you. I looked at A/C Commercial VRF in your most recent file and if I run the macro, none of the values in sheet2 will change for A/C Commercial VRF. I went through attributes 1 to 30 in row 2 of sheet1 and none match the values in row 2 of sheet2 so there will not be any changes. Actually, Attribute 1 for A/C Commercial VRF does match but the new value is the same as the old value so there is no change. Have I interpreted correctly? Also, I don't follow what you mean by:
the attribute can be in different columns based on that category.
for instance. VOLTAGE Rating in in COL P for 1st catagory but is in COL N for NEXT...
If the same attribute is in a different column for two or more categories, if I understood properly, that shouldn't make any difference in the end result. Is this correct?
 
Upvote 0
If you look at Sheet 1 sample here:
Product TypeType Code (P=product type, A=style atribute set)Primary Attribute 1Primary Attribute 2Primary Attribute 3Primary Attribute 4Primary Attribute 5Primary Attribute 6Primary Attribute 7Primary Attribute 8Primary Attribute 9Primary Attribute 10Primary Attribute 11
OLDA/C Commercial VRFPBrand NameBTUHeating BTUVoltage RatingPhaseRefrigerant Type.SEERLength.Width.Height.UPC.
NewA/C Commercial VRFPBrand NameCooling BTUHeating BTUVoltagePhaseRefrigerant SEERLengthWidthHeightUPC.

You can see the old value BTU on Sheet 2 here @ Primary Attribute 7:
Sub CategoryPrimary Attribute 1VALUE_1Primary Attribute 2VALUE_2Primary Attribute 3VALUE_3Primary Attribute 4VALUE_4Primary Attribute 5VALUE_5Primary Attribute 6VALUE_6Primary Attribute 7VALUE_7
A/C Commercial VRFBrand NameValueSub BrandItem NameVRF Indoor UnitTypeSlimSeriesModelBTU4000 BTU/hr
A/C Commercial VRFBrand NameFujitsuSub BrandItem NameVRF Indoor UnitTypeSeriesModelBTU30000 BTU/hr
A/C Commercial VRFBrand NameFujitsuSub BrandItem NameCassette VRF Indoor UnitTypeSeriesModelBTU4000 BTU/hr

It should Change to COOLING BTU...
 
Upvote 0
I'm sorry but that's not going to work. Macros for the most part rely on set patterns. If the pattern is broken, the macros don't return the correct results. In order for the macro I suggested to work, you have to compare the attributes in order as they appear on both sheets. If we have to jump around to find the OLD value in any column, I don' think that I will be able to help.
 
Upvote 0
I understand... How about this. Is there anyway to run a lookup with a find and replace.
My logic:
Do a vlookup or index on the Sheet 1 col B (Product Type) I can CONCATENATE it to read OLD-A/C Commercial VRF and NEW-A/C Commercial VRF.
Once a match is found. Index ROW to find matching attribute label like BTU and repalce it with an OFFSET cell directly Below it.
Something like that.
Even if the MACRO wrote the data to a NEW SHEET with new ATTRIBUTES.

I cannot thank you enough for all your efforts.
 
Upvote 0
I'm trying another approach. Let me get back to you.
 
Upvote 0
Try:
VBA Code:
Sub matchData()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWS As Worksheet, x As Long, fnd As Range, i As Long, rng As Range
    Dim LastRow1 As Long, LastRow2 As Long, myArray1 As Variant, myString1 As String, DataRange1 As Range, lCol1 As Long, fVisRow1 As Long
    Set srcWS = ThisWorkbook.Sheets("Sheet1")
    Set desWS = ThisWorkbook.Sheets("Sheet2")
    LastRow1 = srcWS.Cells(srcWS.Rows.Count, "A").End(xlUp).Row
    LastRow2 = desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Row
    lCol1 = srcWS.Cells(1, srcWS.Columns.Count).End(xlToLeft).Column
    lCol2 = desWS.Rows(1).Find("Primary Attribute 30").Column
    fVisRow1 = srcWS.Range("A2", srcWS.Cells(srcWS.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row
    fVisRow2 = desWS.Range("A2", desWS.Cells(desWS.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row
    For x = 2 To LastRow1 Step 2
        With srcWS.Cells(1).CurrentRegion
            .AutoFilter 2, srcWS.Cells(x, 2)
        End With
        With desWS.Cells(1).CurrentRegion
            .AutoFilter 1, srcWS.Cells(x, 2)
        End With
        
        Set DataRange1 = srcWS.Range("D" & fVisRow1).Resize(, lCol1 - 3)
        For Each rng In DataRange1.Cells
            myString1 = myString1 & ";|;" & rng.Value & ";|;" & rng.Offset(1).Value
        Next rng
        myString1 = Right(myString1, Len(myString1) - 3)
        myArray1 = Split(myString1, ";|;")
        
        For i = LBound(myArray1) To UBound(myArray1) Step 2
            Set fnd = desWS.Rows(fVisRow2).Find(myArray1(i), LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd Is Nothing Then
                With desWS
                    .Range(.Cells(2, fnd.Column), .Cells(LastRow2, fnd.Column)).SpecialCells(xlCellTypeVisible) = myArray1(i + 1)
                End With
            End If
        Next i
        myString1 = ""
    Next x
    srcWS.Range("A1").AutoFilter
    desWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
It works for the first category only.
A/C Commercial VRF.
It does not change the next category data.

but you are getting closer with new idea you had.

Run it on the test-file-sorted.xlxs file and look at COL N Row 10 ~ 18 ( Voltage Rating should change to VOLTAGE)
 
Upvote 0
I had 2 lines of code in the wrong place. Try:
VBA Code:
Sub matchData()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWS As Worksheet, x As Long, fnd As Range, i As Long, rng As Range
    Dim LastRow1 As Long, LastRow2 As Long, myArray1 As Variant, myString1 As String, DataRange1 As Range, lCol1 As Long, fVisRow1 As Long
    Set srcWS = ThisWorkbook.Sheets("Sheet1")
    Set desWS = ThisWorkbook.Sheets("Sheet2")
    LastRow1 = srcWS.Cells(srcWS.Rows.Count, "A").End(xlUp).Row
    LastRow2 = desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Row
    lCol1 = srcWS.Cells(1, srcWS.Columns.Count).End(xlToLeft).Column
    lCol2 = desWS.Rows(1).Find("Primary Attribute 30").Column
    For x = 2 To LastRow1 Step 2
        With srcWS.Cells(1).CurrentRegion
            .AutoFilter 2, srcWS.Cells(x, 2)
            fVisRow1 = srcWS.Range("A2", srcWS.Cells(srcWS.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row
        End With
        With desWS.Cells(1).CurrentRegion
            .AutoFilter 1, srcWS.Cells(x, 2)
            fVisRow2 = desWS.Range("A2", desWS.Cells(desWS.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row
        End With
       
        Set DataRange1 = srcWS.Range("D" & fVisRow1).Resize(, lCol1 - 3)
        For Each rng In DataRange1.Cells
            myString1 = myString1 & ";|;" & rng.Value & ";|;" & rng.Offset(1).Value
        Next rng
        myString1 = Right(myString1, Len(myString1) - 3)
        myArray1 = Split(myString1, ";|;")
       
        For i = LBound(myArray1) To UBound(myArray1) Step 2
            Set fnd = desWS.Rows(fVisRow2).Find(myArray1(i), LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd Is Nothing Then
                With desWS
                    .Range(.Cells(2, fnd.Column), .Cells(LastRow2, fnd.Column)).SpecialCells(xlCellTypeVisible) = myArray1(i + 1)
                End With
            End If
        Next i
        myString1 = ""
    Next x
    srcWS.Range("A1").AutoFilter
    desWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

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