Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Mar 2004
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Text files and dates (VBA/Excel/97)

    When I use VBA to save to a text file with dates, I get the "#" symbol before and after the date. This seems to create a problem when I try to re-import the file. Is there any way to remove the "#"?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Text files and dates (VBA/Excel/97)

    Use the Print instruction instead of the Write instruction. Type Print or Write in a VB window and press F1 to get information on those instructions.

  3. #3
    2 Star Lounger
    Join Date
    Mar 2004
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Text files and dates (VBA/Excel/97)

    Okay, this is the code that I'm working with:

    Sub ExportRange()
    Dim ExpRng As range
    Worksheets("SecurityListing").Activate
    ActiveCell.CurrentRegion.Select

    Set ExpRng = ActiveCell.CurrentRegion
    FirstCol = ExpRng.Columns(1).Count
    LastCol = FirstCol + ExpRng.Columns.Count - 1
    FirstRow = ExpRng.Rows(1).Row + 1
    LastRow = FirstRow + ExpRng.Rows.Count - 1

    Open ThisWorkbook.Path & "Data" & strMonth & strYear & ".txt" For Output As #1
    For r = FirstRow To LastRow
    For c = FirstCol To LastCol
    vData = ExpRng.Cells(r, c).Value
    If IsNumeric(vData) Then vData = Val(vData)
    If c <> LastCol Then
    Print #1, vData;
    Else
    Print #1, vData
    End If
    Next c
    Next r
    Close #1
    End Sub

    My matrix contains both text and numeric columns. I believe that my problem is in the first "If" statement above. When I "print" the data, the text columns are combined with the numeric columns (in other words, there is no space separating the two data points in the text file). How can I change the if statement so that my text columns have a space separator? Thanks.

  4. #4
    2 Star Lounger
    Join Date
    Mar 2004
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Text files and dates (VBA/Excel/97)

    Yes, and I did try this originally. Inserting the " " changes the text file from n rows to one column. I would prefer that the file be saved with n rows though.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Text files and dates (VBA/Excel/97)

    Sorry, I meant

    <code>Print #1, vData ; " ";</code>

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Text files and dates (VBA/Excel/97)

    Error corrected by HansV

    Look up the Print # instruction in the online help, it contains an explanation how to handle this. You can simply add a space, or a comma, or whatever. For example:

    <code>...</code>
    <code>If c <> LastCol Then</code>
    <code> Print #1, vData ; " ";</code>
    <code>Else</code>
    <code> Print #1, vData</code>
    <code>End If</code>
    <code>...</code>

    You can also use Spc(n) to insert n spaces.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •