+ Reply to Thread
Results 1 to 4 of 4

VBA to add a border to a report and sort a column

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    09-09-2009
    Location
    Liverpool, England
    MS-Off Ver
    Excel 2021
    Posts
    1,013

    VBA to add a border to a report and sort a column

    Hi,

    I've been building a report in which millz has been instrumental in helping me deliver. Link

    The final thing I would like to do is to add a border around the outside of the report and sort the 'charge' column (K) descending. I thought I would be able to obtain the code for this by using the macro wizard but this is returning a lot of code, most of which I imagine is unneccessary.

    Please note that this report will be used regularly for various searches so I need the code to remove the old border and reapply it to the new data each time a search is run.

    Thanks in advance,

    Snook
    Attached Files Attached Files

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA to add a border to a report and sort a column

    Hi Snook,
    try changing this line
    ws1.Range("E3:K" & Rows.Count).ClearContents
    to this
    ws1.Range("E3").CurrentRegion.Offset(1).Clear
    and add these lines
    ...
        End If
    Next
    Columns("E:K").AutoFit
    With Range("E2:K" & Cells(Rows.Count, 5).End(xlUp).Row)
        .Sort Key1:=.Cells(1, 7), Order1:=xlDescending, Header:=xlYes
        .Offset(1).Resize(.Rows.Count - 1).Borders.LineStyle = xlContinuous
    End With
    Application.ScreenUpdating = True
    End Sub

  3. #3
    Valued Forum Contributor
    Join Date
    09-09-2009
    Location
    Liverpool, England
    MS-Off Ver
    Excel 2021
    Posts
    1,013

    Re: VBA to add a border to a report and sort a column

    Hi nilem,

    That adds criss cross borders across all cells in the data range i'd prefer just an outside border. Is that possible?

    Thanks

    Snook

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA to add a border to a report and sort a column

    ...
    Columns("E:K").AutoFit
    With Range("E2:K" & Cells(Rows.Count, 5).End(xlUp).Row)
        .Sort Key1:=.Cells(1, 7), Order1:=xlDescending, Header:=xlYes
        With .Offset(1).Resize(.Rows.Count - 1).Borders
            .Item(xlEdgeLeft).LineStyle = xlContinuous
            .Item(xlEdgeBottom).LineStyle = xlContinuous
            .Item(xlEdgeRight).LineStyle = xlContinuous
        End With
    End With
    Application.ScreenUpdating = True
    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] Excel sort alphabetically doesn't move cell border
    By Marco-Kun in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-25-2013, 11:13 AM
  2. Excel sort alphabetically doesn't move cell border
    By Marco-Kun in forum Excel General
    Replies: 0
    Last Post: 04-18-2013, 08:21 AM
  3. [SOLVED] Macro to sort a list with duplicates and add bottom border for last duplicate
    By kuntilfusk7 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-10-2012, 09:44 AM
  4. Replies: 1
    Last Post: 10-22-2009, 05:26 PM
  5. VBA: Sort and report to a form
    By D3Pratt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-30-2009, 10:17 AM

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