2017-01-02 45 views
1

我想在excel中编写一个宏来创建一个新的列,并将这些状态划分为这些区域。我一直得到运行时错误13Excel宏帮助。基于数组创建一个新列。

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

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 

回答

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,但它在我的。


要使其适用于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 
+0

非常感谢!这工作就像一个魅力! –

+0

嘿,对不起还有一个问题。我如何使这适用于整个列? –

+0

@TimothyYelverton - 答案更新,以演示一个循环 – YowE3K

-1

EM为什么不,叔您使用Access创建的表像你一样,然后链接到你要创建(我相信有你写的代码的一些实际使用)其他的逻辑表这就是为什么访问被创建在第一个地方......

+2

这个答案并不能真正帮助的OP。回答关于Excel的(完全有效的)问题“使用Access,这就是为什么创建它”实际上并不是非常有用。 – MichaelMilom

+0

MichaelMilom哈哈... – Nicolas