在VBA中将整数转换为字符串

时间:2016-07-26 21:18:54

标签: vba typeconverter

以下是整体代码的一些内容,我尝试通过P1运行变量If statement。在If statement结束时,如果值P1不符合先前条件,我会将P1转换为空字符串。

我删除了以下代码的大部分不相关部分,只是为了达到目的......

Sub SLNR
    '<Macro = SLNR>
    '<Shortcut = Shift+Ctrl+S>
    '<Desc = recorded 7/26/2016 12:33:03 PM>
    On Error Resume Next

    ' Individual lines for the data
    Dim L1 As String, L2 As String, L3 As String, L4 As String, L5 As String, L6 As String, L7 As String, _
        L8 As String, L9 As String, L10 As String, L11 As String, L12 As String, L13 As String, _
        L14 As String, L15 As String, L16 As String, L17 As String, L18 As String, L19 As String, _
        L20 As String

    Dim L As Integer    'Left column of data
    Dim R As Integer    'Right column of data
    Dim R2 As Integer   'The data row to be checked
    Dim pL As Integer
    Dim pR As Integer

    Dim SP1 As String
        SP1 = "subj_data_field1"
    Dim SP2 As String
        SP2 = "subj_data_field2"
    Dim SP3 As String
        SP3 = "subj_data_field3"

    Dim P1 As Integer
    Dim P2 As Integer
    Dim P3 As Integer

    Dim E As String      'Carriage Return Key shortcut
        E = Chr$(13)

    L   =   0
    R   =   10
    R2  =   2

    pL = L + 14
    pR = R + 13

    With ActiveSession
        .InputMode = 1
        .Output "MS1" & E
        .WaitFor 0, 1, "W,/) "


    ' Copying the first few characters of each line in LN report, to compare with the phone fields...
    .Copy   L,  R2  +   0   ,   R,  R2  +   0
        L1 = Clipboard
    .Copy   L,  R2  +   1   ,   R,  R2  +   1
        L2 = Clipboard
    .Copy   L,  R2  +   2   ,   R,  R2  +   2
        L3 = Clipboard
    .Copy   L,  R2  +   3   ,   R,  R2  +   3
        L4 = Clipboard
    .Copy   L,  R2  +   4   ,   R,  R2  +   4
        L5 = Clipboard
    .Copy   L,  R2  +   5   ,   R,  R2  +   5
        L6 = Clipboard
    .Copy   L,  R2  +   6   ,   R,  R2  +   6
        L7 = Clipboard
    .Copy   L,  R2  +   7   ,   R,  R2  +   7
        L8 = Clipboard
    .Copy   L,  R2  +   8   ,   R,  R2  +   8
        L9 = Clipboard
    .Copy   L,  R2  +   9   ,   R,  R2  +   9
        L10 = Clipboard
    .Copy   L,  R2  +   10  ,   R,  R2  +   10
        L11 = Clipboard
    .Copy   L,  R2  +   11  ,   R,  R2  +   11
        L12 = Clipboard
    .Copy   L,  R2  +   12  ,   R,  R2  +   12
        L13 = Clipboard
    .Copy   L,  R2  +   13  ,   R,  R2  +   13
        L14 = Clipboard
    .Copy   L,  R2  +   14  ,   R,  R2  +   14
        L15 = Clipboard
    .Copy   L,  R2  +   15  ,   R,  R2  +   15
        L16 = Clipboard
    .Copy   L,  R2  +   16  ,   R,  R2  +   16
        L17 = Clipboard
    .Copy   L,  R2  +   17  ,   R,  R2  +   17
        L18 = Clipboard
    .Copy   L,  R2  +   18  ,   R,  R2  +   18
        L19 = Clipboard
    .Copy   L,  R2  +   19  ,   R,  R2  +   19
        L20 = Clipboard


    ' Finding field for P1
    If L1 = SP1 Then
        .Copy   pL,     R2  +   0   ,   pR,     R2  +   0
            P1 = Clipboard
    ElseIf L2 = SP1 Then
        .Copy   pL,     R2  +   1   ,   pR,     R2  +   1
            P1 = Clipboard
    ElseIf L3 = SP1 Then
        .Copy   pL,     R2  +   2   ,   pR,     R2  +   2
            P1 = Clipboard
    ElseIf L4 = SP1 Then
        .Copy   pL,     R2  +   3   ,   pR,     R2  +   3
            P1 = Clipboard
    ElseIf L5 = SP1 Then
        .Copy   pL,     R2  +   4   ,   pR,     R2  +   4
            P1 = Clipboard
    ElseIf L6 = SP1 Then
        .Copy   pL,     R2  +   5   ,   pR,     R2  +   5
            P1 = Clipboard
    ElseIf L7 = SP1 Then
        .Copy   pL,     R2  +   6   ,   pR,     R2  +   6
            P1 = Clipboard
    ElseIf L8 = SP1 Then
        .Copy   pL,     R2  +   7   ,   pR,     R2  +   7
            P1 = Clipboard
    ElseIf L9 = SP1 Then
        .Copy   pL,     R2  +   8   ,   pR,     R2  +   8
            P1 = Clipboard
    ElseIf L10 = SP1 Then
        .Copy   pL,     R2  +   9   ,   pR,     R2  +   9
            P1 = Clipboard
    ElseIf L11 = SP1 Then
        .Copy   pL,     R2  +   10  ,   pR,     R2  +   10
            P1 = Clipboard
    ElseIf L12 = SP1 Then
        .Copy   pL,     R2  +   11  ,   pR,     R2  +   11
            P1 = Clipboard
    ElseIf L13 = SP1 Then
        .Copy   pL,     R2  +   12  ,   pR,     R2  +   12
            P1 = Clipboard
    ElseIf L14 = SP1 Then
        .Copy   pL,     R2  +   13  ,   pR,     R2  +   13
            P1 = Clipboard
    ElseIf L15 = SP1 Then
        .Copy   pL,     R2  +   14  ,   pR,     R2  +   14
            P1 = Clipboard
    ElseIf L16 = SP1 Then
        .Copy   pL,     R2  +   15  ,   pR,     R2  +   15
            P1 = Clipboard
    ElseIf L17 = SP1 Then
        .Copy   pL,     R2  +   16  ,   pR,     R2  +   16
            P1 = Clipboard
    ElseIf L18 = SP1 Then
        .Copy   pL,     R2  +   17  ,   pR,     R2  +   17
            P1 = Clipboard
    ElseIf L19 = SP1 Then
        .Copy   pL,     R2  +   18  ,   pR,     R2  +   18
            P1 = Clipboard
    ElseIf L20 = SP1 Then
        .Copy   pL,     R2  +   19  ,   pR,     R2  +   19
            P1 = Clipboard
    Else
        CStr(P1)        
        P1 = ""              ' <<<<<<<<********** ISSUE IS HERE
    End If


    ' ......... Code has been trimmed... Showing below the reason I need P1 as an Integer

    If P1 <> "" Then
        Format(P1,000-000-0000)
    End If
    If P2 <> "" Then
        Format(P2,000-000-0000)
    End If
    If P3 <> "" Then
        Format(P3,000-000-0000)
    End If

' CONTINUES ON.....

1 个答案:

答案 0 :(得分:3)

您已将P1声明为整数 - 如果您想在操作中途更改数据类型,则应将其声明为Variant而是使用隐式转换以在执行期间控制数据类型,尽管将另一个变量声明为字符串并使用它更有意义。

此外,它上面的CStr(P1)没有做任何事情 - 您需要将返回值分配回变量:

P1 = CStr(P1)

什么会使更多感觉:

Dim P1 As Integer
Dim P1toString As String

'// More code here

P1toString = CStr(P1)

'// More code here

If P1toString <> "" Then
'// etc etc...