Compare and replace cell over multiple sheets

scubadivingfool

New Member
Joined
Jun 17, 2010
Messages
35
Hi Guys,

Need help with a search and replace code. I have the main file, sheet1 has the correct price in column C and the SKU in Column A.

Sheet1
1643547316400.png


I need to search all the sheets that have a partial name of ABC-, as in ABC-1, ABC-2, ABC-3, etc matching the SKU in column A of sheet 1 and replace the cell from sheet 1 into the cell that is 4 cells below the SKU in the sheets named ABC-1, ABC-2 etc.

Sheet2
1643548011429.png


Hopefully this makes sense.
Any help is appreicated
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
It is hard to work with pictures. Also, examples with more data would be helpful. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshots (not pictures) of your sheets. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing'
and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
If you look at sheet 1, what I need is for each of the ItemID to search each "ABC-" and if found, replace the price which is 4 cells down on each ABC with the corresponding price of cell in sheet1.

Here is the file: example.xlsx
ABC-1
817/C12817/G10817/G12817/S13
DESCRIPTIONDESCRIPTIONDESCRIPTIONDESCRIPTION
$1635.05$1672.07$2005.15$5863.85
817/C9817/C10817/CM-22817/CM12
DESCRIPTIONDESCRIPTIONDESCRIPTIONDESCRIPTION
$123.99$1271.14$1607.86$1399.19


ABC-2
582/SV-96582-SV-120582/CHINOOK 14582/CHINOOK 14+
DESCRIPTIONDESCRIPTIONDESCRIPTIONDESCRIPTION
$259.62$288.44$3436.35$5267.48
582/SB90-0810582/SB90-0812582/SB90-1014
DESCRIPTIONDESCRIPTIONDESCRIPTION
$17.33$26.16$36.39


Sheet1
ItemIDUOMSellPrice
------------------
1392/3410EA10.28
1392/4718EA21.22
558/N32LPAEA13.01
1392/3416EA17.92
1392/4712EA13.09
128/CJ09(WHT-S)EA49.89
1392/3118EA7.94
2150/E9158DZ53.91
2150/G3960DZ32.81
1392/3316EA9.66
274/5300(WHT-2XL)EA27.03
783/UCR48AHCEA4345.71
1392/3418EA19.24
1392/691EA18.08
143/TS-S1424CEA39.02
025/5412CBP110EA13.72
152/FG199300GRAYEA104.05
152/FG611277YELEA28.96
074/1675KIT2S48EA464.51
783/WTRCS48HC(FLAT-TOP)EA10654.8
306/PIZ3BEA1936.54
929/50100KIT70.52
025/250LCD131EA215.49
689/S-660-BKDZ45.84
929/DT202EA118.17
016/1293EA3.53
025/2500CT110EA58.28
1392/5457EA46.81
152/FG260900WHTEA17.02
152/FG261000GRAYEA42.74
152/FG9W3000BLAEA249.31
152/FGH115000000EA21
152/FG758088YELEA163.61
2215/7813PK28.58
330/851247EA416.91
330/851147EA175
2150/FN362EA65.65
2215/7810PK28.58
274/FCE-MSK-MED-BLACKEA3.14
1500/AFD3660BNEA60.75
152/FG261960GRAYEA18.58
152/FG262000WHTEA44.23
152/FG342488PLATEA280.97
152/FG421288BLAEA102.3
152/FG637400BLAEA18.61
152/FG637500GRAYEA26.49
152/FGH145000000EA27.71
929/DT133(RH)EA19.3
066/6900E542DZ123.8
117/ML300EA360.48
074/16100KIT48EA422.42
152/FG261960WHTEA18.58
152/FG263100WHTEA24.92
152/FG757788YELEA218.82
2150/G9144DZ231.78
2215/7801PK34.22
274/3040(BLK-S)EA31.88
274/FCE-MSK-LARGE-BLACKEA3.14
274/FCE-MSK-LARGE-WHITEEA3.14
2215/7802PK34.22
3363/93013ADZ47.28
558/1537BEA16.01
689/9909-1-CLDZ49.96
839/UM-RTU500-1EA34.16
929/DT134(RH)EA55.23
025/UC500110EA328.12
152/FG335388BLAEA166.85
1196/M33082P2EA21.84
124/MD2000HT-208V-3PHEA10673.31
2150/10018DZ37.11
025/250LCD110EA215.49
025/UC250110EA291.65
117/ML400EA433.83
152/2020794EA365.74
2150/J0856DZ96.14
2215/7812PK28.58
274/FCE-MSK-MED-NAVYEA3.14
274/FCE-MSK-MED-WHITEEA3.14
330/851248EA416.91
558/1531BEA10.98
558/1537GEA16.01
129/MMB-22WDZ61.99
1009/N-CA5SBRSET117.63
025/5412CBP480EA13.72
152/2020972EA280.66
152/FG261000WHTEA39.71
152/FG263200WHTEA60.4
152/FG264000BLAEA85.45
152/FG611285YELEA28.96
152/FG638906BLAEA20.77
2215/7803PK34.22
463/SE24HCSXG-SPECEA1785.72
817/SP-20EA3781.49
839/UM-ULTRA ATOMER II-DEA225.44
396/5116EA26.14
839/UM-RTU500-4EA86.88
143/TA-HSF-14EA227.41
152/FG263200GRAYEA60.4
2150/46888DZ94.42
2215/7811PK28.58
017/20583.7003EA25.59
025/CDR2020151EA194.27
025/RD1220CW135EA64
056/004898PK92.4
558/1537WEA16.01
702/SI5000EA68.27
3200/62826EA983.06
582/SFE02345 120EA694.63
783/SPE48HC-12CEA5089.45
582/CORTR 110 EA864.05
330/852299EA255.97
330/851176EA437.92
124/DF/DF71EA9425.67
318/T-49-HCEA5879.9
421/2251063EA86.93
318/T-23-HCEA4381.62
124/N900EA5061.4
002/0489/01DZ67.79
002/0489/0DZ67.79
002/0489/41DZ67.79
117/ML180EA126.46
025/GBD101011110EA67.35
025/GBP216110EA44.9
025/EPP400110EA292.43
025/GBD211417110EA119.63
025/GBP518110EA78.58
025/EPP180SW110EA83.48
025/EPP300110EA284.03
025/GBD211414110EA119.63
025/GBD121515110EA92.76
025/GBP318110EA56.13
558/1531WEA10.98
3052/4H-REG89192BL24EA14.86
3478/MANDOLINEEA226.87
016/57509EA7.39
016/574834EA18.42
149/48DZ39.66
689/SW-1425-1-CLDZ31.76
152/FG289000BLAEA132.78
 
Upvote 0
Try:
VBA Code:
Sub ReplaceCell()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, ws As Worksheet, fnd As Range
    v = Sheets("Sheet1").Range("A3", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)).Resize(, 3).Value
    For Each ws In Sheets
        If ws.Name Like "ABC*" Then
            For i = 1 To UBound(v)
                Set fnd = ws.UsedRange.Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole)
                If Not fnd Is Nothing Then
                    fnd.Offset(4).Value = v(i, 3)
                End If
            Next i
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub ReplaceCell()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, ws As Worksheet, fnd As Range
    v = Sheets("Sheet1").Range("A3", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)).Resize(, 3).Value
    For Each ws In Sheets
        If ws.Name Like "ABC*" Then
            For i = 1 To UBound(v)
                Set fnd = ws.UsedRange.Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole)
                If Not fnd Is Nothing Then
                    fnd.Offset(4).Value = v(i, 3)
                End If
            Next i
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
Hi, okay so it works for the most part however I have to manually stop the macro from running otherwise it doesn't stop
 
Upvote 0
I tested the macro on the example file you posted and it worked properly. Are you using the macro on a different file? If so, upload a copy of the file that is causing the problem (de-sensitized if necessary).
 
Upvote 0
I tested the macro on the example file you posted and it worked properly. Are you using the macro on a different file? If so, upload a copy of the file that is causing the problem (de-sensitized if necessary).
Here is the actual file I am using with a lot of sheets and macros deleted.

 
Upvote 0
Your file was huge at more than 62 megabytes. This was caused by the fact that for some reason, the used range in the first sheet went all the way down to row 1,048,576 and over to column CG. If you hold down the CTRL key and press the END key, the cursor will move to the last cell in the used range. If all the unused rows and columns are deleted, the file shrinks to 28 Kilobytes. That's quite a difference in file size. If all of your CGYSR sheets have the same problem, it will take the macro a very long time to run. If you run the macro below, it should delete all the extra rows and columns in all the sheets. Because of the large amount of cleaning it has to do, it may take a while to finish so be patient and let it finish. For the cleaning to be finalized, it is very important that as soon as the macro finishes running, you immediately save the file without doing anything to it. Just go to FILE....SAVE. After saving it, close it and re-open it. Check the file size to make sure that the macro did in fact delete all the extra rows and columns.
VBA Code:
Sub DeleteExtraRowsandColumns()
    Application.ScreenUpdating = False
    Dim lastRow As Long, lCol As Long, ws As Worksheet
    For Each ws In Sheets
        With ws
            .Activate
            lastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            lCol = Cells(3, Columns.Count).End(xlToLeft).Column + 2
            Range("A1").Select
            ActiveCell.SpecialCells(xlLastCell).Select
            Rows(lastRow + 1 & ":" & ActiveCell.Row).EntireRow.Delete
            Range(Cells(1, lCol + 1), Cells(1, ActiveCell.Column)).EntireColumn.Delete
        End With
    Next ws
    Application.ScreenUpdating = True
End Sub
Next try this macro again and see if it runs properly. I made a minor change to reflect the fact that your data in Sheet1 starts in row 1.
VBA Code:
Sub ReplaceCell()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, ws As Worksheet, fnd As Range
    v = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)).Resize(, 3).Value
    For Each ws In Sheets
        If ws.Name Like "CGYSR-*" Then
            For i = 1 To UBound(v)
                Set fnd = ws.UsedRange.Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole)
                If Not fnd Is Nothing Then
                    MsgBox fnd.Address
                    fnd.Offset(4).Value = v(i, 3)
                End If
            Next i
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Your file was huge at more than 62 megabytes. This was caused by the fact that for some reason, the used range in the first sheet went all the way down to row 1,048,576 and over to column CG. If you hold down the CTRL key and press the END key, the cursor will move to the last cell in the used range. If all the unused rows and columns are deleted, the file shrinks to 28 Kilobytes. That's quite a difference in file size. If all of your CGYSR sheets have the same problem, it will take the macro a very long time to run. If you run the macro below, it should delete all the extra rows and columns in all the sheets. Because of the large amount of cleaning it has to do, it may take a while to finish so be patient and let it finish. For the cleaning to be finalized, it is very important that as soon as the macro finishes running, you immediately save the file without doing anything to it. Just go to FILE....SAVE. After saving it, close it and re-open it. Check the file size to make sure that the macro did in fact delete all the extra rows and columns.
VBA Code:
Sub DeleteExtraRowsandColumns()
    Application.ScreenUpdating = False
    Dim lastRow As Long, lCol As Long, ws As Worksheet
    For Each ws In Sheets
        With ws
            .Activate
            lastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            lCol = Cells(3, Columns.Count).End(xlToLeft).Column + 2
            Range("A1").Select
            ActiveCell.SpecialCells(xlLastCell).Select
            Rows(lastRow + 1 & ":" & ActiveCell.Row).EntireRow.Delete
            Range(Cells(1, lCol + 1), Cells(1, ActiveCell.Column)).EntireColumn.Delete
        End With
    Next ws
    Application.ScreenUpdating = True
End Sub
Next try this macro again and see if it runs properly. I made a minor change to reflect the fact that your data in Sheet1 starts in row 1.
VBA Code:
Sub ReplaceCell()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, ws As Worksheet, fnd As Range
    v = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)).Resize(, 3).Value
    For Each ws In Sheets
        If ws.Name Like "CGYSR-*" Then
            For i = 1 To UBound(v)
                Set fnd = ws.UsedRange.Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole)
                If Not fnd Is Nothing Then
                    MsgBox fnd.Address
                    fnd.Offset(4).Value = v(i, 3)
                End If
            Next i
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
OK, that worked. Not sure why I had all those other blank cells it the sheet
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,567
Members
452,652
Latest member
eduedu

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