Forum Discussion

EagleMoxy's avatar
EagleMoxy
Copper Contributor
Oct 18, 2023
Solved

Specific formatting in my excel table for OneDrive sharing. Possibly VBA needed.

Hello,   Thanks for looking at this. Appreciate all feedback both specific and general. If there are any VBA champions here that might be what is needed or maybe more advanced functions. I have f...
  • LeonPavesic's avatar
    Oct 18, 2023

    Hi EagleMoxy,

     

    Here is a VBA code that you can use to format an Excel table for OneDrive sharing:

     

    Sub FormatTableForOneDrive()
    
    'Declare variables
    Dim ws As Worksheet
    Dim tbl As Table
    Dim rngHeader As Range
    Dim rngBody As Range
    Dim col As Column
    
    'Set worksheet
    Set ws = ActiveSheet
    
    'Set table
    Set tbl = ws.Tables("Table1")
    
    'Format header row
    Set rngHeader = tbl.HeaderRowRange
    rngHeader.HorizontalAlignment = xlCenter
    rngHeader.VerticalAlignment = xlCenter
    rngHeader.Font.Size = 11
    rngHeader.Font.Bold = True
    
    'Format body rows
    Set rngBody = tbl.Range("A2:" & tbl.Columns.Count)
    rngBody.HorizontalAlignment = xlLeft
    rngBody.VerticalAlignment = xlTop
    rngBody.Font.Size = 10
    rngBody.Font.Bold = False
    
    'Format specific columns
    'Pending Issue, Response, and Admin Notes columns
    For Each col In tbl.Columns
    If col.Name = "Pending Issue" Or col.Name = "Response" Or col.Name = "Admin Notes" Then
    col.Cells.WrapText = True
    col.Cells.VerticalAlignment = xlBottom
    col.Cells.Font.Size = 9
    End If
    Next
    
    'Status column
    For Each col In tbl.Columns
    If col.Name = "Status" Then
    col.Cells.HorizontalAlignment = xlCenter
    col.Cells.VerticalAlignment = xlCenter
    col.Cells.Font.Size = 11
    col.Cells.Font.Bold = True
    End If
    Next
    
    'Apply borders to body cells
    rngBody.Borders.LineStyle = xlContinuous
    rngBody.Borders.ColorIndex = xlAutomatic
    rngBody.Borders.Weight = xlThin
    
    'Protect table
    tbl.Protect
    
    End Sub

     

     

    To use the VBA code:

    1. Open the Excel workbook that contains the table you want to format.
    2. Press Alt+F11 to open the Visual Basic Editor.
    3. Insert a new module by clicking Insert > Module.
    4. Paste the VBA code into the new module.
    5. Save the module and close the Visual Basic Editor.
    6. Back in the Excel workbook, select the table you want to format.
    7. Press Alt+F8 to open the Macro dialog box.
    8. Select the FormatTableForOneDrive macro and click Run.

    The macro will format the table with the settings specified in the code. The table will be protected so that users cannot change the formatting.

    Please click Mark as Best Response & Like if my post helped you to solve your issue.
    This will help others to find the correct solution easily. It also closes the item.


    If the post was useful in other ways, please consider giving it Like.


    Kindest regards,


    Leon Pavesic
    (LinkedIn)

Resources