+ Reply to Thread
Results 1 to 7 of 7

VBA Format Multiple Ranges

Hybrid 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.

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: VBA Format Multiple Ranges

    You can replace:
    With Range("B7:D" & LastRow)
                    With .Font
                        .Name = "Lucida Sans"
                        .Size = 10
                    End With
                End With
    with
    With Range("B7:D" & LastRow).font
                        .Name = "Lucida Sans"
                        .Size = 10
                End With
    and

    With Range("B7:D" & LastRow)
            .sort Key1:=.Cells(1, 1), Order1:=xlAscending
        End With
    with
    Range("B7:D" & LastRow).sort Key1:=.Cells(1, 1), Order1:=xlAscending

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

    Re: VBA Format Multiple Ranges

    Hi @yudlugar, thank you very much for taking the time to reply to my post and the guidance.

    May I ask, is there a way in which I could streamline the coede any further.

    Many thanks and kind regards

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: VBA Format Multiple Ranges

    If you upload a workbook with a description of what the code is supposed to do, and the code in full. I could try and write it from scratch? I'd expect I would aim up with something very similar to what you have though.

    That said, you don't need to do the ranges separately. For example, instead of:
    Range("A1").interior.colorindex = 3
    Range("A2").interior.colorindex = 3
    I could do:
    Range("A1,A2").interior.colorindex = 3
    If you are unsure of the exact way to build the string to select the different ranges, you could always use union:
    Union(Range("A1"),Range("A2")).interior.colorindex = 3

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

    Re: VBA Format Multiple Ranges

    Hi @yudlugar, thank you for your continued help with this, and my apologies for not replying sooner.

    Your offer of re-writing my code is very generous, but I wouldn't want to put you to anymore trouble. I would also like to see this thorugh because I am very keen to gain my knowledge and confidence in writing the code.

    Your last post was very inetresting because I wasn't aware of the
    Range ("A1,A2")
    method of writing for multiple ranges.

    All the best and kind regards

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: VBA Format Multiple Ranges

    for info:
    Range("A1,C3")
    selects cells A1 and C3
    Range("A1","C3")
    selects cells a1,a2,a3,b1,b2,b3,c1,c2,c3

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

    Re: VBA Format Multiple Ranges

    Hi @yudlugar, thank you for coming back to me with this. This is extremely helpful.

    All the best and kind regards

+ 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. 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