+ Reply to Thread
Results 1 to 6 of 6

Filling in blanks using VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    04-11-2019
    Location
    Burton-on-Trent, England
    MS-Off Ver
    2010
    Posts
    4

    Filling in blanks using VBA

    Hi,

    I have a (hopefully) fairly simple problem for some of you guys.

    In my data, the A column is a product code. B through to L is other data related to that product. If the product code appears more than once (which is ok, because the data, such as location, may be different) then there are just empty cells below. I have used VBA to open this from a txt source in excel.

    The below is directly copied from my excel sheet. The gaps are where the same product code should appear as above. So for example: "8190268" should appear a further three times below the top one. What is the cleanest way to achieve this please? Please bear in mind the sheet may contain a lot more rows than in this example and also will change on a daily basis when importing the updated version.

    To clarify, I don't want any blanks in the A column, and any current blanks have beside them data relating to the product above. So I just want that product code to be in the blank cells.

    Thanks in advance.

    Product
    8083958
    8084248
    8084518
    8182038
    8186058
    8186078
    8190268



    8220048
    8225848
    8244288
    8244398
    8260708
    8340948
    6694948
    7935598
    7935608
    7935618
    7935648

    7639038

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,642

    Re: Filling in blanks using VBA

    Hi there,

    See if the following code does what you need:

    
    
    Option Explicit
    
    
    Sub FillInBlankCells()
    
        Const sPRODUCT_COLUMN   As String = "A"
        Const iFIRST_DATA_ROW   As Integer = 2
        Const sSHEET_NAME       As String = "Product Data"
    
        Dim rFirstCell          As Range
        Dim rLastCell           As Range
        Dim rLastRow            As Range
        Dim rCell               As Range
        Dim wks                 As Worksheet
    
        Set wks = ThisWorkbook.Worksheets(sSHEET_NAME)
    
        With wks
    
            Set rFirstCell = .Range(sPRODUCT_COLUMN & iFIRST_DATA_ROW)
    
            Set rLastRow = .UsedRange.Rows(.UsedRange.Rows.Count)
    
        End With
    
        Set rLastCell = Intersect(rLastRow, _
                                  rFirstCell.EntireColumn)
    
        For Each rCell In Range(rFirstCell, rLastCell).Cells
    
            If rCell.Value = vbNullString Then
                rCell.Value = rCell.Offset(-1, 0).Value
            End If
    
        Next rCell
    
    End Sub
    The highlighted values may be changed to suit your requirements.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    04-11-2019
    Location
    Burton-on-Trent, England
    MS-Off Ver
    2010
    Posts
    4

    Re: Filling in blanks using VBA

    Hi Greg,

    Thank you so much for your time, this worked an absolute treat. Exactly what I needed.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,754

    Re: Filling in blanks using VBA

    If you load your worksheet into Power Query you can use the Fill function to fill down the empty cells with the data above. It is a one step feature in Power Query.

    Here is the Mcode

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", Int64.Type}}),
        #"Filled Down" = Table.FillDown(#"Changed Type",{"Product"})
    in
        #"Filled Down"
    See in my signature how to apply
    Last edited by alansidman; 04-30-2019 at 07:24 AM.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,642

    Re: Filling in blanks using VBA

    Hi again,

    Many thanks for your feedback.

    You're welcome - glad I was able to help.

    Regards,

    Greg M

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,789

    Re: Filling in blanks using VBA

    Another option
    Sub dembele2504()
       With Range("A2", Range("B" & Rows.Count).End(xlUp).Offset(, -1))
          .SpecialCells(xlBlanks).FormulaR1C1 = "=r[-1]c"
          .Value = .Value
       End With
    End Sub

+ 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. Filling without blanks.
    By jgr4444 in forum Excel General
    Replies: 1
    Last Post: 05-03-2018, 03:37 PM
  2. [SOLVED] Only filling in blanks
    By 2ndLunch in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-10-2016, 03:19 PM
  3. Filling in blanks with what's above
    By AdamBee in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-15-2013, 06:12 AM
  4. Script searching blanks in a row then filling them
    By crazy-mrt in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-25-2012, 03:00 PM
  5. Filling in the Blanks
    By stumbler555 in forum Excel General
    Replies: 2
    Last Post: 11-01-2012, 02:15 PM
  6. Filling in the blanks
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-27-2010, 01:00 PM
  7. need help filling in blanks
    By yellowbus in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 03-17-2008, 01:39 PM

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