VBA script that will allow me to copy the unit # in column C to the empty cells below untill it reaches the next unit #, then copies that uni

frankafarah

New Member
Joined
Sep 9, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I would like to create a VBA script that will allow me to copy the unit # in column C to the empty cells below untill it reaches the next unit #, then copies that unit # to the empty cells below it untill it reaches the next unit# and so on.

I have attached a sample excel spreadsheet.

Really appreciate any help.

Thanks
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You can't attach a spreadsheet here. Ideally you use XL2BB to post a small representative sample of your data.
Instructions on using this tool can be found here: XL2BB Add-in.
Alternative share some sample data using one of the sharing platforms such as Drop Box, Google drive etc.

In the meantime see if below does what you are after.
It currently assumes your data starts on Row 2 and that Column B will always have an entry in the last row of your table.
Change these to suit your data.

VBA Code:
Sub FillDownUnit()

    Dim ws As Worksheet
    Dim rngC As Range, arrC As Variant
    Dim firstDataRow As Long, lastRow As Long
    Dim UnitNo As Variant                                   ' If its definitely numeric then Long would be preferable
    Dim i As Long
    
    Set ws = ActiveSheet
    firstDataRow = 2                                        ' If headings on Row 1 then first data row is 2. Change if required
    lastRow = ws.Range("B" & Rows.Count).End(xlUp).Row      ' Change column B to whatever column is going to have data on all lines
    Set rngC = ws.Range("C" & firstDataRow & ":C" & lastRow)
    arrC = rngC.Value
    
    For i = 1 To UBound(arrC)
        If arrC(i, 1) <> "" Then
            UnitNo = arrC(i, 1)
        Else
            arrC(i, 1) = UnitNo
        End If
    Next i
    
    rngC.Value = arrC

End Sub
 
Upvote 0
Welcome to the MrExcel board!

My guess is that you want to turn something like this ..

frankafarah.xlsm
ABCD
1Hdr1Hdr2Hdr3Hdr4
2datadataUnit 23data
3datadatadata
4datadataUnit 5data
5datadatadata
6datadatadata
7datadatadata
8datadatadata
9datadataUnit 100data
10datadatadata
Sheet1


.. into this (which is what Alex's code does)

frankafarah.xlsm
ABCD
1Hdr1Hdr2Hdr3Hdr4
2datadataUnit 23data
3datadataUnit 23data
4datadataUnit 5data
5datadataUnit 5data
6datadataUnit 5data
7datadataUnit 5data
8datadataUnit 5data
9datadataUnit 100data
10datadataUnit 100data
Sheet1


If that is what you want, here is another approach you could try with a copy of your workbook.

VBA Code:
Sub Fill_Units()
  With Range("C2:C" & Range("A" & Rows.Count).End(xlUp).Row)
    .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Hi Peter
Thanks so much. I received quite a few suggestions, but yours was the most concise. Worked perfectly
Frank
 
Upvote 0
Thanks so much.
You're welcome. Thanks for the follow-up.

I received quite a few suggestions,
Hmm, so I now see. For the future please note the following.

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:
VBA script that will allow me to copy the unit # in column C to the empty cells below until it reaches the next unit #, then copies that unit #
VBA script that will allow me to copy the unit # in column C to the empty cells below unti [SOLVED]
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
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