2015-06-19 68 views
2

DATA:會寫空間,但不是最後一個字符(VBA的Excel)

mydata

所需的輸出:

desired

電流輸出:

current

MY當前代碼:

Private Sub GenerateFlatFile_Click() 
Dim myFile As String, rng As Range, cellValue As Variant, I As Integer, j As Integer, SpacingCode As String 

Dim iPar As Integer 
Dim sBlank As Long 
Dim cont As Boolean 
Dim mystring As String 

myFile = "C:\Reformatted.txt" 
Set rng = Selection 

Open myFile For Output As #1 

Dim strArr(1 To 63) As String, intBeg As Integer, intEnd As Integer, intCount As Integer, sChar As String 

For I = 2 To rng.Rows.Count 
    For j = 1 To rng.Columns.Count 
     If InStr(1, CStr(Cells(1, j).Value), "63") = 1 Then 
      strArr(Val(Cells(1, j).Value)) = Cells(I, j).Value 
     ElseIf InStr(1, CStr(Cells(1, j).Value), "Code") Then 

       iPar = InStr(1, CStr(Cells(I, j).Value), "(") 
       If Mid(Cells(I, j).Value, iPar - 1, 1) = "" Then 
        If Mid(Cells(I, j).Value, iPar - 2, 1) = "" Then 
        sChar = Mid(Cells(I, j).Value, iPar - 3, 1) 
        Else: sChar = Mid(Cells(I, j).Value, iPar - 4, 1) 
        End If 
       Else: sChar = Mid(Cells(I, j).Value, iPar - 2, 1) 
       End If 
       If IsNumeric(Mid(Cells(I, j).Value, iPar + 1, 2)) Then 
        sBlank = Mid(Cells(I, j).Value, iPar + 1, 2) 
       Else: sBlank = Mid(Cells(I, j).Value, iPar + 1, 1) 
       End If 
       mystring = Space(sBlank) & sChar 
       cont = InStr(iPar + 1, CStr(Cells(I, j).Value), "(") 

      Do While cont = True 

       iPar = InStr(iPar + 1, CStr(Cells(I, j).Value), "(") 
       If Mid(Cells(I, j).Value, iPar - 1, 1) = "" Then 
        If Mid(Cells(I, j).Value, iPar - 2, 1) = "" Then 
        sChar = Mid(Cells(I, j).Value, iPar - 3, 1) 
        Else: sChar = Mid(Cells(I, j).Value, iPar - 2, 1) 
        End If 
       Else: sChar = Mid(Cells(I, j).Value, iPar - 1, 1) 
       End If 
       If IsNumeric(Mid(Cells(I, j).Value, iPar + 1, 2)) Then 
        sBlank = Mid(Cells(I, j).Value, iPar + 1, 2) 
       Else: sBlank = Mid(Cells(I, j).Value, iPar + 1, 1) 
       End If 

       If sBlank + 1 > Len(mystring) Then 
        mystring = mystring & Space(sBlank - Len(mystring)) & sChar 
       Else: mystring = Application.WorksheetFunction.Replace(mystring, sBlank + 1, 1, sChar) 
       End If 
       cont = InStr(iPar + 1, CStr(Cells(1, j).Value), "(") 

      Loop 

     ElseIf InStr(1, CStr(Cells(1, j).Value), "Difference") Then 
      SpacingCode = Space(rng.Cells(I, j)) 
     Else 
     intBeg = Val(Left(Cells(1, j).Value, InStr(1, Cells(1, j).Value, "-") - 1)) 
     intEnd = Val(Right(Cells(1, j).Value, Len(Cells(1, j).Value) - InStr(1, Cells(1, j).Value, "-"))) 
     intCount = 1 
     For t = intBeg To intEnd 
      strArr(t) = Mid(Cells(I, j).Value, intCount, 1) 
      intCount = intCount + 1 
     Next t 
     End If 
    Next j 
    For t = 1 To UBound(strArr) 
     If strArr(t) = "" Then strArr(t) = " " 
     cellValue = cellValue + strArr(t) 
    Next t 
    Erase strArr 
    cellValue = cellValue + SpacingCode 
    cellValue = cellValue + mystring 
    Print #1, cellValue 
    cellValue = "" 
Next I 

Close #1 
Shell "C:\Windows\Notepad.exe C:\Reformatted.txt", 1 
End Sub 

我一直在嘗試了一段時間,但是當存在(和信它似乎沒有工作之間的兩個空格。

因爲只有1個空間,F和G的作品。只有當有多個字母代碼或兩個空格時,它才起作用。謝謝你的時間!

+0

我建議使用助手列和excel TRIM函數,這將刪除所有空格......或者在你的代碼中使用trim(但是如果你可以改變excel數據,它會更快/更容易) – Kdean571

+0

您也可以使用VBA函數'Replace'將雙倍空間轉換爲單個空間。例如:'替換(「測試測試」,「」,「」)'返回'測試測試'。 –

+0

在'L5'中'F'代碼描述之後沒有括號的原因嗎? –

回答

2

看來你的問題只是在最後一欄。這裏是一個UDF,用跟隨零個或多個空格的正則表達式,將

  • 搜索字符串
  • 尋找任何「字」(字母,數字序列,和/或下劃線),然後開放括號標記(
  • 結合這些字序列插入空格分隔字符串

你應該能夠納入你的代碼這一點。

如果您提供了有關代碼可能類型的更多詳細信息,則可能會更改正則表達式,但上述內容似乎適用。

============================================== ===

Function Codes(S As String) As String 
    Dim RE As Object, MC As Object, M As Object 

Set RE = CreateObject("vbscript.regexp") 
With RE 
    .Global = True 
    .Pattern = "\b(\w+)\s*\(" 
    If .test(S) = True Then 
     Set MC = RE.Execute(S) 
     For Each M In MC 
      Codes = Codes & Space(1) & M.submatches(0) 
     Next M 
    End If 
End With 
Codes = Mid(Codes, 2) 
End Function 

======================================== =========

+0

嘿羅恩,我的團隊和我重新用一個用戶表單來解決這個問題,這似乎已經完成了這項工作!雖然謝謝! – Dasman

+0

確實。用戶表單確實可以更好地控制數據輸入。 –

相關問題