Create Text file from Excel (part 2)

pbt

Well-known Member
Joined
Oct 18, 2005
Messages
1,613
This is a continuation of a project I'm working on and had help on:
http://www.mrexcel.com/forum/showthread.php?t=315241

Using my Old code that I am trying to modify with jindon's suggestions from the above link:

Old code:
Code:
Sub CopyToNewBook()
    Dim Response As Integer
    On Error Resume Next
    Response = MsgBox(Prompt:="This  procedure  will  export  the  payroll  SUMMARY" & vbCr & _
    "into a Tab Delimited Text File and save it to C:\Payroll" & vbCr & _
    "                        PROCEED", Buttons:=vbQuestion + vbYesNo, Title:="               FINAL STEP")
        If Response = vbNo Then
        Exit Sub
        End If
    Application.ScreenUpdating = False

    '//Copy the SUMMARY sheet to a new book (Text tab delimited).
    Sheets("SUMMARY").Copy
    '//Identify the path you wish to save to here.
    MkDir "C:\Payroll"
    ChDir "C:\Payroll"
    '//Save the export sheet as a new workbook named "Payroll"
    '//and the date that resides in the PAYCALC sheet cell B3
    '//Copy will be saved as tab delimited text.
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs "Payroll" & " " & _
    Format(Sheet1.Range("C4").Value, "mmm,dd,yy") & ".txt", _
        FileFormat:=xlText, CreateBackup:=False
    '//Close the copy and return to the original.
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
    MsgBox Prompt:="Export of payroll information to a text file complete" & vbCr & _
    "The file may be found on your C drive in a folder named Payroll", _
    Title:="                    PROCEDURE SUCCESSFUL"
    
End Sub
Gives me a Text file as below:
Excel Workbook
ABCDEFGHIJK
1LBZVB010.55.ST30LS01456/771*46872.57STUCCO50225/28
2LBZVB010.55.ST30LS01406/771*650100.79STUCCO50225/28
3LBZVB010.55.ST30LS03726/771*60093.04STUCCO50225/28
4LBZVB010.55.ST30LS03736/771*40062.03STUCCO50225/28
Sheet1


With this modification to the above code using jindon's lead:
Code:
Sub CopyToTextFile()
    Dim Response As Integer
    On Error Resume Next
    Response = MsgBox(Prompt:="This  procedure  will  export  the  payroll  SUMMARY" & vbCr & _
    "into a Tab Delimited Text File and save it to your" & vbCr & "! DESKTOP !  with a Date and Time Stamp To The Second" & vbCr & _
    " " & vbCr & "                        PROCEED", Buttons:=vbQuestion + vbYesNo, Title:="               FINAL STEP")
        If Response = vbNo Then
        Exit Sub
        End If
    Application.ScreenUpdating = False

    '//Copy the SUMMARY sheet to a new book (Text tab delimited).
    fn = CreateObject("WScript.Shell").SpecialFolders("Desktop") & _
       "\Payroll Export(" & Format$(Now, "mmddyy-hh-mm-ss") & ").txt"
With Sheets("SUMMARY")
    For Each r In .Range("K1", .Range("K" & Rows.Count).End(xlUp))
        If r.Value <> 0 Then txt = txt & vbCrLf & Join(Evaluate("transpose(transpose(" & _
         r.Offset(, -10).Resize(, 11).Address & "))"), vbTab)
    Next
End With
Open fn For Output As #1
    Print #1, Mid$(txt, Len(vbCrLf) + 1)
Close #1
   
    Range("A1").Select
    Application.ScreenUpdating = True
    SplashForm.Show
   
    MsgBox Prompt:="Export of payroll information to a text file complete" & vbCr & _
    "The file may be found on your DESKTOP with a Time Stamp", _
    Title:="                    PROCEDURE SUCCESSFUL"
    
End Sub

I come up with this Text file:

Excel Workbook
ABCDEFGHIJK
8LBZVB010.55.ST30LS01453960571*46872.5696202531646STUCCO502239596
9LBZVB010.55.ST30LS01403960571*650100.7911392405060STUCCO502239596
10LBZVB010.55.ST30LS03723960571*60093.0379746835443STUCCO502239596
11LBZVB010.55.ST30LS03733960571*40062.0253164556962STUCCO502239596
Sheet1


As you can see Col C & K come up with date serial numbers and Col H comes up with 13 didgits to the right of the decimal.

I am running both macros from the same Workbook (for testing purposes). Cols C & K are formatted as Custom > m/d, and Col H is formated as Number > 2 decimal places.

I am lost on how to make second macro produce the results that the first one did in the Text file.

Harry
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Thought this comment might shed some light:

On the SUMMARY sheet, Col's C & K do show date format as 6/7/2008 in the Formula bar, and Col H does show 13 places to the right in the Formula bar.

Harry
 
Upvote 0
Harry
Change
Code:
With Sheets("SUMMARY")
    For Each r In .Range("K1", .Range("K" & Rows.Count).End(xlUp))
        If r.Value <> 0 Then txt = txt & vbCrLf & Join(Evaluate("transpose(transpose(" & _
         r.Offset(, -10).Resize(, 11).Address & "))"), vbTab)
    Next
End With
to
Code:
With Sheets("SUMMARY")
    With .Range("A1", .Range("K" & Rows.Count).End(xlUp))
        For i = 1 To .Rows.Count
            For Each r In .Rows(i).Cells
                temp = temp & vbTab & r.Text
            Next
            txt = txt & vbCrLf & temp
            temp = ""
        Next
    End With
End With
 
Upvote 0
Thank you jindon

The posted fix did the job, but the info on the Text file starts in col 2.

It should be in Col 1.

Harry
 
Upvote 0
Thank you jindon, I really appreciate the Help that you have given me.

All is fine, works great. (You are the Man)
________________________________________________________________
Unrelated Question: With a MsgBox, is it possible to make certain lines Bold?
(seems like I read something that it can't be done, just want verification)

Example: With my last MsgBox, can I make "DESKTOP" bold.

I know that this is a start of another Thread, but if the answer is No, then there will be no other thread.

Harry
 
Upvote 0
If you search the forum, you might get some codes with API functions, but I would create a UserForm instead and it is much easier.
 
Upvote 0
Thank you for the insight on that.

I will experiment with it and see what I come up with.

Harry
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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