copy data to next value on several columns

jocote46

Board Regular
Joined
May 11, 2009
Messages
57
I need your expertise on this, i have this macro but it only works on column A, what if i need the macro to work or do the same thing on multiple columns. i have a big file where i need the macro to copy the data down to the next cell value , etc. until last row.
VBA Code:
 Sub MacroFillAreas()

    For Each area In Columns("A:A").SpecialCells(xlCellTypeBlanks)
        If area.Cells.Row <= ActiveSheet.UsedRange.Rows.Count Then
            area.Cells = Range(area.Address).Offset(-1, 0).Value
        End If
    Next area
End Sub

i know it maybe a simple edit but i tried and i'm getting errors. thanks so much in advance.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
A simple example might be the one like below:
VBA Code:
Sub MacroFillAreas()
  Dim lRow As Integer
 
  For i = 1 To 5 'This will look for until column E. You may increase this as much as you need.
    lRow = Worksheets("OldSheet").Cells(Rows.Count, i).End(xlUp).Row 'Get the last row of that column.
    For ii = 1 To lRow
      Worksheets("NewSheet").Cells(ii, i).Value = Worksheets("OldSheet").Cells(ii, i).Value 'Copy that cell from one sheet to another
    Next
  Next
End Sub
For instance you may add a condition for non-blank cells:
VBA Code:
Sub MacroFillAreas()
  Dim lRow As Integer
 
  For i = 1 To 5 'This will look for until column E. You may increase this as much as you need.
    lRow = Worksheets("OldSheet").Cells(Rows.Count, i).End(xlUp).Row 'Get the last row of that column.
    For ii = 1 To lRow
      If Worksheets("OldSheet").Cells(ii, i).Value <> "" Then 'If old value is not empty
        Worksheets("NewSheet").Cells(ii, i).Value = Worksheets("OldSheet").Cells(ii, i).Value 'Copy that cell from one sheet to another
      End If
    Next
  Next
End Sub
 
Upvote 0
That macro is a fairly inefficient way to fill blanks in a column. However, to suggest the best alternative may require a bit more detail about just what you have, where it is and exactly what you are trying to achieve.

As one example though, if you want to fill all blank cells in the worksheet's UsedRange with the value from the cell above (except row 1 of course since there is no cell above) then you could try this.
This example assumes that the worksheet does not contain formulas that need to be retained.
Test on a copy of the workbook.

VBA Code:
Sub MacroFillAreas_v2()
  With ActiveSheet.UsedRange.Offset(1)
    .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Solution
You're welcome. Thanks for the follow-up.:)
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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