首先,我是个菜鸟。我昨天刚第一次看上去像个VBA。所以我很感激你能哑口无言。LOL。
我每周有几次在工作中得到一张纸。我必须拆分邮政编码,然后把它们移到透视商店。有大约20个邮政编码,虽然我使用排序选项,它仍然需要我一段时间。我想用马可给每个邮政编码一个商店。
这是我的问题。如果邮政编码与我想要用"J1“写成的”胡说八道“中的一个相匹配,我正在试着查看"M1”
我能够做到这一点,我花了几个小时的尝试和错误,才得到了最好的结果。我尝试了很多不同的东西。(最下面是我想出来的)
这就是问题所在。我得把这件事一直做下去。即。如果m3 = 86409 J3 = Kingman。如果m4 = 86409 j4 = Kingman。就这样一直到M5000,J5000。
任何帮助都将不胜感激。我想做的事情很简单,但我自己找不到答案,或者我无法理解。我想我得重新开始了。采取不同的方法。不知道怎么回事。
Sub MoversBirthdays()
Dim zipcode As Long, Store As String
zipcode = Range("J2").Value
If zipcode = "86426" Or "86427" Or "86429" Or "86430" Or "86435" Or "86436" Or "86437" Or "86438" Or "86439" Or "86440" Or "86442" Or "86446" Or "89028" Or "89029" Or "89046" Or "92304" Or "92332" Or "92363" Then Store = "Bullhead" Else: Store = "Kingman"
If zipcode = "" Then Store = ""
Range("M2").Value = Store
End Sub 发布于 2013-09-04 14:39:15
Sub MoversBirthdays()
Dim varZip As Variant
Dim arrStore() As String
Dim StoreIndex As Long
With Range("J2", Cells(Rows.Count, "J").End(xlUp))
If .Row < 2 Then Exit Sub 'No data
ReDim arrStore(1 To .Rows.Count)
For Each varZip In .Value
StoreIndex = StoreIndex + 1
Select Case varZip
Case 86426 To 86427, 86429 To 86430, 86435 To 86440, 86442, 86446, 89028 To 89029, 89046, 92304, 92332, 92363
arrStore(StoreIndex) = "Bullhead"
Case ""
arrStore(StoreIndex) = ""
Case Else
arrStore(StoreIndex) = "Kingman"
End Select
Next varZip
End With
If StoreIndex > 0 Then Range("M2").Resize(StoreIndex).Value = Application.Transpose(arrStore)
End Sub发布于 2013-09-04 14:51:16
您的代码有几个问题。
首先,将zipcode定义为Long,但将其与字符串进行比较。应该将其定义为字符串(或与Longs进行比较)。
第二,不能使用值列表执行If。它解析,但被解释为If zipcode is 86426; Or If 86427; Or If 86429...。实际上,您需要为每个zip指定If zipcode = "xxx"。
但是,由于您使用的是Excel,所以可以将压缩列表放在工作表上,然后引用它进行比较。假设列表在A列中,下面的代码将满足您的需要:
Sub MoversBirthdays2()
Dim zipcode As String, Store As String
Dim c As Range
For Each c In Range("j2:j5000")
zipcode = c.Value
If zipcode = "" Then
Store = ""
Else
If WorksheetFunction.CountIf(Range("a:a"), zipcode) > 0 Then
Store = "Bullhead"
Else
Store = "Kingman"
End If
End If
c.Offset(0, 2) = Store
Next
End Sub如果实际上不需要用空格覆盖M列中的值(如果zip是空的),那么它可以更简单一些:
Sub MoversBirthdays2()
Dim zipcode As String, Store As String
Dim c As Range
For Each c In Range("j2:j5000")
zipcode = c.Value
If zipcode <> "" Then
If WorksheetFunction.CountIf(Range("a:a"), zipcode) > 0 Then
Store = "Bullhead"
Else
Store = "Kingman"
End If
c.Offset(0, 2) = Store
End If
Next
End Subhttps://stackoverflow.com/questions/18616465
复制相似问题