+ Reply to Thread
Results 1 to 25 of 25

Fill in the Blanks - VBA

  1. #1
    Registered User
    Join Date
    07-17-2015
    Location
    Hertfordshire, England
    MS-Off Ver
    Windows 7
    Posts
    11

    Fill in the Blanks - VBA

    Hi,

    i would like a macro in VBA which fills in the blanks in the below report.

    Fill Blanks.PNG

    i normally do this by inserting the relevant number of columns and inserting the formula
    Please Login or Register  to view this content.
    however this is very repetative and i am doing this alot of times throughout each day.

    i have written some code but i am just a beginner. See below current code -

    Please Login or Register  to view this content.
    this code works, but if the number of columns (In purple on the left) changes then it falls over. Also i would like the blank cells in green to be filled in with zero's. Please ignore the fact some of the numbers in green have 'EA' next to them. These are treated as just numbers.


    many thanks.

  2. #2
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    314

    Re: Fill in the Blanks - VBA

    It would help if you would describe - in plain language - what you want to accomplish by this. Just scanning the portion of the spreadsheet that you quote, it looks (assuming that A1 is the top left corner) that you want to replace a missing Customer ID (Ship-To Party) with a color (part of Cat. Year/Month). I've experienced many customers who make me see RED or feel rather BLUE, but I don't know that I'd want to try and ship them anything;-)

    It is tedious to put all that stuff down on paper, but it has the marvelous side-effect of helping clarify your goals and methods!

    Thanks,

    Tony

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,044

    Re: Fill in the Blanks - VBA

    Also, Please upload a sample of your workbook, not a picture of your data. Pictures are pretty much impossible to edit, and no-one wants to re-type your data for you
    Also, not all members can upload picture files (Company firewalls and stuff) - and, depending on what browser is being used, some pics dont even show up on the forum

    Your workbook should show a small desensitized example of the data you are working with and a manual mockup of the expected results.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Registered User
    Join Date
    07-17-2015
    Location
    Hertfordshire, England
    MS-Off Ver
    Windows 7
    Posts
    11

    Re: Fill in the Blanks - VBA

    thanks for the above comments and apologies that i was not clear on what i wanted.

    tony, if it helps the below is what i want it to look like in the end

    Fill Blanks2.png

    Also, Please upload a sample of your workbook, not a picture of your data
    i would love to as would be a lot easier for me and you but it says the file size exceeds the forum limit of 1000KB :-(

  5. #5
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,044

    Re: Fill in the Blanks - VBA

    If your file is too big, reduce the amount of data. All we need is a small representative sample of your data...inlclude only what is relevant to your question. If necessary, put a dummy workbook together.

    Many membe4rs (muyself included) cannot even see your pic

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this demonstration !

    PHP Code: 
    Sub Demo()
        
    With Cells(1).CurrentRegion
            VA 
    = .Value
            
            
    For R& = 2 To UBound(VA)
                For 
    C& = 1 To 3
                    
    If VA(RC) = "" Then VA(RC) = VA(1C)
                
    Next
                
    For 5 To UBound(VA2)
                    If 
    VA(RC) = "" Then VA(RC) = 0
                Next
            Next
            
            
    .Value VA
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 07-17-2015 at 09:08 PM.

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Try this way without loop !

    PHP Code: 
    Sub Demo2()
        
    With Cells(1).CurrentRegion.Columns
            With 
    .Item("A:C")
                If 
    Application.CountBlank(.CellsThen
                    
    .SpecialCells(xlCellTypeBlanks).FormulaR1C1 "=R[-1]C"
                    
    .Formula = .Value
                End 
    If
            
    End With
            With 
    .Item(5).Resize(, .Count 4)
                If 
    Application.CountBlank(.CellsThen .SpecialCells(xlCellTypeBlanks).Value 0
            End With
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  8. #8
    Registered User
    Join Date
    07-17-2015
    Location
    Hertfordshire, England
    MS-Off Ver
    Windows 7
    Posts
    11

    Re: Fill in the Blanks - VBA

    Hi Marc,

    i tried your code but i got an error.

    All,

    please see attached workbook. Sheet 1 is how it looks to start with and sheet 2 is how i would like it to look after running the macro. It is basically saying, if the cell (A:D) is blank, fill it with the above cell.

    The area in purple (columns A:D) can change. Can be 3 columns, could be 7. Also the amount of columns (Months) can be anywhere from 1 to 100.
    The blue area contains numbers and i would like the empty cells to be filled with a zero.

    I should also say, i would like the new output to be in sheet 2 and to keep sheet 1 as it was.

    i hope this is a better explanation :-)


    many thanks,
    Marc
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    01-16-2014
    Location
    Algiers, Algeria
    MS-Off Ver
    Excel 2003, Excel 2013
    Posts
    33

    Re: Fill in the Blanks - VBA

    Hello There,
    Try this:

    Please Login or Register  to view this content.
    Regards.

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !

    Quote Originally Posted by hillmas83 View Post
    i tried your code but i got an error.
    Which code, which error, how can I guess ?‼
    Both codes work on my side, so issue comes from yours …

    Better is to crystal clear explain your needs at first post !
    Last try, this code based upon your sample workbook works too on my side :

    PHP Code: 
    Sub Demo2a()
        
    Dim Rg As Range

        With Sheet1
    .Cells(1).CurrentRegion
            C 
    Application.Match("Cal. year / month", .Rows(2), 0)
            If 
    IsError(CThen Beep: Exit Sub
            Sheet2
    .UsedRange.Clear
            Application
    .ScreenUpdating False
            
    .Offset(1).Copy Sheet2.Cells(1)
        
    End With

        With Sheet2
            With 
    .Cells(1).CurrentRegion.Rows
                With 
    .Item("2:" & .Count).Columns
                    With 
    .Item(1).Resize(, 1)
                        If 
    Application.CountBlank(.CellsThen
                            
    For Each Rg In .SpecialCells(xlCellTypeBlanks)
                                     
    Rg.Value Rg(0).Value
                            Next
                        End 
    If
                    
    End With
                    With 
    .Item(1).Resize(, .Count C)
                        If 
    Application.CountBlank(.CellsThen .SpecialCells(xlCellTypeBlanks).Value 0
                    End With
                End With
            End With
        End With
    End Sub 
    You should like it, so thanks to click on bottom left star icon « Add Reputation » !


    @ XLRATOR : un peu léger ton code …
    Last edited by Marc L; 07-18-2015 at 08:56 AM. Reason: optimizing …

  11. #11
    Registered User
    Join Date
    01-16-2014
    Location
    Algiers, Algeria
    MS-Off Ver
    Excel 2003, Excel 2013
    Posts
    33

    Re: Fill in the Blanks - VBA

    Hello All, Bonjour MARCL,
    @ XLRATOR : un peu léger ton code …
    All I was thinking it is about, is to replace blank Cell Values by Zeros, at list this is what our guy is asking for by saying:

    i would like a macro in VBA which fills in the blanks in the below report.
    Merci Marc.

  12. #12
    Registered User
    Join Date
    01-16-2014
    Location
    Algiers, Algeria
    MS-Off Ver
    Excel 2003, Excel 2013
    Posts
    33

    Re: Fill in the Blanks - VBA

    Hello All, Bonjour MARCL,
    @ XLRATOR : un peu léger ton code …
    All I was thinking it is about, is to replace blank Cell Values by Zeros, at list this is what our guy is asking for by saying:
    i would like a macro in VBA which fills in the blanks in the below report.
    @Marc, D'aprés ce que je voie, la quéstion du gars n'est jusque là pas trop claire.

    Merci quand même.

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Fill in the Blanks - VBA


    Salut ! (Hi !)

    Its needs was highlighted in post #8 …

    Anyway, could you try my Demo2a code and returns any issue ?
    Merci. (Thanks.)
    Last edited by Marc L; 07-18-2015 at 08:47 AM.

  14. #14
    Registered User
    Join Date
    01-16-2014
    Location
    Algiers, Algeria
    MS-Off Ver
    Excel 2003, Excel 2013
    Posts
    33

    Re: Fill in the Blanks - VBA

    I've tried MARCL's Code, it works fine for me.

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Fill in the Blanks - VBA


    Merci !

    I've just optimize it (post #10) …

  16. #16
    Registered User
    Join Date
    07-17-2015
    Location
    Hertfordshire, England
    MS-Off Ver
    Windows 7
    Posts
    11

    Re: Fill in the Blanks - VBA

    Marc L,

    Thank you for this code. It works...but...when their are more columns before the dates it fills them in with zeros instead of what is in the above cell. The number of columns before the first date can change. Can this be amended?

    many thanks,
    Marc

  17. #17
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: Fill in the Blanks - VBA


    Demo2A already does that ‼ Just read in code how C variable is initialized …

    But can't work against erratic worksheet with no brain logic onboard ! Post a sample workbook to see if any logic inside …
    Last edited by Marc L; 07-18-2015 at 09:10 AM.

  18. #18
    Registered User
    Join Date
    07-17-2015
    Location
    Hertfordshire, England
    MS-Off Ver
    Windows 7
    Posts
    11

    Re: Fill in the Blanks - VBA

    Hi,

    see attached sample workbook. In columns E and F it puts in zeros.
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    01-16-2014
    Location
    Algiers, Algeria
    MS-Off Ver
    Excel 2003, Excel 2013
    Posts
    33

    Re: Fill in the Blanks - VBA

    I've just optimize it (post #10) …
    That's the one I've tried.

  20. #20
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Try this !

    Last last try :

    PHP Code: 
    Sub Demo2b()
        
    Dim Rg As Range

        With Sheet1
    .Cells(1).CurrentRegion
            
    For Each Rg In .Rows(3).Cells
                  
    If Rg.NumberFormat "#,##0" Then C& = Rg.Column: Exit For
            
    Next
                                                
    If 0 Then Beep: Exit Sub
            Sheet2
    .UsedRange.Clear
            Application
    .ScreenUpdating False
            
    .Offset(1).Copy Sheet2.Cells(1)
        
    End With

        With Sheet2
    .Cells(1).CurrentRegion.Rows
            With 
    .Item("2:" & .Count).Columns
                With 
    .Item(1).Resize(, 1)
                    If 
    Application.CountBlank(.CellsThen
                        
    For Each Rg In .SpecialCells(xlCellTypeBlanks)
                                 
    Rg.Value Rg(0).Value
                        Next
                    End 
    If
                
    End With
                With 
    .Item(C).Resize(, .Count 1)
                    If 
    Application.CountBlank(.CellsThen .SpecialCells(xlCellTypeBlanks).Value 0
                End With
            End With
        End With
    End Sub 
    fleche.gif You must like it ! So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 07-18-2015 at 10:01 AM. Reason: last optimizing …

  21. #21
    Registered User
    Join Date
    07-17-2015
    Location
    Hertfordshire, England
    MS-Off Ver
    Windows 7
    Posts
    11

    Re: Fill in the Blanks - VBA

    thank Marc. We are so nearly there. the only issue now is that the data does not start in Cell A1...its actually starts in A31 and there is a lot of stuff above it which i want to ignore. With your code, if there are lines above the data it doesnt start the macro.

  22. #22
    Registered User
    Join Date
    07-17-2015
    Location
    Hertfordshire, England
    MS-Off Ver
    Windows 7
    Posts
    11

    Re: Try this !

    see attached for example of above
    Attached Files Attached Files

  23. #23
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Fill in the Blanks - VBA


    You must give all your needs at once !

    Any other point ? It will be my last mod !

  24. #24
    Registered User
    Join Date
    07-17-2015
    Location
    Hertfordshire, England
    MS-Off Ver
    Windows 7
    Posts
    11

    Re: Fill in the Blanks - VBA

    sorry, no this is it now i promise :-)

  25. #25
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Re: Fill in the Blanks - VBA


    So the mod is beginner level, you could have done it yourself !

    Just amend codeline #4 like this : Sheet1.Cells(30, 1).CurrentRegion

    You must like it ! So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 07-18-2015 at 10:57 AM.

+ 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. fill in the blanks
    By pmugagga in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-07-2013, 10:54 AM
  2. fill in the blanks
    By JAMES GABEL in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-24-2007, 04:59 AM
  3. Fill Blanks
    By VBA Noob in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-20-2006, 05:06 AM
  4. [SOLVED] Fill in the blanks!
    By Bhupinder Rayat in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-02-2006, 12:50 PM
  5. Fill in the blanks!
    By Bhupinder Rayat in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-02-2006, 12:50 PM
  6. Fill in the blanks!!
    By Bhupinder Rayat in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-02-2006, 12:35 PM
  7. [SOLVED] RE: Fill in the blanks
    By Jim Thomlinson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-07-2005, 03:45 PM
  8. [SOLVED] Fill in the blanks
    By BOB in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-31-2005, 03:06 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