Results 1 to 8 of 8

Macro based on header search

Threaded View

cardinalsfan0510 Macro based on header search 10-13-2011, 10:53 AM
Leith Ross Re: Macro based on header... 10-13-2011, 12:00 PM
cardinalsfan0510 Re: Macro based on header... 10-13-2011, 12:07 PM
cardinalsfan0510 Re: Macro based on header... 10-13-2011, 12:36 PM
Leith Ross Re: Macro based on header... 10-13-2011, 12:43 PM
cardinalsfan0510 Re: Macro based on header... 10-13-2011, 12:50 PM
cardinalsfan0510 Re: Macro based on header... 10-13-2011, 03:21 PM
Leith Ross Re: Macro based on header... 10-13-2011, 04:37 PM
  1. #6
    Registered User
    Join Date
    10-13-2011
    Location
    OKC
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro based on header search

    Quote Originally Posted by Leith Ross View Post
    Hello cardinalsfan0510,

    Are these converted files still Excel workbooks or text files?
    The files we open to modify are text files, pipe delimited. We then QA the data there and make any changes needed and run the macro shown previously to save it as a pipe delimited file again.

    But when we edit the data and run the macro, it is open as a workbook.

    EDIT: Here is what I have now. I think it is close. I got rid of a few things and simplified others. I made it only look at the open sheet (will only have one sheet open at a time) and changed the range to check only the first row as far as I need it to go. It will run without errors but will not save the file and the formats for the columns in question do not change.

    Again, thanks for the help. I am sure i am just missing something small. I can upload a copy of the file I am testing with if that will help.

    Public Sub ExportToTextFile(FName As String, Sep As String, SelectionOnly As Boolean)
    
    Dim WholeLine As String
    Dim FNum As Integer
    Dim RowNdx As Long
    Dim ColNdx As Integer
    Dim StartRow As Long
    Dim EndRow As Long
    Dim StartCol As Integer
    Dim EndCol As Integer
    Dim CellValue As String
    Dim Cell As Range
    Dim Ext As String
    Dim Filename As String
    Dim Filepath As String
    Dim Rng As Range
    Dim Wkb As Workbook
    Dim Wks As Worksheet
    
    Application.ScreenUpdating = False
    On Error GoTo EndMacro:
    FNum = FreeFile
    
    
    Do While Not IsEmpty(ActiveCell)
      Set Rng = ActiveSheet.Range.Cells(A1, GW1)
           For Each Cell In Rng
                If LCase(Cell) Like "*zip4*" Then
                Cell.EntireColumn.Cells.NumberFormat = "0000"
                Else
                If LCase(Cell) Like "*zip*" Then
                Cell.EntireColumn.Cells.NumberFormat = "00000"
           End If
           End If
           Next Cell
    Loop
    
    If SelectionOnly = True Then
        With Selection
            StartRow = .Cells(1).Row
            StartCol = .Cells(1).Column
            EndRow = .Cells(.Cells.Count).Row
            EndCol = .Cells(.Cells.Count).Column
        End With
    Else
        With ActiveSheet.UsedRange
            StartRow = .Cells(1).Row
            StartCol = .Cells(1).Column
            EndRow = .Cells(.Cells.Count).Row
            EndCol = .Cells(.Cells.Count).Column
        End With
    End If
    
    Open FName For Output Access Write As #FNum
    
    For RowNdx = StartRow To EndRow
        WholeLine = ""
        For ColNdx = StartCol To EndCol
            If Cells(RowNdx, ColNdx).Value = "" Then
                CellValue = ""
            Else
               CellValue = Cells(RowNdx, ColNdx).text
            End If
            CellValue = Replace(CellValue, vbCrLf, "")
            CellValue = Replace(CellValue, vbCr, "")
            CellValue = Replace(CellValue, vbLf, "")
            'CellValue = UCase(CellValue) (This was causing the upper case issue for .jpg)
            WholeLine = WholeLine & CellValue & Sep
        Next ColNdx
        WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
        Print #FNum, WholeLine
    
    Next RowNdx
    
    EndMacro:
    
    On Error GoTo 0
    Application.ScreenUpdating = True
    Close #FNum
    
    End Sub
    
    Public Sub DoTheExport()
    
    Dim FName As Variant
    Dim Sep As String
    
    FName = Application.GetSaveAsFilename()
    If FName = False Then
        MsgBox "You didn't select a file"
        Exit Sub
    End If
    Sep = InputBox("Enter a single delimiter character (e.g., comma or semi-colon)", "Export To Text File")
    ExportToTextFile CStr(FName), Sep, MsgBox("Do You Want To Export The Entire Worksheet?", vbYesNo, "Export To Text File") = vbNo
    
    End Sub
    Last edited by cardinalsfan0510; 10-13-2011 at 02:59 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1