Vba 替换if语句或使用集合
早些时候,我问了一个问题,但没有得到正确答案。我想我无法恰当地表达自己。这是我面临的一个问题:Vba 替换if语句或使用集合,vba,excel,if-statement,collections,excel-2013,Vba,Excel,If Statement,Collections,Excel 2013,早些时候,我问了一个问题,但没有得到正确答案。我想我无法恰当地表达自己。这是我面临的一个问题: Sub organize() Dim GroupID(1 To 30) As String Dim GroupAanwezig As Long Dim class As String Dim i, j, t, totally, countCol As Integer Dim GroupenCol As New Collection i = 1
Sub organize()
Dim GroupID(1 To 30) As String
Dim GroupAanwezig As Long
Dim class As String
Dim i, j, t, totally, countCol As Integer
Dim GroupenCol As New Collection
i = 1
t = 1
rn_totallist = Worksheets("totallist").Cells.SpecialCells(xlCellTypeLastCell).Row
nxA_9N4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_9N4")
nxA_1A2A = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1A2A")
nxA_1A2B = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1A2B")
nxA_2A2 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_2A2")
nxA_2A3 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_2A3")
nxA_3A3 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_3A3")
nxA_1B4A = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1B4A")
nxA_1B4B = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1B4B")
nxA_1B4C = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1B4C")
nxA_1B4D = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1B4D")
nxA_2G4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_2G4")
nxA_3G4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_3G4")
nxA_4G4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_4G4")
nxA_2A4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_2A4")
nxA_3A4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_3A4")
nxA_4A4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_4A4")
nxA_1E3 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1E3")
nxA_1E4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_1E4")
nxA_2E4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_2E4")
nxA_3e3 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_3E3")
nxA_3e4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_3E4")
nxA_4e4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "xA_4E4")
totalstd = nxA_9N4 + nxA_1A2A + nxA_1A2B + nxA_2A2 + nxA_2A3 + nxA_3A3 + nxA_1B4A + nxA_1B4B + nxA_1B4C + nxA_1B4D + nxA_2G4 + nxA_3G4 + nxA_4G4 + nxA_2A4 + nxA_3A4 + nxA_4A4 + nxA_1E3 + nxA_1E4 + nxA_2E4 + nxA_3e3 + nxA_3e4 + nxA_4e4
GroupenCol.Add nxA_9N4
GroupenCol.Add nxA_1A2A
GroupenCol.Add nxA_1A2B
GroupenCol.Add nxA_2A2
GroupenCol.Add nxA_2A3
GroupenCol.Add nxA_3A3
GroupenCol.Add nxA_1B4A
GroupenCol.Add nxA_1B4B
GroupenCol.Add nxA_1B4C
GroupenCol.Add nxA_1B4D
GroupenCol.Add nxA_2G4
GroupenCol.Add nxA_3G4
GroupenCol.Add nxA_4G4
GroupenCol.Add nxA_2A4
GroupenCol.Add nxA_3A4
GroupenCol.Add nxA_4A4
GroupenCol.Add nxA_1E3
GroupenCol.Add nxA_1E4
GroupenCol.Add nxA_2E4
GroupenCol.Add nxA_3e3
GroupenCol.Add nxA_3e4
GroupenCol.Add nxA_4e4
LastRow = Sheets("BLS").Cells.SpecialCells(xlCellTypeLastCell).Row
For i = 2 To LastRow
LastCol = Sheets("BLS").Cells(i, Columns.count).End(xlToLeft).Column
For j = 2 To LastCol
class = Sheets("BLS").Cells(i, j).Value
GroupAanwezig = WorksheetFunction.CountIf(Range("B1:D18"), class)
BLSers = Sheets("BLS").Cells(i, 1).Value
If InStr(nshow, class) = 0 Then
GroupID(t) = class
nshow = nshow & " " & GroupAanwezig & " " & t & " " & GroupID(t) & vbCrLf
t = t + 1
End If
countCol = Sheets("BLS").Cells(i, Columns.count).End(xlToLeft).Column
lastcolumn = countCol - 1
Select Case lastcolumn
Case Is = 0
'MsgBox "EMPTY"
Case Is = 1
totally = lastcolumn * 30
Case Is = 2
totally = ((lastcolumn * 10) - 5) * 2
Case Is = 3
totally = lastcolumn * 10
nclass = "n" + class
MsgBox nclass
'---------------------------------------------------------
'there has to be a faster way to do the if-statement below.
'---------------------------------------------------------
If class = "xA_9N4" Then
MsgBox nxA_9N4
End If
If class = "xA_4A4" Then
MsgBox nxA_4A4
End If
If class = "xA_3A4" Then
MsgBox nxA_3A4
End If
If class = "xA_1B4B" Then
MsgBox nxA_1B4B
End If
If class = "xA_1A2B" Then
MsgBox nxA_1A2B
End If
If class = "xA_2A4" Then
MsgBox nxA_2A4
End If
If class = "xA_2E4" Then
MsgBox nxA_2E4
End If
If class = "xA_1E3" Then
MsgBox nxA_1E3
End If
'--------------------------------------------------------
End Select
End Sub
在最后一种情况下(case is=3),有一个if语句。此if语句是shor版本(需要在案例0、案例1和案例2中重复)。我需要处理返回值(例如)nxA_9N4。在先前解决问题的尝试中,我得到了以下方面的帮助:
工程师:他告诉我要用收藏品
我使用了一个集合,但仍然:(
评论和答复: -如果cell.value与类具有相同的值(始终如此),则我将从另一个工作表返回该值。该值对名为“xA4A”的组/类进行计数,并将计数值返回为nxA4A。使用IFS语句,我想知道class=“xA4A”是否为nxA4A,然后给出nxA4A的计数 顺便说一句,eval函数在2013年不起作用。如果您查看该方法,您会发现它最多需要4个参数。第一个参数是要添加的对象(正如您已经做的那样),但第二个可选参数是键-用于在集合中标识此对象的唯一字符串值 因此,如果您将项目添加到集合中,如下所示:
GroupenCol.Add nxA_9N4,“xA_9N4”
如果在名为“class”的变量中有所需的字符串值,则可以如下方式访问集合:
MSGBoxGroupEncol(类)
或者这个:
msgboxgroupencol.Item(类)
(它们是等效的,因为
项
是集合的默认属性
)当前代码有什么问题?在执行过程时,逻辑是否按预期工作并显示消息框?您试图用if语句实现什么?似乎是正确的。我在VBS中测试了我的答案,没有问题,但VBA for excel缺少,尽管它似乎在access中工作:/…已编辑。查看code,看起来你很接近了。我修改了GroupenCol.Add
,添加了第二个参数(一个键),它是第一个参数的字符串版本,没有n:GroupenCol.Add nxA_9N4,“xA_9N4”
,然后,我使用MsgBox GroupenCol.Item(class)代替If
s
但是,我会推迟回答,直到在Excel中正确测试。回复:非常感谢它能工作:)我遇到的错误是内存不足。这和我忘了加上的引号有关。。。谢谢barrowc和你们其他人