+ Reply to Thread
Results 1 to 11 of 11

Macro to to enter value underneath last row in the last column

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Macro to to enter value underneath last row in the last column

    Hi.
    Can anybody modify for me the code below so that the code will add the total sum of the last column underneath the last row? The code below works only for the last row in column "E", but I want the code to be flexible and detect the last column as well. Please note that my code also has an "auto fill down" which I would like to put in the last column as well.
    Thanks in advance.


    Sub Last_row_last_column()
    
    Dim ws As Worksheet:    Set ws = Sheets("Sheet1")
    Dim LR As Long, rCell As Long
        
    'misc
    LR = ws.Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    'Part A
    If IsEmpty(ws.Range("D18")) Then
        ws.Range("D18").Formula = ""
        ws.Range("E18").Formula = ""
    End If
    
    ws.Range("D18:E18").Resize(LR - 18 + 1, 2).FillDown
    
    'Part B
    For rCell = LR To 18 Step -1
        If ws.Range("C" & rCell).Value = 0 Then
            ws.Range("C" & rCell).EntireRow.Delete Shift:=xlUp
        ElseIf ws.Range("C" & rCell).Value = "" Then
            ws.Range("C" & rCell).EntireRow.Delete Shift:=xlUp
        End If
    Next rCell
    
    With ws.Range("E" & LR + 1)
        .Formula = "=SUM(E18:E" & LR & ")"
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Font.Bold = True
        .Font.Name = "Cambria"
        .Font.Size = 8
    End With
    
    'Clean Up
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by kosherboy; 02-03-2014 at 11:04 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Macro to to enter value underneath last row in the last column

    I tried working with the code to the best of my ability but the code doesn't work:-(

    I pasted my entire code including scripts that I originally omitted.

    I would really appreciate if anyone can shed some light on this issue.

    Thank you.

    Option Explicit
    Sub EXECUTE_MULTIPLE_TASKS()
    
    Dim ws As Worksheet:    Set ws = Sheets("Sheet1")
    Dim lRow As Long, lCol As Long, rCell As Long
    
        
    'misc
    lRow = .Range("A" & .Rows.Count).End(xlUp).Row
    lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    'Part A
    If IsEmpty(ws.Range(lCol - 2)) Then
        ws.Range(lCol - 2).Formula = ""
        ws.Range(lCol - 1).Formula = ""
    End If
    
    ws.Range(lCol - 2, lCol - 2).Resize(lRow - 18 + 1, 2).FillDown
    
    'Part B
    For rCell = lRow To 18 Step -1
        If ws.Range(lCol - 3 & rCell).Value = 0 Then
            ws.Range(lCol - 3 & rCell).EntireRow.Delete Shift:=xlUp
        ElseIf ws.Range(lCol - 3 & rCell).Value = "" Then
            ws.Range(lCol - 3 & rCell).EntireRow.Delete Shift:=xlUp
        End If
    Next rCell
    
    
    'Part C
    Rows("18:3000").Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range( _
            "A18:A1161"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A18:AN1161")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Cells(1, 1).Select
    
    'Part D
    lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
    With ws.Range("A" & lRow + 1, lCol & lRow + 1)
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Interior.Color = RGB(242, 242, 242)
        
    End With
    
    With ws.Range(lCol - 4 & lRow + 1)
        .Value = "Total Qty"
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Font.Bold = True
        .Font.Name = "Cambria"
        .Font.Size = 8
    End With
    
    With ws.Range(lCol - 1 & lRow + 1)
        .Value = "Total Cost"
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Font.Bold = True
        .Font.Name = "Cambria"
        .Font.Size = 8
    End With
    
    With ws.Range(lCol - 3 & lRow + 1)
        .Formula = "=SUM(lCol - 3 & rCell & lRow & )"
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Font.Bold = True
        .Font.Name = "Cambria"
        .Font.Size = 8
    End With
    
    With ws.Range("E" & lRow + 1)
        .Formula = "=SUM(lCol & rCell & lRow & )"
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Font.Bold = True
        .Font.Name = "Cambria"
        .Font.Size = 8
    End With
    
    With ws.Range("B16")
        .Formula = "=SUM(lCol & rCell & lRow & )"
    End With
    
    'Part E
    ws.PageSetup.PrintArea = "$A$1" & lCol & lRow + 1
    
    
    'Clean Up
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
    End Sub

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to to enter value underneath last row in the last column

    Maybe this can help?

    Sub kosherboy()
    Dim lr As Long
    Dim lc As Long
    lr = ActiveSheet.UsedRange.Rows.count + 1
    lc = ActiveSheet.UsedRange.Columns.count
    Cells(lr, lc).Formula = "=SUM(" & Cells(2, lc).Address & ":" & Cells(lr - 1, lc).Address & ")"
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Macro to to enter value underneath last row in the last column

    Hi. Thanks for responding.
    This code is not summing up the last row in the last column

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to to enter value underneath last row in the last column

    It works for me. Can you attach a sample file with some data?

  6. #6
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Macro to to enter value underneath last row in the last column

    Here you go Row & Columns.xlsx. So basically what I would like is that the last 2 columns should be "filled down" with the formula in the last 2 columns in row "18", and then auto sum the last column in the last row. The reason for this is because there will be times where columns will be inserted however the last two column will always remain the same as far as formula wise.
    Last edited by kosherboy; 02-03-2014 at 05:37 PM.

+ 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] Macro to count unique values in a column, enter it in next column, then delete duplicates
    By pmorisse in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-25-2013, 03:27 PM
  2. VBA Halp! Sort Columns by date; Insert/add Data Underneath the Cell in Column B
    By goradiar in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-06-2012, 09:10 AM
  3. Replies: 0
    Last Post: 05-17-2012, 12:59 PM
  4. Vertical to horizontal one column and underneath
    By vinaynaran in forum Excel General
    Replies: 2
    Last Post: 10-14-2009, 01:02 PM
  5. macro to copy a column and put it underneath another
    By cadalist in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-28-2007, 06:59 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