+ Reply to Thread
Results 1 to 4 of 4

Counting cells in a column to add formulas to that equal number of cells

Hybrid View

  1. #1
    Registered User
    Join Date
    01-14-2009
    Location
    Earth
    MS-Off Ver
    2007
    Posts
    43

    Counting cells in a column to add formulas to that equal number of cells

    What I am trying to do is count how many cells have data and then drop in formulas in some cells to the right, according to how many cells I have counted as NOT being empty. Attached is a picture that may more clearly explain what I am trying to do.

    There must be a better way to do this... because what I have isn't working this is where my macro is running into problems:

    ...
        ' COUNT THE NUMBER OF CUSTOMERS AND SAVE AS A VARIABLE
        Range("B2").Select
        Do While IsEmpty(ActiveCell.Value) Or ActiveCell.Value = ""
            custCount = custCount + 1
            n = n + 1
            ActiveCell.Offset(1, 0).Select
        Loop
        ' MsgBox "n = " & n & " ..... custCount = " & custCount, vbInformation, "VARIABLES AFTER WHILE LOOP"
        ' MsgBox "C" & custCount, vbInformation, "Test"
        
        ' FILL CARRIER CELLS WITH FORUMLAS AND QUANTITY CELL WITH 1s
        For n = 0 To custCount
            Range("C2", "C" & custCount).Select
            ActiveCell.FormulaR1C1 = "1"
            n = n + 1
        Next n
    Attached Images Attached Images
    Last edited by flipjarg; 07-11-2012 at 11:30 AM. Reason: solved

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Counting cells in a column to add formulas to that equal number of cells

    Maybe something like:

    Sub FindItemsInColumn()
        Dim nRow As Long
        Dim nLastRow As Long
        
        ' Find last row with data in column B
        nLastRow = Cells(Rows.Count, "B").End(xlUp).Row
        
        For nRow = 2 To nLastRow
            Range("C2:C" & nRow).FormulaR1C1 = "1"
        Next nRow
    End Sub

  3. #3
    Registered User
    Join Date
    01-14-2009
    Location
    Earth
    MS-Off Ver
    2007
    Posts
    43

    Re: Counting cells in a column to add formulas to that equal number of cells

    Hey, this works great! Now I just need to do some reading so I understand how it works!

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Counting cells in a column to add formulas to that equal number of cells

    nLastRow = Cells(Rows.Count, "B").End(xlUp).Row
    In an Excel Spread sheet use Ctrl + an arrow key. Up, down, left, and right.

    Rows.Count is the last row in a spread sheet.

    Cells(Rows.Count, "B") is the last cell in column B.

    End works like Ctrl+ arrow key.

    End(xlUp) = Ctrl + up arrow key.

    So Cells(Rows.Count, "B").End(xlUp) is the last cell in column B with data.

    .Row gives the row where that cell is located.

+ 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