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?
[TABLE="width: 352"]
<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>[TR]
[TD="class: xl24, width: 29, bgcolor: silver"] [/TD]
[TD="class: xl24, width: 64, bgcolor: silver, align: center"]A[/TD]
[TD="class: xl24, width: 64, bgcolor: silver, align: center"]B[/TD]
[TD="class: xl24, width: 64, bgcolor: silver, align: center"]C[/TD]
[TD="class: xl27, width: 26, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, width: 29, bgcolor: silver, align: center"] [/TD]
[TD="class: xl24, width: 64, bgcolor: silver, align: center"]A[/TD]
[TD="class: xl24, width: 64, bgcolor: silver, align: center"]B[/TD]
[TD="class: xl24, width: 64, bgcolor: silver, align: center"]C[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]1[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Rate1[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]1[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Rate1[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]2[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]2[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate1[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]3[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]3[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate1[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]4[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]4[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate1[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]5[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Rate2[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]5[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Rate2[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]6[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]6[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate2[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]7[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Rate3[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]7[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Rate3[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]8[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]8[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate3[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]9[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]9[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate3[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]10[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]10[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate3[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]11[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name3[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]11[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate3[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name3[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]12[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeC[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]12[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate3[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeC[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]13[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]13[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate3[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]TypeC[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]14[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Rate4[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]14[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Rate4[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]15[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]15[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate4[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]16[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name3[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]16[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate4[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name3[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]17[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name4[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]17[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate4[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]TypeB[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name4[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]18[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeC[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]18[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate4[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeC[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]19[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Rate5[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]19[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Rate5[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name1[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: silver, align: center"]20[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[TD="class: xl27, bgcolor: transparent, align: center"] [/TD]
[TD="class: xl24, bgcolor: silver, align: center"]20[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]Rate5[/TD]
[TD="class: xl26, bgcolor: transparent, align: center"]TypeA[/TD]
[TD="class: xl25, bgcolor: transparent, align: center"]Name2[/TD]
[/TR]
</TBODY>[/TABLE]
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
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,223,272
Messages
6,171,113
Members
452,381
Latest member
Nova88

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