Loop A Macro

Ranjith Edakkatt

New Member
Joined
Feb 1, 2017
Messages
9
Dear All,
I have an excel data with date, source and SR number. In some cells of date and source is blank, in these case I need to copy the data of above cells and also needs to repeat the same until the SR number becomes blank. Table given below. Thanks in advance.

[TABLE="class: grid, width: 100, align: center"]
<tbody>[TR]
[TD]Date[/TD]
[TD]Source[/TD]
[TD]SR_No[/TD]
[/TR]
[TR]
[TD]13/2/2017[/TD]
[TD]PBU[/TD]
[TD]1-567[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]1-568[/TD]
[/TR]
[TR]
[TD]11/2/2017[/TD]
[TD]PBU[/TD]
[TD]1-896[/TD]
[/TR]
[TR]
[TD]10/2/2017[/TD]
[TD]PBU[/TD]
[TD]1-854[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]1-856[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]1-874[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]1-458[/TD]
[/TR]
[TR]
[TD]10/1/2017[/TD]
[TD]PBU[/TD]
[TD]1-521[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi

not sure I quite follow what you want to do.

Copy what?
copy to where
what is your existing code(if any)
what should your data look like after the code runs

thanks

dave
 
Upvote 0
After re reading your post

are you lookin at filling in the gaps in your data,

if so, try this on a copy of your workbook.

code assume sheet1 is your sheet
also assumes column a,b and c

Dave

Code:
Sub fill_gaps()

Dim sht As Worksheet
Dim LastRow As Long
Set sht = ThisWorkbook.Worksheets("sheet1")
LastRow = sht.Cells(sht.Rows.Count, "C").End(xlUp).Row + 1
    For A = 1 To LastRow - 1
        If Range("'sheet1'!$A$" & A) = "" Then Range("'sheet1'!$A$" & A) = CDate(Range("'sheet1'!$A$" & A - 1))
        If Range("'sheet1'!$B$" & A) = "" Then Range("'sheet1'!$B$" & A) = Range("'sheet1'!$B$" & A - 1)
    Next
End Sub
 
Last edited:
Upvote 0
Thanks Dave for your response.

Here your clarification.

Copy what? - Copy the data from just the above cell of respective column if its not blank.
Copy to where - In only blank cells.

what is your existing code(if any) - code mentioned below, I need to repeat the same until the SR number becomes blank.


Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.Offset(-1, 0).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
 
Upvote 0
Hi

Did you see my post 3, i think after re-reading i understood what you needed.
Try the code from post 3 on a copy of your workbook.

good luck and let me know please.

Dave
 
Upvote 0
or this one is a bit more tidy

Code:
Sub fill_gaps()

Dim sht As Worksheet
Dim LastRow As Long
Set sht = ThisWorkbook.Worksheets("sheet1")
LastRow = sht.Cells(sht.Rows.Count, "C").End(xlUp).Row + 1
    For A = 1 To LastRow - 1
        If Range("A" & A) = "" Then Range("A" & A) = CDate(Range("A" & A - 1))
        If Range("B" & A) = "" Then Range("B" & A) = Range("B" & A - 1)
    Next
End Sub
 
Upvote 0
thanks for the feedback mate.

glad it worked.

dave
 
Upvote 0
One other suggestion without looping to consider:
Code:
Sub fill_gaps_v1()

    Dim x       As Long
    
    With Sheets("Sheet1")
        x = .Cells(.Rows.Count, 3).End(xlUp).row
        With .Cells(1, 1).Resize(x, 2)
            .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
            .Calculate
            .Value = .Value
        End With
    End With
        
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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