Excel VBA - 在指定的字符数

时间:2016-07-05 19:01:15

标签: vba excel-vba excel

我正在尝试构建一个vba代码来构建我们用来测试的txt文件。我遇到了一个问题。我的一些结果会有.00,我使用Str = Str & "00" & Left(CashRightJust(Range("h63"), 11), 9)删除了 这基本上是告诉它查看单元格H63,右对齐金额,但如果它是“00”,则向左移动9来降低分数。
我的问题是我们现在需要测试它是否有像.25这样的实际变化。单独使用此代码会在更改结束时添加零。我需要调整此代码以反映它是否超过.00不编辑或添加零

我希望这是有道理的。我仍然相当新,并且已经相当远,但仍有一些时刻我迷失了。谢谢。

Spreadsheet created to build code to send to txt file

    Function Detail_Rec1()




    Dim strlencount As Integer
    Dim strspacer As Integer



    If Range("b63").Value <> "5" Then
        Exit Function
    End If

    Str = Str & Range("b63").Value        **Result: 5**
    Str = Str & Range("c63").Value        **Result: 400**
    Str = Str & Range("d63").Value        **Result: 1234567**    

    Str = Module1.SpaceAdd(Str, 1)        **Result: 1 space**

    Str = Str & Trim(Range("e63").Value)
    strlencount = Len(Trim(Range("e63").Value))
    strspacer = 30 - strlencount
    Str = Module1.SpaceAdd(Str, strspacer) **Result: Company name with spacefill for 30 character; name is left justified**

   Str = Str & Trim(Range("f63").Value)
    strlencount = Len(Trim(Range("f63").Value))
    strspacer = 11 - strlencount
    Str = Module1.SpaceAdd(Str, strspacer)   **Result: Company ID number; left justify; space filled total 11 characters**

     Str = Str & Range("G63").Value          Result: 116

    Str = Str & CashRightJust(Range("h63"), 11)   **Result: 1000; only 1000 no cents; dollars only; 11 character zero filled right justify**
    Str = Str & CashRightJust(Range("i63"), 11)**Result: 1000; only 1000 no cents; dollars only; 11 character zero filled right justify**
    Str = Str & CashRightJust(Range("j63"), 11)**Result: 1000; only 1000 no cents; dollars only; 11 character zero filled right justify**

    Str = Str & Trim(Range("k63").Value)
    strlencount = Len(Trim(Range("k63").Value))
    strspacer = 4 - strlencount
    Str = Module1.ZeroAdd(Str, strspacer)  **Result: Rate of 4 characters entered; 4 character length**


    Str = Module1.SpaceAdd(Str, 1)   **Result: 1 space**

    Str = Str & CashRightJust(Range("l63"), 11)   **Result: 3348.75 needs to be 334875.  11 characters, right justified, no decimal.**  


    Str = Str & CashRightJust(Range("m63"), 11)


    Str = Str & CashRightJust(Range("n63"), 11)


    Str = Str & CashRightJust(Range("o63"), 11)



    Str = Str & "00000" & Right(Range("p63").Value, 6)
    strlencount = Len(Trim(Range("p63").Value))
    strspacer = 6 - strlencount
    Str = Module1.ZeroAdd(Str, strspacer)

[正在编码的行的Excel图像] [文本文件应如何显示] [2] 2]

我需要的最终结果是

23.45 - 2345 23.00 - 2300除非规范只说美元,否则它必须是23 没有四舍五入。 我希望这可以通过视觉效果提供更多帮助

添加信息:我目前使用的$ amts模块如下:

  If Str = 0 Then
        CashRightJust = ZeroAdd(Str2, c)
        Exit Function
    Else
        If InStr(Str, ".") > 0 Then
            Str2 = Right(Str, 2)

            If InStr(Str2, ".") > 0 Then
                strnew = Str & "0"
            Else
                strnew = Str
            End If
        Else
            strnew = Str & "00"
        End If

Excel snapshot of info being coded

54001234567 Bob's Tires 987654321 116000000010000000000005000000009503525 00000334875

这就是它的出现方式: 54001234567 Bob's Tyres 987654321 11600000100000000000000000000000950003525 000334875

我无法发布txt文件的图像我没有足够的声誉;抱歉;这是它应该出现的方式

1 个答案:

答案 0 :(得分:0)

你可以试试这样的东西吗?它假设你的整个字符串都在单元格A1中,并且这是不需要美元和美分的情况(我假设你可以处理它们的情况,因为它听起来你或多或少只留下了字符串那个案子)。

lenOfStr = 11
newStr = ""
subStr = Right(Cells(1, 1), lenOfStr)
If Right(subStr, 2) = "00" Then 'we need to keep these
    lenOfStr = 9
End If
foundLeftmost = False
For i = 1 To lenOfStr
    If Mid(subStr, i, 1) <> "0" and Mid(subStr, i, 1) <> "." Then
        newStr = newStr & Mid(subStr, i, 1) 'start collecting for the new string
        foundLeftmost = True
    End If
    If foundLeftmost and Mid(subStr, i, 1) <> "." Then 'need to include zeros that may show up in the middle of the substring
        newStr = newStr & Mid(subStr, i, 1)
    End If
Next i
subStr = newStr

最终结果存储在subStr中。希望我能正确理解你的问题。如果我没有,请告诉我。