vba replace special characters

Urraco

Board Regular
Joined
Apr 19, 2021
Messages
69
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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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