Results 1 to 7 of 7

VBA Format Multiple Ranges

Threaded View

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Format Multiple Ranges

    Hi, I wonder whether someone may be able to help me please.

    I'm relatively new to VB, so a lot of my code is very long winded, an example of which is below:

    '***First Range
        LastRow = Cells(Rows.count, "B").End(xlUp).Row
            If LastRow >= StartRow Then
                With Range("B7:D" & LastRow)
                    With .Font
                        .Name = "Lucida Sans"
                        .Size = 10
                    End With
                End With
            End If
    
        With Range("D7:D" & LastRow)
            .NumberFormat = "#,##0.00"
            .HorizontalAlignment = xlCenter
        End With
        
        LastRow = Range("B" & Rows.count).End(xlUp).Row
            For i = 7 To LastRow
                 With Range("C" & i)
                    X = Split(.Value, ",")
                    If UBound(X) = 1 Then .Value = Trim(X(1)) & " " & Trim(X(0))
                 End With
            Next i
    
        With Range("B7:D" & LastRow)
            .sort Key1:=.Cells(1, 1), Order1:=xlAscending
        End With
        
    '***Second Range
    
        LastRow = Cells(Rows.count, "G").End(xlUp).Row
            If LastRow >= StartRow Then
                With Range("G7:H" & LastRow)
                    With .Font
                        .Name = "Lucida Sans"
                        .Size = 10
                    End With
                End With
            End If
        
         With Range("H7:H" & LastRow)
            .NumberFormat = "#,##0.00"
            .HorizontalAlignment = xlCenter
        End With
    
        LastRow = Range("G" & Rows.count).End(xlUp).Row
            For i = 7 To LastRow
                 With Range("G" & i)
                    X = Split(.Value, ",")
                    If UBound(X) = 1 Then .Value = Trim(X(1)) & " " & Trim(X(0))
                 End With
            Next i
    
        With Range("G7:H" & LastRow)
            .sort Key1:=.Cells(1, 1), Order1:=xlAscending
        End With
    The code applies formatting to two ranges within a given sheet in my workbook, but as I mentioned at the start, it's messy and 'long winded'.

    I just wondered whether someone with more experience, could possibly look at this please and offer some guidance on how I may be able to streamline the code.

    Many thanks and kind regards
    Last edited by hobbiton73; 11-21-2013 at 04:30 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 03-19-2013, 07:03 PM
  2. Compiling multiple ranges of cells from multiple sheets into one grand table ?
    By teacher_rob in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-24-2012, 11:42 AM
  3. Replies: 0
    Last Post: 10-15-2012, 07:24 AM
  4. Replies: 8
    Last Post: 01-07-2011, 07:36 AM
  5. Replies: 4
    Last Post: 11-22-2005, 01:30 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