How to convert formatted text to html in Excel

Rude86

New Member
Joined
Sep 12, 2016
Messages
4
Hi Guys,

Sorry if this has been covered I tried searching but couldn't come up with anything relevant.

I am having trouble finding a script to convert text in an excel cell to format into html.
The reason I need this done is I am importing product descriptions into a program and they only accept CSV format.
When I save the excel doc into csv it loses its formatting and just strings the text as 1 line with no space formatting.

EXAMPLE


Industry Specification

A5.1 E 6011

Features



-Fast freezing weld metal

-Thin flux coating

-Deep, forceful arc

-Versatile electrode



When saving this as CSV it just strings all the text as 1 line.
Is there a way of keeping the formatting when saving to csv?

Thanks in advance!




<tbody>
</tbody>

<tbody>
</tbody>
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
CSV is a text-only format. Font attributes, line breaks within cells, etc. are not retained. It's not clear how you'd expect to get HTML in CSV format - the two are entirely different file formats. If you want to include HTML tags in the CSV output, you'd need to add them to the data before saving as CSV.
 
Upvote 0
CSV is a text-only format. Font attributes, line breaks within cells, etc. are not retained. It's not clear how you'd expect to get HTML in CSV format - the two are entirely different file formats. If you want to include HTML tags in the CSV output, you'd need to add them to the data before saving as CSV.

That's correct!

Is there a way of running a script or something that can add those tags in automatically?
All i basically need is line spacing
 
Upvote 0
Excel is a spreadsheet, it is not a web designing tool.

Have you tried Save as WebPage
 
Last edited:
Upvote 0
Code:
Sub exportCSV()

Dim rng1 As Range
Dim rng2 As Range

' exclude column headers
Set rng1 = Range("A2:B10")
 
Open ActiveWorkbook.Path & "\Export.csv" For Output As #1
 
For Each rng2 In rng1
  Print #1, rng2.Value
Next
 
Close #1

End Sub

Worksheet looks like:

NAME AGE
BOB1
SUSAN2
JIM3
CAROL4
CARRY5
JOHN6
SMITH7
APPLE8
BEAR9

<colgroup><col width="72" span="2" style="width:54pt"> </colgroup><tbody>
</tbody>

Output is CSV file:

BOB
1
SUSAN
2
JIM
3
CAROL
4
CARRY
5
JOHN
6
SMITH
7
APPLE
8
BEAR
9

<colgroup><col width="72" style="width:54pt"> </colgroup><tbody>
</tbody>

See if you can use this as a starting point.
 
Last edited:
Upvote 0
Is there a way of running a script or something that can add those tags in automatically?
All i basically need is line spacing
I'm not sure what you mean by line spacing. If you want to replace the line-feeds in your posted sample with HTML line break codes, you could use a macro like:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim xlCell As Range
For Each xlCell In ActiveSheet.UsedRange.Cells
  With xlCell
    If InStr(.Text, Chr(10)) > 0 Then
      .Value = Replace(.Text, Chr(10), "< br >")
    End If
  End With
Next
Application.ScreenUpdating = True
End Sub
Note: I've had to insert spaces either side of 'br' because of the way the forum software handles the raw tags - remove those spaces before running the macro!
 
Last edited:
Upvote 0
I'm not sure what you mean by line spacing. If you want to replace the line-feeds in your posted sample with HTML line break codes, you could use a macro like:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim xlCell As Range
For Each xlCell In ActiveSheet.UsedRange.Cells
  With xlCell
    If InStr(.Text, Chr(10)) > 0 Then
      .Value = Replace(.Text, Chr(10), "
")
    End If
  End With
Next
Application.ScreenUpdating = True
End Sub



Ill give the above a go in a bit.

What i mean is the text goes from this:

Industry Specification

A5.1 E 6011

Features



-Fast freezing weld metal

-Thin flux coating

-Deep, forceful arc

-Versatile electrode


TO THIS


Industry SpecificationA5.1 E 6011 Features -Fast freezing weld metal-Thin flux coating-Deep, forceful arc-Versatile electrode

 
Upvote 0
So, are all these data in a single cell, or are they in separate cells? If they were in a single cell, I'd expect the output to already be in your 'to this' format. There is no HTML code in any of that!

The code I posted assumes the data are in a single cell and you want the output to have HTML code applied for the line breaks, thus:
Code:
Industry Specification< br >< br >A5.1 E 6011< br >< br >Features< br >< br >< br >< br >-Fast freezing weld metal< br >< br >-Thin flux coating< br >< br >-Deep, forceful arc< br >< br >-Versatile electrode
As you can see, the macro adds the HTML < br > codes (I've had to insert spaces either side of 'br' because of the way the forum software handles the raw tags).
 
Last edited:
Upvote 0
The code i posted here does exactly what you are asking and saves it as a csv file for you. If your data is in one column, then just change this:

Rich (BB code):
Set rng1 = Range("A2:B10")


to this:

Rich (BB code):
Set rng1 = Range("A2:A10")
 
Upvote 0
I'm not sure what you mean by line spacing. If you want to replace the line-feeds in your posted sample with HTML line break codes, you could use a macro like:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim xlCell As Range
For Each xlCell In ActiveSheet.UsedRange.Cells
  With xlCell
    If InStr(.Text, Chr(10)) > 0 Then
      .Value = Replace(.Text, Chr(10), "< br >")
    End If
  End With
Next
Application.ScreenUpdating = True
End Sub
Note: I've had to insert spaces either side of 'br' because of the way the forum software handles the raw tags - remove those spaces before running the macro!

You are a legend!!

I have just tried this and it works great!

Thankyou very much this will save me a lot of time!
 
Upvote 0

Forum statistics

Threads
1,214,406
Messages
6,119,330
Members
448,888
Latest member
Arle8907

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