如何使Excel电子表格范围存储VBA变量?

时间:2015-07-10 00:32:56

标签: string excel vba parsing substring

好的,我已经花了......我只是说...就像一个小时(可悲的是我撒谎,就像整整一周)试图解决这个问题。 我无法弄清楚godd * mnit> _<

假设我们在记事本(_A_File_.txt)中有一个只包含4行的文本文件:

ACCOUNT NUMBER: 123456789    '(line 2)
SHORT NAME: JON SMITH        '(line 2)
ACCOUNT NUMBER: 987654321    '(line 3)
SHORT NAME: BOB BARKER       '(line 4)

让我们假设在excel电子表格中如下:

Range C1 = "actNumberTrim"
Range C2 = "shortNameTrim"
Range C3 = "actNumberTrim"
Range C4 = "shortNameTrim"

让我们假设以下VBA脚本:

Sub obey_me_you_stoopit_code_()

Dim actNumber As Integer
Dim shortName As Integer
Dim actNumberTrim As String
Dim shortNameTrim As String
Dim trimArray1 As Variant
Dim myFile As String
Dim text As String
Dim textline As String


actNumber = InStr(text, "ACCOUNT NUMBER: ") 'THERE IS A SPACE BETWEEN : AND "
shortName = InStr(text, "SHORT NAME: ") 'THERE IS A SPACE BETWEEN : AND "
actNumberTrim = Mid(text, actNumber + 16, 10)
shortNameTrim = Mid(text, shortName + 12, 10)


myFile = "C:\Users\Bob\Desktop\_A_File_.txt"

Open myFile For Input As #1
Do Until EOF(1)

    Line Input #1, textline
    text = text & textline
    Loop

trimArray1 = ThisWorkbook.Worksheets("sheet1").Range("C1:C4")


For i = 1 To UBound(trimArray1)

 MsgBox trimArray1(i, 1)

Next i
Close #1
End Sub

我得到的输出是:

[ actNumberTrim ]
[ shortNameTrim ]
[ actNumberTrim ]
[ shortNameTrim ]

我如何修复上述内容,以便将范围C1:C4中的值视为:

  1. 变量而不是,比如说......单元格中没有意义的文字
  2. 因此返回的输出为:

    [ 123456789 ]
    [ JON SMITH ]
    [ 987654321 ]
    [ BOB BARKER ]
    
  3. 我尝试更改actNumberTrimshortNameTrim的数据类型。 它没有用 我得到的就是这个:

      

    运行时错误:"您的代码很糟糕"

    思想?

    编辑1 (尝试使用Microsoft Scripting Runtime)

        Dim trimArray1 As Variant
        Dim myFile1 As String
        Dim text As String
        Dim textline As String
        Dim myDict As New Dictionary
    
        myFile1 = "C:\Users\BOB\Desktop\_A_File_.txt"
        Open myFile1 For Input As #1
        Do Until EOF(1)
    
            Line Input #1, textline
            text = text & textline
            Loop
        Close #1
        trimArray1 = ThisWorkbook.Worksheets("sheet22").Range("C1:C4")
        v = Split(textline, ":") 'where v has been declared to be variant
        myDict.Add Trim(v(0)), Trim(v(1))
        For i = 1 To UBound(trimArray1)
    
        MsgBox myDict(trimArray1(i, 1))
    
        Next i
    
    
        [ BLANK ]
        [ BOB BARKER ]
        [ BLANK ]
        [ BOB BARKER ]
    

    细胞C1:C4含有:

        ACCOUNT NUMBER
        SHORT NAME
        ACCOUNT NUMBER
        SHORT NAME
    

    不确定我做错了什么或者我是否忽略了什么。如何将此方法用于没有"的字段:"分开他们?我发布的文本文件的版本有点过于简单了。反馈

    编辑2 以下是我最初的方法

    以下是示例文本文件中两个完整记录的示例:

        ACCOUNT ABCDEF12                                                                 
        CUSTOMER NAME:    JOHN B. SMITH         CSA REP:  154983                   
        ACCOUNT OPEN:     05/10/15
        CUSTOMER ADDRESS: 123 SOMEWHERE DRIVE   SOMETHING HERE:                   
        LAST ORDER:       06/24/2011             COUNTRY CODE: UNITED STATES      
        INVOICE #:        123456789             STATE CODE:    CALIFORNIA         
        LAST MAINTENANCE: 01/02/15               COUNTY CODE:  UNCODED            
        SOME INDICATOR:   NO   COMPLAINTS: NO   IPM IND:       DATAPREP/PERF4     
        SOME INDICATOR:   NO STATUS:  NONE      AUTO RENEW:    YES                
        SOMETHING HERE   NO                             
        SOMETHING HERE:          ABC IND:       
        SOMETHING HERE   2    ABC ASSET NO:  T                                           
    
        ACCOUNT ABCDEF12                                                                 
        CUSTOMER NAME:    JOHN B. SMITH         CSA REP:  154983                   
        ACCOUNT OPEN:     05/10/15
        CUSTOMER ADDRESS: 123 SOMEWHERE DRIVE   SOMETHING HERE:                   
        LAST ORDER:       06/24/2011             COUNTRY CODE: UNITED STATES      
        INVOICE #:        123456789             STATE CODE:    CALIFORNIA         
        LAST MAINTENANCE: 01/02/15               COUNTY CODE:  UNCODED            
        SOME INDICATOR:   NO   COMPLAINTS: NO   IPM IND:       DATAPREP/PERF4     
        SOME INDICATOR:   NO STATUS:  NONE      AUTO RENEW:    YES                
        SOMETHING HERE:   NO                             
        SOMETHING HERE:          ABC IND:       
        SOMETHING HERE:   2    ABC ASSET NO:  T                                               
    

    以下是我最初编码的方式:

    '
        Dim myFile As String
        Dim text As String
        Dim textline As String
        Dim cstAct as integer
        Dim actOpe as integer
        Dim cusNam as integer
        Dim act as integer
        Dim reg as integer
    
        myFile = "put file patch to text file here"
        myFile = Application.GetOpenFilename()
    
        Do Until EOF(1)
        Line Input #1, textline
        text = text & textline
        Loop
    
        cusAct = InStr(text, "ACCOUNT ")
        actOpe = InStr(text, "ACCOUNT OPEN:")
        reg = InStr(text, "REGION:")
        cusNam = InStr(text, "CUSTOMER NAME:")
    
        For i = 2 To ThisWorkbook.Worksheets("b2").Range("a65536").End(xlUp).Row
        ThisWorkbook.Worksheets("name").Range("a" & i).Value = Mid(text, act + 6, 9)
        ThisWorkbook.Worksheets("name").Range("b" & i).Value = Mid(text, cstAct + 6, 9)
        ThisWorkbook.Worksheets("name").Range("c" & i).Value = Mid(text, actOpe + 13, 27)
        ThisWorkbook.Worksheets("name").Range("d" & i).Value = Mid(text, cusNam  + 20, 19)
    
        next i
    
        'Format and autofit                         
        For x = 2 To ThisWorkbook.Worksheets("b2").Range("a65536").End(xlUp).Row
        Range("a" & x).Value = Application.WorksheetFunction.Clean(trim(Range("a" & x)))
        Range("b" & x).Value = Application.WorksheetFunction.Clean(trim(Range("b" & x)))
        Range("c" & x).Value = Application.WorksheetFunction.Clean(trim(Range("c" & x)))
        'etc etc
        next x
    

1 个答案:

答案 0 :(得分:1)

您可以将字符串作为用于字典(其行为类似于链接到相应的变量)。在“工具/参考”下的VBA编辑器中,包括对 Microsoft Scripting Runtime 的引用。然后在代码的顶部有一行像

Dim myDict As New Dictionary 

然后,当您遍历文件并通过连续行加载变量textline时,不要将其添加到大变量的末尾,而是使用行

v = Split(textline, ":") 'where v has been declared to be variant
myDict.Add Trim(v(0)), Trim(v(1))

将单元格固定在C列中,使它们与冒号前文本文件中的实际(修剪)字符串重合。无需消除空格 - "Account Number"是完全有效的单元格值和完全有效的字典键。稍后,当您循环遍历从C列中提取的值时,请替换

MsgBox trimArray1(i, 1)

通过

MsgBox myDict(trimArray1(i, 1))

这应该(如果我理解你的意图正确的话)做你想做的事。