简体   繁体   English

在VBA中散列和传递参数

[英]Hashing and passing arguments in vba

I have vba code were i need the count of cells that meet a specific string pattern. 我有vba代码,我需要满足特定字符串模式的单元格计数。 For example: 例如:

Sub WBR()
    Dim wf As WorksheetFunction
    Set wf = Application.WorksheetFunction
    Dim MinDate As String
    Dim MaxDate As String
    MinDate = InputBox("Minimum Date")
    MaxDate = InputBox("Maximum Date")
    If Not (IsDate(MinDate) And IsDate(MaxDate)) Then
        MsgBox "You should have specified valid dates!"
        Exit Sub
    End If
    If CDate(MinDate) > CDate(MaxDate) Then
        MsgBox "You should have specified sensible dates!"
        Exit Sub
    End If

    With ActiveWorkbook.Worksheets("Latency")
        [AE4] = wf.CountIf(.Range("O:O"), "Pass")
        [AE5] = wf.CountIf(.Range("O:O"), "Fail")
        [AE2] = wf.CountIfs(.Range("K:K"), ">=" & CLng(CDate(MinDate)), _
                .Range("K:K"), "<=" & (CLng(CDate(MaxDate)) + 1), _
                .Range("O:O"), "Pass")
    End With

    With ActiveWorkbook.Worksheets("TT")                
        [AE43] = wf.CountIfs(.Range("I:I"), "<>Duplicate TT", _
              .Range("G:G"), "<>Not Tested", _
              .Range("U:U"), "Item")
    End With

    With ActiveWorkbook.Worksheets("TT")                
        [AE44] = wf.CountIfs(.Range("G:G"), "Not Tested")
    End With

This code searches for the specific text and if satisfied gives the count of that in the designated cell. 此代码搜索特定的文本,如果满意,则给出指定单元格中该文本的计数。

I need to implement the same code for more than 30+ cells. 我需要为30多个单元实现相同的代码。

So how do I create a hash and pass the "keywords" here as an argument and if that condition is met then the count should be updated in the cell. 因此,如何创建哈希并在此处传递“关键字”作为参数,如果满足该条件,则应在单元格中更新计数。

I am struggling to fully understand what your code is doing but you could make a makeshift "hash" by basically having a big string with some separators, and then split those separators at the end... 我正在努力完全理解您的代码在做什么,但是您可以通过基本上有一个带有一些分隔符的大字符串,然后在末尾拆分这些分隔符来做出临时的“哈希”。

YourHash="Key_1:Value_1->Key_2:Value_2->Key_3:Value_3"
Split_1 = Split(YourHash,"->")
For i = 0 to UBound(Split_1)
    Split_2 = Split(Split_1(i),":")
    DesiredKey = Split_2(0)
    DesiredValue = Split_2(1)
Next

Something like that could work for you but I might not be fully understanding what you need. 这样的事情可能对您有用,但我可能还没有完全了解您的需求。

I wonder why you are doing this in VBA to begin with. 我不知道为什么要从VBA开始。 Why not use the worksheetfunctions you have in your VBA code already as normal functions in the cells you are placing their results into and use two cells for start and end date? 为什么不将已在VBA代码中使用的工作表函数用作已放入其结果中的单元格中的普通函数,并使用两个单元格作为开始和结束日期?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM