VBA to fill in the gaps?

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
409
Afternoon All

Anyone know if you can get VBA to fill in the gaps based on the entry above, for example, A1 'Rate1' is copied down until it hits A5 'Rate2', then Rate2 is copied down until it hits A7 'Rate3' etc, End result is it starts off looking like table to the left and end up looking like table to the right?
ABC ABC
1Rate1TypeAName1 1Rate1TypeAName1
2 Name2 2Rate1TypeAName2
3 TypeBName1 3Rate1TypeBName1
4 Name2 4Rate1TypeBName2
5Rate2TypeAName1 5Rate2TypeAName1
6 Name2 6Rate2TypeAName2
7Rate3TypeAName1 7Rate3TypeAName1
8 Name2 8Rate3TypeAName2
9 TypeBName1 9Rate3TypeBName1
10 Name2 10Rate3TypeBName2
11 Name3 11Rate3TypeBName3
12 TypeCName1 12Rate3TypeCName1
13 Name2 13Rate3TypeCName2
14Rate4TypeBName1 14Rate4TypeBName1
15 Name2 15Rate4TypeBName2
16 Name3 16Rate4TypeBName3
17 Name4 17Rate4TypeBName4
18 TypeCName1 18Rate4TypeCName1
19Rate5TypeAName1 19Rate5TypeAName1
20 Name2 20Rate5TypeAName2

<COLGROUP><COL style="WIDTH: 22pt; mso-width-source: userset; mso-width-alt: 1060" width=29><COL style="WIDTH: 48pt" span=3 width=64><COL style="WIDTH: 20pt; mso-width-source: userset; mso-width-alt: 950" width=26><COL style="WIDTH: 22pt; mso-width-source: userset; mso-width-alt: 1060" width=29><COL style="WIDTH: 48pt" span=3 width=64><TBODY>
</TBODY>
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Why VBA ?

Select the A column
F5>Special
Blanks
Enter a = sign
press the arrow up key and hit Ctrl Enter
 
Upvote 0
Afternoon All

Anyone know if you can get VBA to fill in the gaps based on the entry above, for example, A1 'Rate1' is copied down until it hits A5 'Rate2', then Rate2 is copied down until it hits A7 'Rate3' etc, End result is it starts off looking like table to the left and end up looking like table to the right?
...
Since you asked about VBA, here's a VBA approach as applied to ColumnA
Code:
Sub fillgaps()
Dim e
For Each e In Range("A:A")
If Len(e(2)) = 0 Then e(2) = e.Value
If e(2).End(4).Row = Rows.Count Then Exit For
Next e
End Sub
So yes, if you wish you can do it with VBA.
 
Upvote 0
Since you asked about VBA, here's a VBA approach as applied to ColumnA
Code:
Sub fillgaps()
Dim e
For Each e In Range("A:A")
If Len(e(2)) = 0 Then e(2) = e.Value
If e(2).End(4).Row = Rows.Count Then Exit For
Next e
End Sub
So yes, if you wish you can do it with VBA.

Thanks mirabeau, does is exactly what I need it to do.
 
Upvote 0
Thanks mirabeau, does is exactly what I need it to do.
Thanks for feedback. :)

Just for the record, here's another, faster one. Took me under 1.2 seconds to fill in randomized gaps in 1 million rows. (where 50% of cells were gaps)
Code:
Sub morefillgaps()
Dim lr As Long, i As Long, a, c
lr = Range("A" & Rows.Count).End(3).Row
a = Range("A1").Resize(lr)
For i = 1 To lr
    If Len(a(i, 1)) > 0 Then c = a(i, 1) Else a(i, 1) = c
Next i
Range("A1").Resize(lr) = a
End Sub
 
Upvote 0
Thanks for feedback. :)

Just for the record, here's another, faster one. Took me under 1.2 seconds to fill in randomized gaps in 1 million rows. (where 50% of cells were gaps)
Code:
Sub morefillgaps()
Dim lr As Long, i As Long, a, c
lr = Range("A" & Rows.Count).End(3).Row
a = Range("A1").Resize(lr)
For i = 1 To lr
    If Len(a(i, 1)) > 0 Then c = a(i, 1) Else a(i, 1) = c
Next i
Range("A1").Resize(lr) = a
End Sub

Thanks mate, mine is 45,000 rows A to AD so the previous version did this in a few seconds also. thanks again :)
 
Upvote 0
Since you asked about VBA, here's a VBA approach as applied to ColumnA
Code:
Sub fillgaps()
Dim e
For Each e In Range("A:A")
If Len(e(2)) = 0 Then e(2) = e.Value
If e(2).End(4).Row = Rows.Count Then Exit For
Next e
End Sub
So yes, if you wish you can do it with VBA.
this works perfect, i use it for two columns. Column A stops at the end of my range, column B doesnt. Any help?
 

Attachments

  • ex..PNG
    ex..PNG
    10.2 KB · Views: 6
Upvote 0
this works perfect, i use it for two columns. Column A stops at the end of my range, column B doesnt. Any help?
Welcome to the Board!

You would be better off posting your question to a new thread instead of reviving a thread that is over 10 years old, and none of the people on it are active on this board anymore.
Then it will also appear on the "Unanswered threads" listing that many people use to look for new unanswered questions.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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