Excel宏帮助。基于数组创建新列。

时间:2017-01-02 20:28:05

标签: arrays excel vba excel-vba

我正在尝试在Excel中编写一个宏来创建一个新列,这些状态分为这些区域。我一直收到运行时错误13

这是我到目前为止的代码。

Sub Region ()

Dim Pacific As Variant
Pacific = Array("WA", "OR", "ID", "CA", "NV", "AZ", "NM", "HI", "AK")

Dim Continental As Variant
Continental = Array("AR", "IA", "CO", "KS", "LA", "MS", "MT", "ND", "NE", "OK", "SD", "UT", "WY")

Dim SouthEast As Variant
SouthEast = Array("GA", "AL", "FL", "SC", "KY", "TN")

Dim Midwest As Variant
Midwest = Array("MN", "WI", "IL", "IN", "MI", "OH")

Dim NorthAtlantic As Variant
NorthAtlantic = Array("ME", "NH", "MA", "RI", "CT", "VT", "NY", "PA", "NJ", "DE", "MD", "WV", "VA", "NC")

Dim Texas As Variant
Texas = Array("TX”)

Dim state As String , result As String
score = Range("F1").Value
If state = Pacific Then
    result = "PACIFIC"
ElseIf state = Continental Then
    result = "Continental"
ElseIf state = SouthEast Then
    result = "SouthEast"
ElseIf state = Midwest Then
    result = "Midwest"
ElseIf state = NorthAtlantic Then
    result = "North Atlantic"
ElseIf state = Texas Then
    result = "Texas"
Else
    result = "fail"
End If
Range("Z1").Value = result 
End Sub

2 个答案:

答案 0 :(得分:1)

AFAIK,在数组中搜索字符串的出现并不是VBA中的一个简单问题。您必须使用循环,或者可能使用WorksheetFunction.Match

更简单的方法可能是完全避免数组 - 您的代码可以很容易地重构为使用Select Case语句:

Sub Region ()

    Dim state As String , result As String
    state = Range("F1").Value

    Select Case state
        Case "WA", "OR", "ID", "CA", "NV", "AZ", "NM", "HI", "AK"
            result = "PACIFIC"
        Case "AR", "IA", "CO", "KS", "LA", "MS", "MT", "ND", "NE", "OK", "SD", "UT", "WY"
            result = "Continental"
        Case "GA", "AL", "FL", "SC", "KY", "TN"
            result = "SouthEast"
        Case "MN", "WI", "IL", "IN", "MI", "OH"
            result = "Midwest"
        Case "ME", "NH", "MA", "RI", "CT", "VT", "NY", "PA", "NJ", "DE", "MD", "WV", "VA", "NC"
            result = "North Atlantic"
        Case "TX"
            result = "Texas"
        Case Else
            result = "fail"
    End Select

    Range("Z1").Value = result 
End Sub

注意:您还遇到了两个代码问题。

  1. 你有

    score = Range("F1").Value
    

    当我认为你的意思

    state = Range("F1").Value
    
  2. 您有"TX”而不是"TX" - 我不确定是否会导致您的Excel版本出现问题,但我的确存在问题。

  3. 要扩展此功能以使其适用于F列中的所有单元格,您需要遍历每一行:

    Sub Region ()
    
        Dim state As String , result As String
        Dim lastRow As Long
        Dim r As Long
    
        With ActiveSheet
            lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
            For r = 1 to lastRow
    
                state = .Cells(r, "F").Value
    
                Select Case state
                    Case "WA", "OR", "ID", "CA", "NV", "AZ", "NM", "HI", "AK"
                        result = "PACIFIC"
                    Case "AR", "IA", "CO", "KS", "LA", "MS", "MT", "ND", "NE", "OK", "SD", "UT", "WY"
                        result = "Continental"
                    Case "GA", "AL", "FL", "SC", "KY", "TN"
                        result = "SouthEast"
                    Case "MN", "WI", "IL", "IN", "MI", "OH"
                        result = "Midwest"
                    Case "ME", "NH", "MA", "RI", "CT", "VT", "NY", "PA", "NJ", "DE", "MD", "WV", "VA", "NC"
                        result = "North Atlantic"
                    Case "TX"
                        result = "Texas"
                    Case Else
                        result = "fail"
                End Select
    
                .Cells(r, "Z").Value = result
            Next
        End With
    End Sub
    

答案 1 :(得分:-1)

为什么不使用Access创建表,然后链接到您要创建的其他逻辑表(我假设您编写的代码有一些实际用途)这就是为什么访问是在第一名......