+ Reply to Thread
Results 1 to 7 of 7

How does this VBA code work?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-08-2006
    Location
    UK
    MS-Off Ver
    2013
    Posts
    204

    Exclamation How does this VBA code work?

    Some kind soul provided me with this sample code which will populate a userform omitting rows where a cell is blank in column C (have attached the sheet).

    I'm trying to adapt it to a sheet where there are 22 columns (A - Q), and it is if a cell in column M is blank that the user form should show.

    Unfortunately I don't seem to understand the syntax of the code. I think the "Count, 3" bit is throwing me off.

    If someone could quickly explain it to me I'd be most appreciative.

    Private Sub UserForm_Initialize()
    Dim xCell As Range
    For Each cell In Range("C2", Cells(Rows.Count, 3).End(xlUp))
    If Not IsEmpty(cell) Then Me.CB_RefNo.AddItem cell.Offset(0, -2).Value
    Next
    
    End Sub
    Attached Files Attached Files
    Last edited by wonderdunder; 04-02-2012 at 11:46 AM.

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: How does this VBA code work? Desperately need help

    For Each cell In Range("M2", Cells(Rows.Count, "M").End(xlUp))

  3. #3
    Forum Contributor
    Join Date
    02-08-2006
    Location
    UK
    MS-Off Ver
    2013
    Posts
    204

    Re: How does this VBA code work?

    offending 2 words removed. Can I see the "hidden" responses please or shall I go to the the naughty corner?

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: How does this VBA code work?

    For Each cell In Range("M2", Cells(Rows.Count, "M").End(xlUp))
    is what I said before...

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How does this VBA code work?

    The title doesn't really describe the problem - something like explain code that loads userform.

    However, there's absolutely no point in looping through the range, just refer to all the non blank cells using Specialcells

    Private Sub UserForm_Initialize()
        Dim rRng As Range, rCl As Range
        With Sheet1
            On Error Resume Next
            Set rRng = .Range(.Cells(2, 3), .Cells(.Rows.Count, _
                3).End(xlUp)).SpecialCells(xlCellTypeConstants)
            On Error GoTo 0
            For Each rCl In rRng
                Me.CB_RefNo.AddItem Format(rCl.Value, "long date")
            Next rCl
        End With
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Forum Contributor
    Join Date
    02-08-2006
    Location
    UK
    MS-Off Ver
    2013
    Posts
    204

    Re: How does this VBA code work?

    Roy - many thanks. I'm trying to adopt this code so that the drop down box will only show data in column C if the cell in column M of the same row is not blank, but I do not understand the syntax of the code (as per my first post). Can you or someone else explain the code or if not provide the code amendments that will do as I need it to?

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How does this VBA code work?

    Here'an explanation of my coe
    Private Sub UserForm_Initialize()
    'set the variables
        Dim rRng As Range, rCl As Range
        With Sheet1 '< - use Sheet 1
            On Error Resume Next '<- need to have errror handler in case no cells meet the SpecvialCells criteria
    		'R1C1 format Row number, Column number
    		'set the range from C2 to last used cell in C
    		'Rows.Count will be the last cell in the column & check back to the top
    		'to finf the next empty cell
    		'SpecialCells(xlCellTypeConstants) will pick only cells containing Constants
            Set rRng = .Range(.Cells(2, 3), .Cells(.Rows.Count, _
                3).End(xlUp)).SpecialCells(xlCellTypeConstants)
            On Error GoTo 0
    		'loop through those cells & format & add to ComboBox
            For Each rCl In rRng
                Me.CB_RefNo.AddItem Format(rCl.Value, "long date")
            Next rCl
        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)

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