字符串格式

时间:2015-10-27 13:25:27

标签: string vba

我遇到了一个令我疯狂的问题。我的宏中有两个FOR循环,每个循环都有一个计数器来跟踪某个进程执行了多少次。计数器工作得很好,在循环结束时包含正确的数字。接下来我要做的是将计数格式化为带有前导零的五位数字。我使用两种不同的方法尝试了这一点(见下文)。

cCount = String(5 - Len(cTemp), "0") & cTemp
mCount = String(5 - Len(mTemp), "0") & mTemp

cCount = Format(cTemp, "00000")
mCount = Format(mTemp, "00000")

问题在于第二个计数器。当我逐步完成它时,第一个格式公式可行,但第二行不起作用,无论我使用哪个版本。现在就是这样,如果,当我还在宏中时,我会将mCount的名称更改为其他任何内容,例如mCnt,然后将宏步骤移回以重新处理该行,它将正确格式化变量。但它不是名字,因为如果我再用mCnt运行宏,它会做同样的事情。我可以将它改回mCount,它会工作。

所有变量都以整数形式变暗。我正在寻找的一个例子是,如果mTemp是15,那么mCount将是00015。但是,mCount刚刚回来为15. cCount工作正常。

事实上一切都是正确的,如果我暂停宏,更改变量名称并重新处理行,我可以使它工作,这让我完全不知道问题是什么。

Sub MakePay()

Dim strFileToOpen As String
Dim payDate, payTab, payCheckTemp, payCheck, payAccTemp As String
Dim payAcc, payAmount, payTotalC, payTotalM As String
Dim savePath As String
Dim payFileNameCLP, payFileNameMF As String
Dim payString1, payString2, payString3, payString4, payString5, payString6 As String
Dim payString7, payString8, payString9 As String
Dim rCnt, i, j, cTemp, cCount, mTemp, mCount As Integer
Dim payTotalMTemp, payAmountTemp, payTotalCTemp As Double


' Set date
payDate = Format(Now(), "yyyymmddhhmmss")
' Ask for check number and format to field length
payCheckTemp = InputBox("Please enter the check number.")
payCheck = payCheckTemp & String(15 - Len(payCheckTemp), " ")
' Create file names and open text files for writing
payFileNameCLP = "CLP_" & payDate & "_01.txt"
payFileNameMF = "MDF_" & payDate & "_01.txt"
savePath = Environ("USERPROFILE") & "\Desktop\"
Open savePath & payFileNameCLP For Output As #1
Open savePath & payFileNameMF For Output As #2

' Build header rows and print them
payString1 = "100"
payString2 = "200          C"
Print #1, payString1
Print #1, payString2
Print #2, payString1
Print #2, payString2

' reset counters for number of claims and total dollar amounts in files
cTemp = 0
mTemp = 0
payTotalCTemp = 0
payTotalMTemp = 0

For i = 1 To Sheets.Count
    ' Process the Clearpoint tab
    If Left(Sheets(i).Name, 3) = "CLE" Then
        Sheets(i).Activate
        rCnt = Cells(Rows.Count, 1).End(xlUp).Row
        For j = 3 To (rCnt - 1)
            ' Read accession # and format it for field length
            payAccTemp = Cells(j, 3).Value
            payAcc = payAccTemp & String(17 - Len(payAccTemp), " ")
            ' Read payment amount, if $0, skip
            payAmountTemp = Format(Cells(j, 5).Value2, "#,###.00")
            If payAmountTemp = "" Then
                GoTo SkipCDL
            End If
            ' Add payment to total Clearpoint payments
            payTotalCTemp = payTotalCTemp + payAmountTemp
            ' Format payment by deleting decimal and then format to field length
            payAmount = Format(payAmountTemp * 100, "0000000;-000000")

            ' Build payment strings and print them
            payString3 = "400" & String(10, " ") & payAcc & payCheck
            payString4 = "450" & String(10, " ") & payAcc & String(150, " ") & payAmount
            payString5 = "500" & String(10, " ") & payAcc & String(73, " ") & payAmount
            Print #1, payString3
            Print #1, payString4
            Print #1, payString5
            ' Increase Clearpoint patient count
            cTemp = cTemp + 1
SkipCDL:
        Next j
    ' Process Medfusion tab
    ElseIf Left(Sheets(i).Name, 3) = "MED" Then
        Sheets(i).Activate
        rCnt = Cells(Rows.Count, 1).End(xlUp).Row
        For j = 3 To (rCnt - 1)
            ' Read accession # and format it for field length
            payAccTemp = Cells(j, 3).Value
            payAcc = payAccTemp & String(17 - Len(payAccTemp), " ")
            ' Read payment amount, if $0, skip
            payAmountTemp = Format(Cells(j, 5).Value2, "#,###.00")
            If payAmountTemp = "" Then
                GoTo SkipMDF
            End If
            ' Add payment to total Medfusion payments
            payTotalMTemp = payTotalMTemp + payAmountTemp
            ' Format payment by deleting decimal and then format to field length
            payAmount = Format(payAmountTemp * 100, "0000000;-000000")

            ' Build payment strings and print them
            payString3 = "400" & String(10, " ") & payAcc & payCheck
            payString4 = "450" & String(10, " ") & payAcc & String(150, " ") & payAmount
            payString5 = "500" & String(10, " ") & payAcc & String(73, " ") & payAmount
            Print #2, payString3
            Print #2, payString4
            Print #2, payString5
            ' Increase Medfusion count
            mTemp = mTemp + 1
SkipMDF:
        Next j
    End If
Next i

' Format patient counter and total payment to field length

cCount = Format(cTemp, "00000")
mCount = Format(mTemp, "00000")
payTotalC = Format(payTotalCTemp * 100, "000000000;-00000000")
payTotalM = Format(payTotalMTemp * 100, "000000000;-00000000")


' Build footer strings and print them
payString6 = "800" & String(26, " ") & "9999" & cCount & String(131, " ") & payTotalC
payString7 = "800" & String(26, " ") & "9999" & mCount & String(131, " ") & payTotalM
payString8 = "900" & String(57, " ") & "099990" & cCount & String(154, " ") & String(2, "0") & payTotalC
payString9 = "900" & String(57, " ") & "099990" & mCount & String(154, " ") & String(2, "0") & payTotalM
Print #1, payString6
Print #2, payString7
Print #1, payString8
Print #2, payString9
' Close all files
Application.DisplayAlerts = False

    Close #1
    Close #2
Application.DisplayAlerts = True

End Sub

1 个答案:

答案 0 :(得分:4)

问题在于如何声明变量。

在VBA / classic vb中,所有声明都应该在它们自己的行上或者指定了正确的数据类型,否则你可能会意外地创建一个Variant数据类型,它可以伪装成任何其他数据类型,VB引擎有规则确定类型。

请参阅https://msdn.microsoft.com/en-us/library/56ht941f(v=vs.90).aspx

此外,每当在VBA中进行编码时,都要确保在任何新代码模块的顶部声明Option Explicit。它将在未来为您节省大量的痛苦。

此外,您正在尝试将字符串格式推送到整数中,这是不可能发生的。

因此...

   Option  Explicit
    .....

    'Dim i, j as Integer 'BAD i is a variant, j is an integer
    Dim i As Integer
    Dim j As Integer 'GOOD both are Integers
    'OR
    Dim x As Integer, y as Integer 'I believe this will work too

    dim displayI as String
    i = 23
    displayI = Format(i, "00000")

在您的代码中,为什么不只是内联格式?

payString6 = "800" & String(26, " ") & "9999" & Format(cCount,"00000") & String(131, " ") & payTotalC