+ Reply to Thread
Results 1 to 4 of 4

Tidying up code

Hybrid View

  1. #1
    Registered User
    Join Date
    01-10-2017
    Location
    Wellington, New Zealand
    MS-Off Ver
    2010
    Posts
    15

    Angry Tidying up code

    Hi,

    I have copied together some code on the internet to practice some VBA. The code below gets the job done but it is very long and I am pretty sure it can be cut down. I have tried to shorten it but I have not a clue. I have this sum line of code that adds up fields that are returned when I select a certain sales rep.

    to shorten the code I need to do something like this (doesn't work) so I can format the sheet in code rather than repeating it for J,K,L,N,M,O,P and Q.

    [ThisWorkbook.Sheets("Sheet1").Range("J,K,L,N,M,O,P,Q" & lastrow + 1)]



    If someone could help me be more succinct that would be great!!

    spreadsheet attached if that helps.

    Here is the code that I have. It returns the values from a dataset I have and returns a little report based on the rep name I select. I then try and add a total line totaling the numbers and putting a dash in where there aren't numbers.

    
    Sub finddata()
    
    Dim repname As String
    Dim finalrow As Integer
    Dim i As Integer
    
    Sheets("Sheet1").Range("J6:Q50").ClearContents
    Sheets("Sheet1").Range("J6:Q50").Font.Bold = False
    Sheets("Sheet1").Range("J6:Q50").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        Selection.Borders(xlEdgeLeft).LineStyle = xlNone
        Selection.Borders(xlEdgeTop).LineStyle = xlNone
        Selection.Borders(xlEdgeBottom).LineStyle = xlNone
        Selection.Borders(xlEdgeRight).LineStyle = xlNone
        Selection.Borders(xlInsideVertical).LineStyle = xlNone
        Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    
    repname = Sheets("Sheet1").Range("L3").Value
    finalrow = Sheets("Sheet1").Range("C5000").End(xlUp).Row
    
    For i = 3 To finalrow
        If Cells(i, 3) = repname Then
        Range(Cells(i, 1), Cells(i, 7)).Copy
        Range("K50").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
        End If
        
    Next i
    
    lastrow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 15).End(xlUp).Row
    
    ThisWorkbook.Sheets("Sheet1").Range("J" & lastrow + 1) = "Total"
    ThisWorkbook.Sheets("Sheet1").Range("J" & lastrow + 1).Font.Bold = True
    
    With ThisWorkbook.Sheets("Sheet1").Range("J" & lastrow + 1).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With ThisWorkbook.Sheets("Sheet1").Range("J" & lastrow + 1).Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThick
        End With
    
    ThisWorkbook.Sheets("Sheet1").Range("o" & lastrow + 1) = WorksheetFunction.Sum(ThisWorkbook.Sheets("Sheet1").Range("O6:O" & lastrow))
    ThisWorkbook.Sheets("Sheet1").Range("o" & lastrow + 1).Font.Bold = True
    ThisWorkbook.Sheets("Sheet1").Range("O" & lastrow + 1).VerticalAlignment = xlCenter
    
    With ThisWorkbook.Sheets("Sheet1").Range("O" & lastrow + 1).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With ThisWorkbook.Sheets("Sheet1").Range("O" & lastrow + 1).Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThick
        End With
    
    ThisWorkbook.Sheets("Sheet1").Range("K" & lastrow + 1) = "-"
    ThisWorkbook.Sheets("Sheet1").Range("K" & lastrow + 1).HorizontalAlignment = xlCenter
    ThisWorkbook.Sheets("Sheet1").Range("K" & lastrow + 1).VerticalAlignment = xlCenter
    ThisWorkbook.Sheets("Sheet1").Range("K" & lastrow + 1).Font.Bold = True
    
    With ThisWorkbook.Sheets("Sheet1").Range("K" & lastrow + 1).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With ThisWorkbook.Sheets("Sheet1").Range("K" & lastrow + 1).Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThick
        End With
    
    ThisWorkbook.Sheets("Sheet1").Range("L" & lastrow + 1) = "-"
    ThisWorkbook.Sheets("Sheet1").Range("L" & lastrow + 1).HorizontalAlignment = xlCenter
    ThisWorkbook.Sheets("Sheet1").Range("L" & lastrow + 1).VerticalAlignment = xlCenter
    ThisWorkbook.Sheets("Sheet1").Range("L" & lastrow + 1).Font.Bold = True
    
    With ThisWorkbook.Sheets("Sheet1").Range("L" & lastrow + 1).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With ThisWorkbook.Sheets("Sheet1").Range("L" & lastrow + 1).Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThick
        End With
    
    ThisWorkbook.Sheets("Sheet1").Range("M" & lastrow + 1) = "-"
    ThisWorkbook.Sheets("Sheet1").Range("M" & lastrow + 1).HorizontalAlignment = xlCenter
    ThisWorkbook.Sheets("Sheet1").Range("M" & lastrow + 1).VerticalAlignment = xlCenter
    ThisWorkbook.Sheets("Sheet1").Range("M" & lastrow + 1).Font.Bold = True
    
    With ThisWorkbook.Sheets("Sheet1").Range("M" & lastrow + 1).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With ThisWorkbook.Sheets("Sheet1").Range("M" & lastrow + 1).Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThick
        End With
    
    ThisWorkbook.Sheets("Sheet1").Range("N" & lastrow + 1) = "-"
    ThisWorkbook.Sheets("Sheet1").Range("N" & lastrow + 1).HorizontalAlignment = xlCenter
    ThisWorkbook.Sheets("Sheet1").Range("N" & lastrow + 1).VerticalAlignment = xlCenter
    ThisWorkbook.Sheets("Sheet1").Range("N" & lastrow + 1).Font.Bold = True
    
    With ThisWorkbook.Sheets("Sheet1").Range("N" & lastrow + 1).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With ThisWorkbook.Sheets("Sheet1").Range("N" & lastrow + 1).Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThick
        End With
    
    ThisWorkbook.Sheets("Sheet1").Range("P" & lastrow + 1) = WorksheetFunction.Sum(ThisWorkbook.Sheets("Sheet1").Range("P6:P" & lastrow))
    ThisWorkbook.Sheets("Sheet1").Range("P" & lastrow + 1).Font.Bold = True
    ThisWorkbook.Sheets("Sheet1").Range("P" & lastrow + 1).VerticalAlignment = xlCenter
    
    With ThisWorkbook.Sheets("Sheet1").Range("P" & lastrow + 1).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With ThisWorkbook.Sheets("Sheet1").Range("P" & lastrow + 1).Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThick
        End With
    
    ThisWorkbook.Sheets("Sheet1").Range("Q" & lastrow + 1) = WorksheetFunction.Sum(ThisWorkbook.Sheets("Sheet1").Range("Q6:Q" & lastrow))
    ThisWorkbook.Sheets("Sheet1").Range("Q" & lastrow + 1).Font.Bold = True
    ThisWorkbook.Sheets("Sheet1").Range("Q" & "R" & "S" & lastrow + 1).VerticalAlignment = xlCenter
    
    With ThisWorkbook.Sheets("Sheet1").Range("Q" & lastrow + 1).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With ThisWorkbook.Sheets("Sheet1").Range("Q" & lastrow + 1).Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThick
        End With
    
    
    Columns("J:Q").EntireColumn.AutoFit
    
        Application.CutCopyMode = False
        Range("L3").Select
    
    End Sub
    Thanks
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Tidying up code

    See if this is how you wanted.
    Used AdvancedFilter instead of loop.
    Sub finddata()
        Application.ScreenUpdating = False
        With Sheets("Sheet1")
            .Range("J5:Q50").Clear
            .Range("i2").Formula = "=c3=l$3"
            With .Range("a2").CurrentRegion
                .AdvancedFilter 2, .Parent.Range("i1:i2"), .Parent.Range("k5")
            End With
            With .Range("k5").CurrentRegion
                .Borders.LineStyle = xlNone
                .Interior.ColorIndex = xlNone
                .Offset(1).Font.Bold = False
                With .Rows(.Rows.Count + 1)
                    With .Offset(, -1).Resize(, .Columns.Count + 1)
                        .HorizontalAlignment = xlCenter
                        .Font.Bold = True
                        .Borders(3).Weight = 2
                        .Borders(9).LineStyle = xlDouble
                        .Cells(1).Resize(, 5).Value = Array("Total", "-", "-", "-", "-")
                        .Cells(, 6).Resize(, 3).FormulaR1C1 = "=sum(r6c:r[-1]c)"
                        .EntireColumn.AutoFit
                    End With
                End With
            End With
            .Range("i2").Clear
        End With
        Application.ScreenUpdating = True
    End Sub

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,447

    Re: Tidying up code

    Hi trobb,

    This problem can be done without any VBA or advanced filters. See the attached that gives a Pivot Table answer. You need to format the Pivot Table to not show subtotals and in Tabular format. You then simply filter by the Rep. See the attached. No VBA needed!! Note: I guess I'm just lazy and want to use the easy method for doing these things, and I do write VBA.

    PT instead of VBA.xlsm
    Last edited by MarvinP; 05-07-2017 at 10:32 AM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Tidying up code

    trobb

    Thanks for the rep.

+ 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. Tidying up VBA code
    By MrBibby in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2013, 07:29 PM
  2. [SOLVED] Can anyone advise on tidying up code?
    By whitieklf in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-30-2013, 09:55 AM
  3. tidying up code
    By tryer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-14-2011, 10:03 AM
  4. code tidying
    By stevesunfold in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 12-26-2008, 05:23 PM
  5. Tidying/ Simplifying Code
    By bomberchia in forum Excel General
    Replies: 6
    Last Post: 11-29-2008, 06:16 AM
  6. Help with tidying up code please
    By Andy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-10-2005, 01:00 AM
  7. tidying up code and refering macros
    By philbennison in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-12-2005, 08:32 AM

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