macro for updating cells

instanceoftime

Board Regular
Joined
Mar 23, 2011
Messages
102
Thank you all for past help, I am here yet again.

I have a spreadsheet of inventory. I paste into it incoming inventory and would like to copy current info (price and sku) into the new cells.

The spreadsheet is sorted so any new inventory shows first.

If the item number is the same (A) I would like to copy (G) and (H) from below it (directly or not directly if it is blank as well)

20COKE CLASSIC 24/12 OZ5.9912/15/2214SUNDRIESBeverages
20COKE CLASSIC 24/12 OZ5.9911/4/2214SUNDRIES04900001278111.49Beverages
20COKE CLASSIC 24/12 OZ12.694/13/2214SUNDRIES04900001278111.49
25COKE DIET 24/12 OZ14.4911/4/2214SUNDRIESBeverages
25COKE DIET 24/12 OZ13.398/11/2214SUNDRIESBeverages
25COKE DIET 24/12 OZ12.696/23/2214SUNDRIES04900001063311.49Beverages
25COKE DIET 24/12 OZ11.697/27/2114SUNDRIES04900001063311.49
25COKE DIET 24/12 OZ11.195/26/2114SUNDRIES04900001063311.49
25COKE DIET 24/12 OZ10.991/13/2114SUNDRIES04900001063311.49
81PEPSI 24/12Z HI-CONE P1008.492/18/2214SUNDRIES012017017.69
81PEPSI 24/12Z HI-CONE P1006.595/5/2114SUNDRIES012017017.69
83DIET PEPSI 24/12Z HI-CONE8.498/27/2214SUNDRIESBeverages
83DIET PEPSI 24/12Z HI-CONE8.495/31/2214SUNDRIES0120001718577.69Beverages
83DIET PEPSI 24/12Z HI-CONE6.595/5/2114SUNDRIES0120001718577.69
123RUBBER MULCH 1.5CUFT RED10.999/14/2227GARDENGardening
150KIKKOMAN SOY SAUCE 64 OZ6.6911/4/2213FOODSauces
150KIKKOMAN SOY SAUCE 64 OZ5.9910/17/2213FOODSauces
150KIKKOMAN SOY SAUCE 64 OZ5.9910/6/2213FOOD0413900015055.39Sauces
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
try this.
VBA Code:
Sub SbFillBlank()
    Dim R1 As Range
    Set R1 = Range("A1") 'set the data's first cell
    
    Dim myRows
    myRows = R1.CurrentRegion.Rows.Count
    
    Dim i
    For i = myRows - 1 To 0 Step -1
        If R1.Offset(i, 6) = "" And R1.Offset(i, 0) = R1.Offset(i + 1, 0) Then
            R1.Offset(i, 6) = R1.Offset(i + 1, 6)
            R1.Offset(i, 7) = R1.Offset(i + 1, 7)
        End If
    Next i
End Sub
 
Upvote 0
Solution
try this.
VBA Code:
Sub SbFillBlank()
    Dim R1 As Range
    Set R1 = Range("A1") 'set the data's first cell
   
    Dim myRows
    myRows = R1.CurrentRegion.Rows.Count
   
    Dim i
    For i = myRows - 1 To 0 Step -1
        If R1.Offset(i, 6) = "" And R1.Offset(i, 0) = R1.Offset(i + 1, 0) Then
            R1.Offset(i, 6) = R1.Offset(i + 1, 6)
            R1.Offset(i, 7) = R1.Offset(i + 1, 7)
        End If
    Next i
End Sub
 
Upvote 0
If I have understood correctly, this should do them all at once.

VBA Code:
Sub Fill_Values()
  With Range("G1:H" & Range("A" & Rows.Count).End(xlUp).Row)
    .SpecialCells(xlBlanks).FormulaR1C1 = "=IF(RC1=R[1]C1,R[1]C,"""")"
    .Value = .Value
  End With
End Sub
 
Upvote 0
try this.
VBA Code:
Sub SbFillBlank()
    Dim R1 As Range
    Set R1 = Range("A1") 'set the data's first cell
   
    Dim myRows
    myRows = R1.CurrentRegion.Rows.Count
   
    Dim i
    For i = myRows - 1 To 0 Step -1
        If R1.Offset(i, 6) = "" And R1.Offset(i, 0) = R1.Offset(i + 1, 0) Then
            R1.Offset(i, 6) = R1.Offset(i + 1, 6)
            R1.Offset(i, 7) = R1.Offset(i + 1, 7)
        End If
    Next i
End Sub
I didn't get any output?

also I was hoping to start with row 1 and go throught last row on sheet. (currently 30k row)
 
Upvote 0
If I have understood correctly, this should do them all at once.

VBA Code:
Sub Fill_Values()
  With Range("G1:H" & Range("A" & Rows.Count).End(xlUp).Row)
    .SpecialCells(xlBlanks).FormulaR1C1 = "=IF(RC1=R[1]C1,R[1]C,"""")"
    .Value = .Value
  End With
End Sub
Hmm, didn't do anything for me? I will have to look again to make sure it's not me.
 
Upvote 0
Hmm, didn't do anything for me? I will have to look again to make sure it's not me.
Are you sure that you have given us the correct column information?
Here is my sheet before the code was run

instanceoftime.xlsm
ABCDEFGHI
120COKE CLASSIC 24/12 OZ5.994491014SUNDRIESBeverages
220COKE CLASSIC 24/12 OZ5.994486914SUNDRIES4900001278111.49Beverages
320COKE CLASSIC 24/12 OZ12.694466414SUNDRIES4900001278111.49
425COKE DIET 24/12 OZ14.494486914SUNDRIESBeverages
525COKE DIET 24/12 OZ13.394478414SUNDRIESBeverages
625COKE DIET 24/12 OZ12.694473514SUNDRIES4900001063311.49Beverages
725COKE DIET 24/12 OZ11.694440414SUNDRIES4900001063311.49
825COKE DIET 24/12 OZ11.194434214SUNDRIES4900001063311.49
925COKE DIET 24/12 OZ10.994420914SUNDRIES4900001063311.49
1081PEPSI 24/12Z HI-CONE P1008.494461014SUNDRIES12017017.69
1181PEPSI 24/12Z HI-CONE P1006.594432114SUNDRIES12017017.69
1283DIET PEPSI 24/12Z HI-CONE8.494480014SUNDRIESBeverages
1383DIET PEPSI 24/12Z HI-CONE8.494471214SUNDRIES120001718577.69Beverages
1483DIET PEPSI 24/12Z HI-CONE6.594432114SUNDRIES120001718577.69
15123RUBBER MULCH 1.5CUFT RED10.994481827GARDENGardening
16150KIKKOMAN SOY SAUCE 64 OZ6.694486913FOODSauces
17150KIKKOMAN SOY SAUCE 64 OZ5.994485113FOODSauces
18150KIKKOMAN SOY SAUCE 64 OZ5.994484013FOOD413900015055.39Sauces
Sheet1


.. and after the code

instanceoftime.xlsm
ABCDEFGHI
120COKE CLASSIC 24/12 OZ5.994491014SUNDRIES4900001278111.49Beverages
220COKE CLASSIC 24/12 OZ5.994486914SUNDRIES4900001278111.49Beverages
320COKE CLASSIC 24/12 OZ12.694466414SUNDRIES4900001278111.49
425COKE DIET 24/12 OZ14.494486914SUNDRIES4900001063311.49Beverages
525COKE DIET 24/12 OZ13.394478414SUNDRIES4900001063311.49Beverages
625COKE DIET 24/12 OZ12.694473514SUNDRIES4900001063311.49Beverages
725COKE DIET 24/12 OZ11.694440414SUNDRIES4900001063311.49
825COKE DIET 24/12 OZ11.194434214SUNDRIES4900001063311.49
925COKE DIET 24/12 OZ10.994420914SUNDRIES4900001063311.49
1081PEPSI 24/12Z HI-CONE P1008.494461014SUNDRIES12017017.69
1181PEPSI 24/12Z HI-CONE P1006.594432114SUNDRIES12017017.69
1283DIET PEPSI 24/12Z HI-CONE8.494480014SUNDRIES120001718577.69Beverages
1383DIET PEPSI 24/12Z HI-CONE8.494471214SUNDRIES120001718577.69Beverages
1483DIET PEPSI 24/12Z HI-CONE6.594432114SUNDRIES120001718577.69
15123RUBBER MULCH 1.5CUFT RED10.994481827GARDENGardening
16150KIKKOMAN SOY SAUCE 64 OZ6.694486913FOOD413900015055.39Sauces
17150KIKKOMAN SOY SAUCE 64 OZ5.994485113FOOD413900015055.39Sauces
18150KIKKOMAN SOY SAUCE 64 OZ5.994484013FOOD413900015055.39Sauces
Sheet1


The post #2 code also worked for me, (but it loops individually through each row, which would likely be quite slow for 30k rows)
 
Upvote 0
Are you sure that you have given us the correct column information?

I can't figure out why you get no output.
Maybe you can try this code amending from Peter_Sss's.
All you need is to make sure the Columns("G:H") is what you need.

VBA Code:
Sub Fill_Values_1()
    Columns("G:H").SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=IF(RC1=R[1]C1,R[1]C,"""")"
    Columns("G:H") = Columns("G:H").Value
End Sub
 
Upvote 0
try this.
VBA Code:
Sub SbFillBlank()
    Dim R1 As Range
    Set R1 = Range("A1") 'set the data's first cell
   
    Dim myRows
    myRows = R1.CurrentRegion.Rows.Count
   
    Dim i
    For i = myRows - 1 To 0 Step -1
        If R1.Offset(i, 6) = "" And R1.Offset(i, 0) = R1.Offset(i + 1, 0) Then
            R1.Offset(i, 6) = R1.Offset(i + 1, 6)
            R1.Offset(i, 7) = R1.Offset(i + 1, 7)
        End If
    Next i
End Sub
I don't know what I did yesterday but started fresh this morning and this worked perfectly on my sample. Thanks HongRu and others for your help.
 
Upvote 0

Forum statistics

Threads
1,216,091
Messages
6,128,775
Members
449,468
Latest member
AGreen17

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