+ Reply to Thread
Results 1 to 7 of 7

Setting range to last row in a table

Hybrid View

  1. #1
    Registered User
    Join Date
    09-06-2018
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    24

    Setting range to last row in a table

    Hi everyone,

    I'm a complete novice at excel and vba. If you see the attached worksheet, my goal was to have a code that would fill each blank cell in columns A and B with the sum of the cells before it, until it reached another blank cell. I searched online and have found a code that works, see below, however it is a defined range. I will be inputting new data daily and therefore need it to automatically adjust the range to the last row in the table. I've searched the internet for a couple of weeks now trying to find a solution that works to no avail. My current code is:

    Sub PickingTotals()
        Dim myCell      As Range
        Dim currentSum  As Double
        For Each myCell In Worksheets("ALL DATA").Range("a2:a287,b2:b287")
            If myCell = vbNullString Then
                myCell = currentSum
                myCell.Interior.Color = 16777215
                currentSum = 0
            Else
                currentSum = currentSum + myCell
            End If
        Next myCell
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Setting range to last row in a table

    Hi,

    Try this code which I've commented to help you understand what's happening:

    Public Sub FillBlankRows()
    
    Dim colSum(1) As Double
    Dim lastRow As Long
    Dim thisRow As Long
    Dim thisCol As Long
    
    ' Find the last row
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    ' Set the current column sums to zero
    colSum(0) = 0
    colSum(1) = 0
    
    ' Process all rows
    For thisRow = 2 To lastRow
        ' Process both columns
        For thisCol = 1 To 2
            ' Is this row blank?
            If Cells(thisRow, thisCol).Value = "" Then
                ' Yes - put in the running total and reset the total to zero
                Cells(thisRow, thisCol).Value = colSum(thisCol - 1)
                colSum(thisCol - 1) = 0
            Else
                ' No - add this column value to the running total
                colSum(thisCol - 1) = colSum(thisCol - 1) + Cells(thisRow, thisCol).Value
            End If
        Next thisCol
    Next thisRow
    
    End Sub
    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Registered User
    Join Date
    09-06-2018
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    24

    Re: Setting range to last row in a table

    Thank you for your quick reply.

    I've copied it over into my spreadsheet, and changed this line
     For thisCol = 1 To 2
    to
     For thisCol =25 To 26
    as that is where the columns are on my full sheet. When I run the code, it says error "9", Subscript out of range and highlights this column
    colSum(thisCol - 1) = colSum(thisCol - 1) + Cells(thisRow, thisCol).Value
    .

    Is there anything else I should change?

    Thank you!

  4. #4
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Setting range to last row in a table

    Hehe. Change all "thisCol - 1" to be "thisCol - 25" then

    WBD

  5. #5
    Registered User
    Join Date
    09-06-2018
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    24

    Re: Setting range to last row in a table

    Worked an absolute treat!!! Thank you so much, the amount of time I've spent trying to work it out myself is unbelievable haha.

    Thanks again!

  6. #6
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Setting range to last row in a table

    No problem. Remember to mark the thread as solved and, if you're so inclined, help me build my reputation :D

    WBD

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Setting range to last row in a table

    Sub PickingTotals1()
        Dim myCell      As Range
        Dim currentSum  As Double
         Dim Rng As Range
         
        With Worksheets("ALL DATA")
            Set Rng = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row & ", B2:B" & .Range("A" & .Rows.Count).End(xlUp).Row)
            For Each myCell In Rng
                If myCell = vbNullString Then
                    myCell = currentSum
                    myCell.Interior.Color = 16777215
                    currentSum = 0
                Else
                    currentSum = currentSum + myCell
                End If
            Next myCell
        
        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. [SOLVED] Setting range located directly beneath a table
    By ddoctor in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-20-2017, 05:56 PM
  2. Replies: 2
    Last Post: 09-01-2017, 05:43 PM
  3. How to stop a table from being deleted when setting a range?
    By Mutak in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-12-2016, 09:17 AM
  4. Setting a range of cells, within a table, from text cells
    By sligh in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-14-2015, 04:40 PM
  5. Replies: 15
    Last Post: 09-10-2013, 05:31 AM
  6. Replies: 11
    Last Post: 07-15-2012, 04:08 PM
  7. Setting Range in Pivot Table
    By Dhiraj in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 08-14-2005, 09:05 PM

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