vba replace special characters

Urraco

Board Regular
Joined
Apr 19, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I made a shortcut to save my Sheet into text format..
how could I add 2 more things to the script below.
I tried this but it turns out that I didn't put them where they should be..

1 . replace all alt+enter with space
altEnter.Value = Replace(altEnter, vbLf, " ")

2. save only selected columns (visible cell)
SpecialCells(xlCellTypeVisible)

Thank you very much

Code:
Sub saveTxtV3()
    Dim fileName As Variant
    Dim rng As Range
    Dim DelimChar As String
    Dim LastRow As Long
    Dim LastColumn As Long
    
On Error Resume Next
Set rng = Application.InputBox("select", Type:=8)
On Error GoTo 0
    If rng Is Nothing Then Exit Sub
         fileName = Application.GetSaveAsFilename("C:\Folder\" & ActiveSheet.Name, "Text File (*.txt), *.txt")
            If fileName = False Then Exit Sub

    If Dir(fileName) <> "" Then
        If MsgBox("File '" & fileName & "' already existe.  Overwrite?", vbYesNo + vbExclamation) <> vbYes Then Exit Sub
        Kill fileName
    End If

    Open fileName For Output As #1
    For i = 1 To rng.Rows.Count
        For j = 1 To rng.Columns.Count
            lineText = IIf(j = 1, "", lineText & vbTab) & rng.Cells(i, j)
        Next j
        Print #1, lineText
    Next i
    Close #1

MsgBox "Done"
    
    CreateObject("Shell.Application").Open (fileName)
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,215,097
Messages
6,123,077
Members
449,094
Latest member
mystic19

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