Results 1 to 4 of 4

Tidying up code

Threaded 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

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