+ Reply to Thread
Results 1 to 11 of 11

Format Cells between empty rows until there is no more data

Hybrid View

edebraal Format Cells between empty... 06-21-2013, 10:49 AM
tehneXus Re: Format Cells between... 06-21-2013, 04:37 PM
edebraal Re: Format Cells between... 06-21-2013, 05:30 PM
tehneXus Re: Format Cells between... 06-21-2013, 05:51 PM
edebraal Re: Format Cells between... 06-21-2013, 06:04 PM
tehneXus Re: Format Cells between... 06-21-2013, 06:10 PM
edebraal Re: Format Cells between... 07-31-2013, 04:30 PM
edebraal Re: Format Cells between... 06-21-2013, 06:49 PM
tehneXus Re: Format Cells between... 08-17-2013, 09:13 AM
edebraal Re: Format Cells between... 09-12-2013, 04:03 PM
Fotis1991 Re: Format Cells between... 09-13-2013, 02:38 AM
  1. #1
    Registered User
    Join Date
    06-20-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 365 ProPlus
    Posts
    15

    Format Cells between empty rows until there is no more data

    I am working with a macro that sorts my data. I would like to add VBA code to format the boundary of data between the column range of A:H and between the empty rows. I have attached the example of the finished look I am trying to get and what I start with. Any help is greatly appreciated. Thanks!

    -E.D.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Format Cells between empty rows until there is no more data

    Hi and welcome to the forums,

    try this:
    Option Explicit
    
    Sub test()
        Dim xlRng As Range
        Dim aBorders: aBorders = Array(xlMedium, xlMedium, xlMedium, xlMedium, xlThin, xlThin)
        Dim sAddr As String
        Dim i As Byte
        On Error GoTo ErrorHandler
        Application.ScreenUpdating = False
        With Worksheets("Start")
            Set xlRng = .Columns(1).Find(What:="*", After:=.Cells(.Rows.Count, 1), LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
            If Not xlRng Is Nothing Then
                sAddr = xlRng.Address
                Do
                    With xlRng.CurrentRegion
                        .WrapText = True
                        .Borders(xlDiagonalDown).LineStyle = xlNone
                        .Borders(xlDiagonalUp).LineStyle = xlNone
                        For i = 7 To 12
                            With .Borders(i)
                                .LineStyle = xlContinuous
                                .ColorIndex = xlAutomatic
                                .TintAndShade = 0
                                .Weight = aBorders(i - 7)
                            End With
                        Next i
                    End With
                    Set xlRng = .Columns(1).Find(What:=vbNullString, After:=xlRng, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
                    xlRng.RowHeight = 10
                    Set xlRng = .Columns(1).Find(What:="*", After:=xlRng, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
                Loop While Not xlRng Is Nothing And xlRng.Address <> sAddr And xlRng.Row <> 1
            End If
        End With
        
    Proc_Exit:
        Application.ScreenUpdating = True
        Exit Sub
    ErrorHandler:
        MsgBox Err.Number & ": " & Err.Description, vbCritical + vbOKOnly, "Error"
        Resume Proc_Exit
    End Sub
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Registered User
    Join Date
    06-20-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 365 ProPlus
    Posts
    15

    Re: Format Cells between empty rows until there is no more data

    I put your code at the end of what I am using and I get this error:
    Compile error:
    Duplicate declaration in current scope.

    The "i As Byte" in the "Dim i As Byte" line is highlighted.

    I have this code above what you have.
        Dim r As Long, mcol As String, i As Long
        ' find last used cell in Column B
        r = Cells(Rows.Count, "B").End(xlUp).Row
        ' get value of  last used cell in column B
        mcol = Cells(r, 2).Value
        ' insert rows by looping from bottom
        For i = r To 2 Step -1
        If Cells(i, 2).Value <> mcol Then
        mcol = Cells(i, 2).Value
        Rows(i + 1).Insert
        End If
        Next i
    Last edited by edebraal; 06-21-2013 at 06:45 PM.

  4. #4
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Format Cells between empty rows until there is no more data

    Hi,

    The code is not intended to be pasted below other code, especially when you don't know whats happening there, use it as a separate sub by calling it at the end of your main procedure.

  5. #5
    Registered User
    Join Date
    06-20-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 365 ProPlus
    Posts
    15

    Re: Format Cells between empty rows until there is no more data

    Hi,

    Sorry about that. I am very new to this and am trying to update a macro I didn't create. I created it as you said to and I get and Error 9: Subscript out of range

    Thanks for all your help!

  6. #6
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Format Cells between empty rows until there is no more data

    Hi,

    I assume its in the following line caused by a missing worksheet called "Start"
    With Worksheets("Start")
    Can the sheetname change or is it every time the same you run the code on?

  7. #7
    Registered User
    Join Date
    06-20-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 365 ProPlus
    Posts
    15

    Re: Format Cells between empty rows until there is no more data

    Result.xlsx
    Start.xlsx

    Hi,

    I have been using the macro you helped me with and ran into a small problem. If there is no data in 2 consecutive cells side by side the formatting will not go out to row H it will stop. I have attached an example of what I started with and what happened. Is there a simple fix for this?

  8. #8
    Registered User
    Join Date
    06-20-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 365 ProPlus
    Posts
    15

    Re: Format Cells between empty rows until there is no more data

    Hi,

    I changed the code to say Sheet1 which is the typical name I use. Thanks for all your help you solved it and will save me many hours down the road!

  9. #9
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Format Cells between empty rows until there is no more data

    Hi,
    try
    Option Explicit
    
    Sub test()
        Dim xlRng As Range
        Dim aBorders: aBorders = Array(xlMedium, xlMedium, xlMedium, xlMedium, xlThin, xlThin)
        Dim sAddr As String
        Dim i As Byte
        Dim lngMaxRows As Long
        
        On Error GoTo ErrorHandler
        Application.ScreenUpdating = False
        
        With Worksheets("Start")
            lngMaxRows = .Cells(1, 1).CurrentRegion.Columns.Count
            
            Set xlRng = .Columns(1).Find(What:="*", After:=.Cells(.Rows.Count, 1), LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
            If Not xlRng Is Nothing Then
                sAddr = xlRng.Address
                Do
                    With xlRng.CurrentRegion
                        .Select
                        Set xlRng = .Resize(, lngMaxRows - .Columns.Count + .Columns.Count)
                    End With
                    With xlRng
                        .Select
                        .WrapText = True
                        .Borders(xlDiagonalDown).LineStyle = xlNone
                        .Borders(xlDiagonalUp).LineStyle = xlNone
                        For i = 7 To 12
                            With .Borders(i)
                                .LineStyle = xlContinuous
                                .ColorIndex = xlAutomatic
                                .TintAndShade = 0
                                .Weight = aBorders(i - 7)
                            End With
                        Next i
                    End With
                    Set xlRng = .Columns(1).Find(What:=vbNullString, After:=xlRng.Cells(1, 1), LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
                    xlRng.RowHeight = 10
                    Set xlRng = .Columns(1).Find(What:="*", After:=xlRng.Cells(1, 1), LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
                Loop While Not xlRng Is Nothing And xlRng.Address <> sAddr And xlRng.Row <> 1
            End If
        End With
        
    Proc_Exit:
        Application.ScreenUpdating = True
        Exit Sub
    ErrorHandler:
        MsgBox Err.Number & ": " & Err.Description, vbCritical + vbOKOnly, "Error"
        Resume Proc_Exit
    End Sub
    What I did:
    Instead of using the "currentregion" of a cell I check the max number of columns in row 1 first to determine how many columns should be "bordered"

  10. #10
    Registered User
    Join Date
    06-20-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 365 ProPlus
    Posts
    15

    Re: Format Cells between empty rows until there is no more data

    Thanks! That worked!

  11. #11
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Format Cells between empty rows until there is no more data

    Based on your last post in this thread, its apparent that you are satisfied with the solution(s) you've received and have solved your question, but you haven't marked your thread as "SOLVED". I will do it for you this time.

    In future, to mark your thread as Solved, you can do the following -
    Select Thread Tools-> Mark thread as Solved.

    Incase your issue is not solved, you can undo it as follows -
    Select Thread Tools-> Mark thread as Unsolved.

    Also, since you are relatively new to the forum, i would like to inform you that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post which helped you. This adds to the reputation of the person who has taken the time to help you.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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