Excel VBA. Code improving : Create txt file with specific name

Let say there is a code that copy - past data from sheet to txt file. Now the name of the txt file always "testing". I need to improve this code in order that txt file that is creating will have name that consist from :
1) the info in cell C8 sheet "parameters " and 2) second part of the name is located in sheet "data " in the cell G8
Sub test()
    Dim TempSht As Worksheet
    Dim rRng As Range
    Dim sFullPath As String
    sFullPath = ThisWorkbook.Path & Application.PathSeparator

    With Sheet1
        Set rRng = .Range(.Cells(8, 2), .Cells(.Rows.Count, 7).End(xlUp))
    End With
    Set TempSht = Sheets.Add
    rRng.Copy TempSht.Range("A1")
    TempSht.Copy
    ActiveWorkbook.SaveAs Filename:=sFullPath & "testing.txt", FileFormat:=xlText, CreateBackup:=False
    Application.DisplayAlerts = False
    TempSht.Delete
    ActiveWorkbook.Close True
    Application.DisplayAlerts = True
End Sub

Open in new window


Please take at the attachment
ABC-5182015--120500-PM.txt
testing.txt
For-forum.xlsx
SunnyXAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
Try this amendment to the previous code

Option Explicit


Sub ConvertToTabDelimited()
    Dim TempSht As Worksheet
    Dim rRng As Range
    Dim sFullPath As String, sName As String
    
    sName = Sheets("parameters").Range("C8") & Sheets("data").Range("G8") & .txt
    sFullPath = ThisWorkbook.Path & Application.PathSeparator & sName
    With Application
        .ScreenUpdating = False
        On Error GoTo err_quit
        With Sheet1
            Set rRng = .Range(.Cells(8, 2), .Cells(.Rows.Count, 7).End(xlUp))
        End With
        Set TempSht = Sheets.Add
        rRng.Copy TempSht.Range("A1")
        TempSht.Copy
        ActiveWorkbook.SaveAs Filename:=sFullPath, FileFormat:=xlText, CreateBackup:=False
        .DisplayAlerts = False
        TempSht.Delete
        ActiveWorkbook.Close True
        .DisplayAlerts = True

err_quit:
        .ScreenUpdating = True
    End With
End Sub

Open in new window

0
SunnyXAuthor Commented:
thx for your reply but

the code  doesn't work properly. The code gives error on line 9 ( highlighting =>  .txt )

Therefore I change from  .txt to ".txt"
and the code still doesn't work properly.

It doesn't delete temporary sheet list and doesn't make txt file
0
Roy CoxGroup Finance ManagerCommented:
I missed the speech marks around.txt, but the code does work when they are added,

Does the workbook actually contain those two sheets mentioned?
What text is in the two cells?  It may be that the cells contain characters not allowed in file names.
Option Explicit


Sub ConvertToTabDelimited()
    Dim TempSht As Worksheet
    Dim rRng As Range
    Dim sFullPath As String, sName As String
    
    sName = Sheets("parameters").Range("C8") & Sheets("data").Range("G8") & ".txt"
  
    sFullPath = ThisWorkbook.Path & Application.PathSeparator & sName
    With Application
        .ScreenUpdating = False
        On Error GoTo err_quit
        With Sheet1
            Set rRng = .Range(.Cells(8, 2), .Cells(.Rows.Count, 7).End(xlUp))
        End With
        Set TempSht = Sheets.Add
        rRng.Copy TempSht.Range("A1")
        TempSht.Copy
        ActiveWorkbook.SaveAs Filename:=sFullPath, FileFormat:=xlText, CreateBackup:=False
        .DisplayAlerts = False
        TempSht.Delete
        ActiveWorkbook.Close True
        .DisplayAlerts = True

err_quit:
        .ScreenUpdating = True
    End With
End Sub

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Roy CoxGroup Finance ManagerCommented:
I your example file I presume the sheet should be called data. If so the cell  G* contains a data and "/* is not allowed in file names. You need to reformat it something like this

 
sName = Sheets("parameters").Range("C8") & Format(Sheets("data").Range("G8"), " dd mm yy hh mm") & ".txt"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SunnyXAuthor Commented:
thx u so much !
0
Roy CoxGroup Finance ManagerCommented:
Glad it worked
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.