+ Reply to Thread
Results 1 to 10 of 10

VBA script that will allow me to copy the unit # in column C to the empty cells below unti

Hybrid View

FFarah VBA script that will allow me... 09-09-2023, 01:07 AM
skywriter Re: VBA script that will... 09-09-2023, 03:15 AM
FFarah Re: VBA script that will... 09-10-2023, 06:47 PM
FFarah Re: VBA script that will... 09-10-2023, 07:05 PM
ORoos Re: VBA script that will... 09-09-2023, 03:17 AM
FFarah Re: VBA script that will... 09-10-2023, 06:48 PM
maxpit Re: VBA script that will... 09-09-2023, 08:10 AM
TMS Re: VBA script that will... 09-09-2023, 08:57 AM
TMS Re: VBA script that will... 09-10-2023, 03:13 PM
TMS Re: VBA script that will... 09-10-2023, 06:52 PM
  1. #1
    Registered User
    Join Date
    09-08-2023
    Location
    Canada
    MS-Off Ver
    365
    Posts
    4

    VBA script that will allow me to copy the unit # in column C to the empty cells below unti

    I would like to create a 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 # to the empty cells below it until it reaches the next unit# and so on.

    I have attached a sample excel spreadsheet.

    Really appreciate any help.

    Thanks
    Attached Files Attached Files
    Last edited by FFarah; 09-09-2023 at 01:16 AM. Reason: error

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,792

    Re: VBA script that will allow me to copy the unit # in column C to the empty cells below

    Option Explicit
    
    Sub UnitNumbers()
    Application.ScreenUpdating = False
    Dim lr As Long, ws As Worksheet, a, r As Range
    Set ws = Worksheets("Sheet1")
    lr = ws.Cells(Rows.Count, "F").End(xlUp).Row
    Set a = ws.Range("C6:C" & lr).SpecialCells(xlCellTypeBlanks).Areas
        For Each r In a
            r.NumberFormat = r(0).NumberFormat
            r.Value = r(0)
        Next r
    Application.ScreenUpdating = True
    End Sub

  3. #3
    Registered User
    Join Date
    09-08-2023
    Location
    Canada
    MS-Off Ver
    365
    Posts
    4

    Re: VBA script that will allow me to copy the unit # in column C to the empty cells below

    Thanks so much your script worked fine

  4. #4
    Registered User
    Join Date
    09-08-2023
    Location
    Canada
    MS-Off Ver
    365
    Posts
    4

    Re: VBA script that will allow me to copy the unit # in column C to the empty cells below

    Thanks your script worked fine

  5. #5
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2024
    Posts
    998

    Re: VBA script that will allow me to copy the unit # in column C to the empty cells below

    Hi there,

    Assuming the first Unit code to copy is in cell C6 (row 6).

    Try the code below;



    Option Explicit
    
    Sub FillUnitDown()
    
    Dim cell As Range
    Dim lRow As Integer
    
    lRow = Cells(Rows.Count, 6).End(xlUp).Row   ' 6 = column F to which end we fill down to.
    
    Range("C6:C" & lRow).Select     ' Assumming the first Unit number is in row 6
    
    For Each cell In Selection
      If cell = "" Then
        cell.FillDown
      End If
    Next
    
    End Sub
    Last edited by ORoos; 09-09-2023 at 03:21 AM.
    If your Question is answered; please mark it SOLVED. If you are happy with a member's solution, say 'Thanks' and click the 'Star' to Add Reputation.

  6. #6
    Registered User
    Join Date
    09-08-2023
    Location
    Canada
    MS-Off Ver
    365
    Posts
    4

    Re: VBA script that will allow me to copy the unit # in column C to the empty cells below

    hanks. Worked fine

  7. #7
    Forum Contributor maxpit's Avatar
    Join Date
    07-24-2023
    Location
    Como, Italy
    MS-Off Ver
    MSO 365 - Ver 2208
    Posts
    185

    Re: VBA script that will allow me to copy the unit # in column C to the empty cells below

    Hi FFarah,

    this is another version
    Sub UpdateUnit()
    
        Dim urF As Long
        Dim Row As Long
        
        urF = Cells(Rows.Count, "F").End(xlUp).Row                ' last row in column "F"
        Row = 6                                                   ' starting row
        
        Application.ScreenUpdating = False                        ' disable screen update
        Do While Row <= urF
            If Len(Cells(Row, 3).Text) = 0 Then
                Cells(Row - 1, 3).Copy Cells(Row, 3)
            End If
            Row = Row + 1
        Loop
        Application.ScreenUpdating = True                         ' enable screen update
        
    End Sub
    Try and let us know.

    Bye

    P.S.: if you want only value of cell, modify
    Cells(Row - 1, 3).Copy Cells(Row, 3)
    with
    Cells(Row - 1, 3).Value = Cells(Row, 3).Value
    Last edited by maxpit; 09-09-2023 at 08:29 AM.
    Max
    let's compare ideas

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,649

    Re: VBA script that will allow me to copy the unit # in column C to the empty cells below

    Or try this. No loop.

    Option Explicit
    
    Sub sFillBlanks()
    
    Dim lLR As Long, rRng As Range
    
    Application.ScreenUpdating = False
    On Error GoTo lblExit
    
    lLR = Range("F" & Rows.Count).End(xlUp).Row
    Set rRng = Range("C6:C" & lLR).SpecialCells(xlCellTypeBlanks)
    rRng.FormulaR1C1 = "=R[-1]C"
    Set rRng = Range("C6:C" & lLR)
    With rRng
        .NumberFormat = "@"
        .Value = .Value
    End With
    
    lblExit:
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by TMS; 09-09-2023 at 09:00 AM.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,649

    Re: VBA script that will allow me to copy the unit # in column C to the empty cells below

    Thanks for the rep.

    Let us know how you get on.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,649

    Re: VBA script that will allow me to copy the unit # in column C to the empty cells below

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Find the last row on column A -> copy the row from A unti H-column
    By Tsinos in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-28-2022, 06:47 AM
  2. Need A Fast Script To Copy 80000 Cells But My Current Script Is Too Slow
    By Genus Max in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-20-2019, 09:54 AM
  3. VBA Script To Copy & Paste Values & Formatting To First Empty Row
    By cfcMalky in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-09-2019, 04:37 AM
  4. Copy cells to next empty column
    By A440 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-04-2015, 11:34 AM
  5. [SOLVED] Copy all cells in column that contain data & paste to first empty row of another column.
    By BPSJACK in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-02-2014, 08:45 AM
  6. Need to find empty filled cells in a column and copy to the empty cell below
    By Grahamfeeley in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-30-2014, 10:09 AM
  7. Copy a formula to cells in column A which don't have an empty cell in column B
    By floep in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-24-2006, 04:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1