How to style table border ?

VBAEXCELNew

New Member
Joined
Apr 3, 2023
Messages
38
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello,

in my vba i would like table border to be black but however after a lot of test trying to add in css it dont work, i have realize that the only style i can use is through excel vba style for tables

I am trying to get my table that i have created in vba to have a border that is black or xlContinuous but so far no luck does anyone know how ?
VBA Code:
HtmlContent = "<table>"
    HtmlContent = "<br><table border='1'><tbody>"
  
    'strQuotation = Trim(ThisWorkbook.Sheets("Master Table").Range("B" & R.Offset(0, 1)).Text)
    
    HtmlContent = HtmlContent & "<tr><th>Quotation / DC No.:</th><td>" & R.Offset(0, 1) & "</td></tr>"
    HtmlContent = HtmlContent & "<tr><th>Description:</th><td>" & R.Offset(0, 2) & "</td></tr> "
    HtmlContent = HtmlContent & "<tr><th>Status:</th><td>" & R.Offset(0, 0) & "<br>" & R.Offset(0, 5) & "<br>" & "</td></tr>"
    HtmlContent = HtmlContent & "<tr><th>Remarks:</th> <td>" & R.Offset(0, 8) & "</td></tr>"
    

HtmlContent = HtmlContent & "</tbody></table>"
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
In order to set the border color and style using inline CSS, you can use the following modified code:

VBA Code:
HtmlContent = "<br><table style='border-collapse: collapse;'><tbody>"

'strQuotation = Trim(ThisWorkbook.Sheets("Master Table").Range("B" & R.Offset(0, 1)).Text)

HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black;'>Quotation / DC No.:</th><td style='border: 1px solid black;'>" & R.Offset(0, 1) & "</td></tr>"
HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black;'>Description:</th><td style='border: 1px solid black;'>" & R.Offset(0, 2) & "</td></tr> "
HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black;'>Status:</th><td style='border: 1px solid black;'>" & R.Offset(0, 0) & "<br>" & R.Offset(0, 5) & "<br>" & "</td></tr>"
HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black;'>Remarks:</th> <td style='border: 1px solid black;'>" & R.Offset(0, 8) & "</td></tr>"

HtmlContent = HtmlContent & "</tbody></table>"

The border-collapse: collapse; style applied to the table will remove any spacing between table cells. The border: 1px solid black; style is applied to each table cell (<th> and <td>), which will give each cell a 1px solid black border.
 
Upvote 1
In order to set the border color and style using inline CSS, you can use the following modified code:

VBA Code:
HtmlContent = "<br><table style='border-collapse: collapse;'><tbody>"

'strQuotation = Trim(ThisWorkbook.Sheets("Master Table").Range("B" & R.Offset(0, 1)).Text)

HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black;'>Quotation / DC No.:</th><td style='border: 1px solid black;'>" & R.Offset(0, 1) & "</td></tr>"
HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black;'>Description:</th><td style='border: 1px solid black;'>" & R.Offset(0, 2) & "</td></tr> "
HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black;'>Status:</th><td style='border: 1px solid black;'>" & R.Offset(0, 0) & "<br>" & R.Offset(0, 5) & "<br>" & "</td></tr>"
HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black;'>Remarks:</th> <td style='border: 1px solid black;'>" & R.Offset(0, 8) & "</td></tr>"

HtmlContent = HtmlContent & "</tbody></table>"

The border-collapse: collapse; style applied to the table will remove any spacing between table cells. The border: 1px solid black; style is applied to each table cell (<th> and <td>), which will give each cell a 1px solid black border.
I have another question i wan my Th to be align left how can i do it ?
 
Upvote 0
Yes, you can align the text in the "th" element to the left by adding the "text-align: left;" style to the "th" element. Here's an updated version of the code that should do what you want:

VBA Code:
HtmlContent = "<br><table style='border-collapse: collapse;'><tbody>"
'strQuotation = Trim(ThisWorkbook.Sheets("Master Table").Range("B" & R.Offset(0, 1)).Text)

HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black; text-align: left;'>Quotation / DC No.:</th><td style='border: 1px solid black;'>" & R.Offset(0, 1) & "</td></tr>"
HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black; text-align: left;'>Description:</th><td style='border: 1px solid black;'>" & R.Offset(0, 2) & "</td></tr> "
HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black; text-align: left;'>Status:</th><td style='border: 1px solid black;'>" & R.Offset(0, 0) & "<br>" & R.Offset(0, 5) & "<br>" & "</td></tr>"
HtmlContent = HtmlContent & "<tr><th style='border: 1px solid black; text-align: left;'>Remarks:</th> <td style='border: 1px solid black;'>" & R.Offset(0, 8) & "</td></tr>"

HtmlContent = HtmlContent & "</tbody></table>"

Notice that the "text-align: left;" style is added to each "th" element to align the text to the left. You can modify the code as needed to adjust the border and other styles if necessary.
 
Upvote 1
Solution

Forum statistics

Threads
1,215,086
Messages
6,123,040
Members
449,092
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top