String 字符串格式的疯狂问题

String 字符串格式的疯狂问题,string,vba,String,Vba,我遇到了一个让我发疯的问题。我的宏中有两个FOR循环,每个FOR循环都有一个计数器来跟踪某个进程执行了多少次。计数器工作正常,循环结束时包含正确的数字。接下来我要做的是将计数格式化为一个带前导零的五位数。我尝试了两种不同的方法(见下文) 或 问题在于第二个计数器。在我逐步介绍时,第一个格式公式起作用,但第二行不起作用,无论我使用上面的哪个版本。现在的问题是,如果在我还在宏中的时候,我将mCount的名称改为其他名称,例如mCnt,然后将宏向上移动以重新处理该行,它将正确格式化变量。但它不是名称,

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

问题在于第二个计数器。在我逐步介绍时,第一个格式公式起作用,但第二行不起作用,无论我使用上面的哪个版本。现在的问题是,如果在我还在宏中的时候,我将mCount的名称改为其他名称,例如mCnt,然后将宏向上移动以重新处理该行,它将正确格式化变量。但它不是名称,因为如果我使用mCnt再次运行宏,它也会执行相同的操作。我可以把它改回mCount,它会工作的

所有变量都变暗为整数。我正在寻找的一个例子是,如果mTemp是15,那么mCount将是00015。然而,McCount刚刚恢复15岁。帐户工作正常

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

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

问题在于如何声明变量

在VBA/classic vb中,所有声明都应该在自己的行上,或者指定了正确的数据类型,否则可能会意外创建变量数据类型,该变量数据类型可能伪装为任何其他数据类型,而vb引擎有确定该类型的规则

此外,每当在VBA中编码时,请确保在任何新代码模块的顶部声明Option Explicit。这将在将来为你省去很多痛苦

此外,您正在尝试将字符串格式转换为整数,这是不可能的

所以

在代码中,为什么不直接内联格式化

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

帐户、cTemp、mCount和mTemp的类型是什么?什么是mCount的错误输出?我编辑了这个问题以包含这个。谢谢。也许我误解了这个问题。“整数”不是这个问题的答案吗?它们是整数,而不是字符串,所以您应该将字符串格式应用到整数中。如果希望显示字符串值,请创建一个字符串变量,然后使用它。
Len(ctemp)
看起来有点奇怪?获取整数的长度?例如,以下命令从一个整数
Dim a返回一个长度为2的整数a=1 Debug.Print Len(a)
Debug.Print Len(CStr(a))
返回预期结果。现在进行测试。只是好奇。调暗所有变量有多重要?何时知道哪些变量需要调暗?我知道大多数代码在没有它的情况下都可以正常工作,而且每个小计数器都有自己的暗线,代码可能会很长。我很好奇,因为我在这个问题上学到了一些新的东西。在你遇到像这样微妙的错误之前,这并不重要:)…最佳实践是总是将所有变量和数据类型声明为你所期望的。还更新了答案,包括如何在一行上正确声明多个。确定。现在一切都像我期待的那样。我不得不处理一堆其他变量,因为一旦我给了它们所有正确的声明,就会出现各种类型的不匹配。不过,我现在已经把一切都理顺了。谢谢你的帮助。
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
   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