Forum Discussion
Specific formatting in my excel table for OneDrive sharing. Possibly VBA needed.
- 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:
- Open the Excel workbook that contains the table you want to format.
- Press Alt+F11 to open the Visual Basic Editor.
- Insert a new module by clicking Insert > Module.
- Paste the VBA code into the new module.
- Save the module and close the Visual Basic Editor.
- Back in the Excel workbook, select the table you want to format.
- Press Alt+F8 to open the Macro dialog box.
- 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)
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:
- Open the Excel workbook that contains the table you want to format.
- Press Alt+F11 to open the Visual Basic Editor.
- Insert a new module by clicking Insert > Module.
- Paste the VBA code into the new module.
- Save the module and close the Visual Basic Editor.
- Back in the Excel workbook, select the table you want to format.
- Press Alt+F8 to open the Macro dialog box.
- 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)
- EagleMoxyOct 20, 2023Copper Contributor
LeonPavesicThank you so much, I really appreciate this, wonderful !!. Thanks for taking the time