Table Formatting by a Macro

T

Thejan Mendis

Hi all,

I hope somebody can help me on creating a small and very helpful EXCEL
macro.

I have a excel document with table like content and I need to create a macro
which will format each cell with <td>*what ever the cell vale</td> and each
row with <tr>*cells</tr>

Example

a b c
1 2 5

<tr><td>a</td><td>b</td><td>c</td></tr>
<tr><td>1</td><td>2</td><td>5</td></tr>

if i can make apply this to selected range that will be much better


Please help me................

THEJAN
 
C

Chip Pearson

I assume you want to make an HTML page with a table of Excel
data. Try something like the following:


Sub AAA()
Dim FNum As Integer
Dim FName As String
Dim Rng As Range
Dim Rw As Range
Dim FullRange As Range

Set FullRange = Range("A1:C3") '<<< CHANGE

FName = "H:\Test.htm" '<<< CHANGE
FNum = FreeFile()
Open FName For Output As #FNum
Print #FNum, "<HTML>"
Print #FNum, "<TABLE>"
For Each Rw In FullRange.Rows
Print #FNum, "<TR>"
For Each Rng In Rw.Cells
Print #FNum, "<TD>" & Rng.Text & "</TD>"
Next Rng
Print #FNum, "</TR>"
Next Rw
Print #FNum, "</TABLE>"
Print #FNum, "</HTML>"
Close #FNum
End Sub

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top