Consulting

Results 1 to 7 of 7

Thread: How create border in excel vba

  1. #1
    VBAX Newbie
    Joined
    Sep 2024
    Posts
    5
    Location

    How create border in excel vba

    I am trying a border using VBA in excel but am getting object required error. Kindly help me to fix

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,787
    Location
    Without seeing your code and knowing what kind of borders you want, it's hard to say

    The first is the macro recorder, the second is some cleanup


    
    Option Explicit
    
    
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    
    '
        Range("A1:D12").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Selection.Borders(xlInsideVertical).LineStyle = xlNone
        Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    End Sub
    
    
    Sub Macro2()
        With Range("A1:D12")
            .Borders(xlDiagonalDown).LineStyle = xlNone
            .Borders(xlDiagonalUp).LineStyle = xlNone
            With .Borders
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            .Borders(xlInsideVertical).LineStyle = xlNone
            .Borders(xlInsideHorizontal).LineStyle = xlNone
        End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Sep 2024
    Posts
    5
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Without seeing your code and knowing what kind of borders you want, it's hard to say

    The first is the macro recorder, the second is some cleanup


    Option Explicit
    
    Sub Macro1()
        ' Macro1 Macro
        Range("A1:D12").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Selection.Borders(xlInsideVertical).LineStyle = xlNone
        Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    End Sub
    
    Sub Macro2()
        With Range("A1:D12")
            .Borders(xlDiagonalDown).LineStyle = xlNone
            .Borders(xlDiagonalUp).LineStyle = xlNone
            With .Borders
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            .Borders(xlInsideVertical).LineStyle = xlNone
            .Borders(xlInsideHorizontal).LineStyle = xlNone
        End With
    End Sub
    Sub ApplyDynamicBorder()
        Dim ws As Worksheet
        Dim lastRow As Long
        Dim lastCol As Long
        Dim tableRange As Range
        ' Set the worksheet
        Set ws = ThisWorkbook.Sheets("Sheet1")
        ' Find the last row with data in column A
        lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        ' Find the last column with data in row 1
        lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
        ' Define the range of the table
        Set tableRange = ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastCol))
        ' Apply borders to the table range
        With tableRange.Borders
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = 0
        End With
    End Sub
    Last edited by Aussiebear; 09-25-2024 at 01:30 PM. Reason: Added code tags to supplied code

  4. #4
    VBAX Newbie
    Joined
    Sep 2024
    Posts
    5
    Location
    I am using above code but am getting object required error and I am trying here to create dynamic border based on the data changes in source file

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,924
    I get no error with your ApplyDynamicBorder code!
    It gives a thin black border around every cell in the table.
    Is that what you want?
    Paul's suggestion was to put a border around the whole range (table) and no borders around each cell. If that's your preference then your section ' Apply borders to the table range might be reduceable to one line:
    tableRange.BorderAround 1
    however, you might want a bit more control, eg. a medium thickness green continuous border:
    tableRange.BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, Color:=RGB(143, 219, 41)
    More info:
    https://learn.microsoft.com/en-us/of...e.borderaround
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    VBAX Newbie
    Joined
    Sep 2024
    Posts
    5
    Location
    Yes you are right. But am getting object required error while running that code. Could you please me anything I am missing over there

  7. #7
    VBAX Newbie
    Joined
    Sep 2024
    Posts
    5
    Location
    It is worked now. Thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •