L'Espace Délimité Sur "Export To Text' Excel Macro Problème

J'ai le dessous de macro vba pour Exporter les cellules sélectionnées dans un fichier texte. Le problème semble être le délimiteur.

J'ai besoin de tout pour être dans une position exacte. J'ai chaque largeur de colonne à la bonne largeur(9 pour 9 comme le SSN) et j'ai les cellules de la police que Courier New(9pt) dans une Feuille Excel.

Lorsque je l'exécute il sort VRAIMENT proche de ce dont j'ai besoin, mais il ne semble pas traiter avec les colonnes qui sont juste un seul espace en largeur.

Je vais mettre de l' ENSEMBLE méthode (et de l'accompagnement de la fonction) au fond de référence mais d'abord je voudrais poster la partie je PENSE que est là que j'ai besoin de se concentrer sur. Je ne sais pas dans quel sens...

C'est là que je crois ma question est(le séparateur est défini à delimiter = "" -->

' Loop through every cell, from left to right and top to bottom.
  For RowNum = 1 To TotalRows
     For ColNum = 1 To TotalCols
        With Selection.Cells(RowNum, ColNum)
        Dim ColWidth As Integer
        ColWidth = Application.RoundUp(.ColumnWidth, 0)
        ' Store the current cells contents to a variable.
        Select Case .HorizontalAlignment
           Case xlRight
              CellText = Space(Abs(ColWidth - Len(.Text))) & .Text
           Case xlCenter
              CellText = Space(Abs(ColWidth - Len(.Text)) / 2) & .Text & _
                         Space(Abs(ColWidth - Len(.Text)) / 2)
           Case Else
              CellText = .Text & Space(Abs(ColWidth - Len(.Text)))
        End Select
        End With


' Write the contents to the file.
   ' With or without quotation marks around the cell information.
            Select Case quotes
               Case vbYes
                  CellText = Chr(34) & CellText & Chr(34) & delimiter
               Case vbNo
                  CellText = CellText & delimiter
            End Select
            Print #FNum, CellText;

   ' Update the status bar with the progress.
            Application.StatusBar = Format((((RowNum - 1) * TotalCols) _
               + ColNum) / (TotalRows * TotalCols), "0%") & " Completed."

   ' Loop to the next column.
         Next ColNum
   ' Add a linefeed character at the end of each row.
         If RowNum <> TotalRows Then Print #FNum, ""
   ' Loop to the next row.
      Next RowNum

C'est le TOUT INCLUS! Pour référence, l'original est ICI.

Sub ExportText()
'
' ExportText Macro
'
Dim delimiter As String
Dim quotes As Integer
Dim Returned As String
delimiter = ""
quotes = MsgBox("Surround Cell Information with Quotes?", vbYesNo)
' Call the WriteFile function passing the delimiter and quotes options.
Returned = WriteFile(delimiter, quotes)
' Print a message box indicating if the process was completed.
Select Case Returned
Case "Canceled"
MsgBox "The export operation was canceled."
Case "Exported"
MsgBox "The information was exported."
End Select
End Sub
'-------------------------------------------------------------------
Function WriteFile(delimiter As String, quotes As Integer) As String
' Dimension variables to be used in this function.
Dim CurFile As String
Dim SaveFileName
Dim CellText As String
Dim RowNum As Integer
Dim ColNum As Integer
Dim FNum As Integer
Dim TotalRows As Double
Dim TotalCols As Double
' Show Save As dialog box with the .TXT file name as the default.
' Test to see what kind of system this macro is being run on.
If Left(Application.OperatingSystem, 3) = "Win" Then
SaveFileName = Application.GetSaveAsFilename(CurFile, _
"Text Delimited (*.txt), *.txt", , "Text Delimited Exporter")
Else
SaveFileName = Application.GetSaveAsFilename(CurFile, _
"TEXT", , "Text Delimited Exporter")
End If
' Check to see if Cancel was clicked.
If SaveFileName = False Then
WriteFile = "Canceled"
Exit Function
End If
' Obtain the next free file number.
FNum = FreeFile()
' Open the selected file name for data output.
Open SaveFileName For Output As #FNum
' Store the total number of rows and columns to variables.
TotalRows = Selection.Rows.Count
TotalCols = Selection.Columns.Count
' Loop through every cell, from left to right and top to bottom.
For RowNum = 1 To TotalRows
For ColNum = 1 To TotalCols
With Selection.Cells(RowNum, ColNum)
Dim ColWidth As Integer
ColWidth = Application.RoundUp(.ColumnWidth, 0)
' Store the current cells contents to a variable.
Select Case .HorizontalAlignment
Case xlRight
CellText = Space(Abs(ColWidth - Len(.Text))) & .Text
Case xlCenter
CellText = Space(Abs(ColWidth - Len(.Text)) / 2) & .Text & _
Space(Abs(ColWidth - Len(.Text)) / 2)
Case Else
CellText = .Text & Space(Abs(ColWidth - Len(.Text)))
End Select
End With
' Write the contents to the file.
' With or without quotation marks around the cell information.
Select Case quotes
Case vbYes
CellText = Chr(34) & CellText & Chr(34) & delimiter
Case vbNo
CellText = CellText & delimiter
End Select
Print #FNum, CellText;
' Update the status bar with the progress.
Application.StatusBar = Format((((RowNum - 1) * TotalCols) _
+ ColNum) / (TotalRows * TotalCols), "0%") & " Completed."
' Loop to the next column.
Next ColNum
' Add a linefeed character at the end of each row.
If RowNum <> TotalRows Then Print #FNum, ""
' Loop to the next row.
Next RowNum
' Close the .prn file.
Close #FNum
' Reset the status bar.
Application.StatusBar = False
WriteFile = "Exported"
End Function

D'Autres Découvertes

J'ai découvert qu'il ya quelque chose de mal avec Case xlCenter ci-dessous. C'est vendredi et je n'ai pas été capable d'envelopper ma tête autour de lui, mais encore tout ce qu'elle fait dans ce case a été de retirer les " ". J'ai vérifié cela en définissant toutes les colonnes à Gauche Justifiée, de sorte que le Case Else serait utilisé à la place et à l'ALTO! Mon espace est resté. J'aimerais comprendre pourquoi, mais en fin de compte, il est Une) de travail et B). e.James solution est de mieux de toute façon.

Merci pour l'aide.

Dim ColWidth As Integer
ColWidth = Application.RoundUp(.ColumnWidth, 0)
' Store the current cells contents to a variable.
Select Case .HorizontalAlignment
Case xlRight
CellText = Space(Abs(ColWidth - Len(.Text))) & .Text
Case xlCenter
CellText = Space(Abs(ColWidth - Len(.Text)) / 2) & .Text & _
Space(Abs(ColWidth - Len(.Text)) / 2)
Case Else
CellText = .Text & Space(Abs(ColWidth - Len(.Text)))
End Select
Pouvez-vous donner un peu plus d'informations sur le mode de défaillance? Quel type de sortie voyez-vous maintenant, et au type de sortie vous attendez-vous à voir?
Je vais essayer. Je ne peux pas afficher le fichier tel qu'il a des informations sensibles sur elle. L'ensemble de l'exportation devraient être 360 "positions" de large. Sur 400 dossiers environ 15 où "plus large" puis 360 par une position ou 2. Position 10 (deuxième colonne) est de 1 et le vide. Ce qui manquait sur TOUTES les lignes. Les 15 qui ont été trop large, étaient tous originaires de la même colonne (Adresse de la Rue de la Position 186-210)
Je crois que je vois le problème avec votre centrage code. Imaginez que la largeur de la colonne est fixée à 20, et le texte de cette colonne est "bonjour". Ensuite, votre code sera mis 7 ou 8 espaces de chaque côté du texte (selon le 15/2 est arrondi), et ce sera la cause de votre longueur totale soit 19 ou 21, mais certainement pas 20!
Je vais ajouter un peu de code dans ma réponse à apporter une solution

OriginalL'auteur Refracted Paladin | 2010-01-15