VBA动态搜索模式与动态

时间:2018-10-11 13:22:13

标签: excel vba

我有自己的脚本,该脚本使我可以通过在TextBox中键入内容来搜索数据。 问题是我的是静态的(例如第6至30行)。 如果我添加新行,则脚本将不会使用它。 我想我必须使用变量而不是预定义范围,但是我不知道该怎么做。

最诚挚的问候

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "dbName.md_event",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$and" : [
                {
                    "payload.asset" : {
                        "$eq" : "355565072950945"
                    }
                },
                {
                    "payload.recorded_at" : {
                        "$lte" : "2018-10-02T04:52:25Z"
                    }
                },
                {
                    "payload.recorded_at" : {
                        "$gte" : "2018-10-02T00:00:07Z"
                    }
                }
            ]
        },
        "winningPlan" : {
            "stage" : "FETCH",
            "filter" : {
                "payload.asset" : {
                    "$eq" : "355565072950945"
                }
            },
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "payload.recorded_at" : -1
                },
                "indexName" : "payload.recorded_at_-1",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "payload.recorded_at" : [ ]
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "payload.recorded_at" : [
                        "[\"2018-10-02T04:52:25Z\", \"2018-10-02T00:00:07Z\"]"
                    ]
                }
            }
        },
        "rejectedPlans" : [
            {
                "stage" : "FETCH",
                "filter" : {
                    "$and" : [
                        {
                            "payload.recorded_at" : {
                                "$lte" : "2018-10-02T04:52:25Z"
                            }
                        },
                        {
                            "payload.recorded_at" : {
                                "$gte" : "2018-10-02T00:00:07Z"
                            }
                        }
                    ]
                },
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "keyPattern" : {
                        "payload.asset" : 1
                    },
                    "indexName" : "payload.asset_1",
                    "isMultiKey" : false,
                    "multiKeyPaths" : {
                        "payload.asset" : [ ]
                    },
                    "isUnique" : false,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 2,
                    "direction" : "forward",
                    "indexBounds" : {
                        "payload.asset" : [
                            "[\"355565072950945\", \"355565072950945\"]"
                        ]
                    }
                }
            }
        ]
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 6707,
        "executionTimeMillis" : 2485,
        "totalKeysExamined" : 973001,
        "totalDocsExamined" : 973001,
        "executionStages" : {
            "stage" : "FETCH",
            "filter" : {
                "payload.asset" : {
                    "$eq" : "355565072950945"
                }
            },
            "nReturned" : 6707,
            "executionTimeMillisEstimate" : 2215,
            "works" : 973002,
            "advanced" : 6707,
            "needTime" : 966294,
            "needYield" : 0,
            "saveState" : 7832,
            "restoreState" : 7832,
            "isEOF" : 1,
            "invalidates" : 0,
            "docsExamined" : 973001,
            "alreadyHasObj" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 973001,
                "executionTimeMillisEstimate" : 432,
                "works" : 973002,
                "advanced" : 973001,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 7832,
                "restoreState" : 7832,
                "isEOF" : 1,
                "invalidates" : 0,
                "keyPattern" : {
                    "payload.recorded_at" : -1
                },
                "indexName" : "payload.recorded_at_-1",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "payload.recorded_at" : [ ]
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "payload.recorded_at" : [
                        "[\"2018-10-02T04:52:25Z\", \"2018-10-02T00:00:07Z\"]"
                    ]
                },
                "keysExamined" : 973001,
                "seeks" : 1,
                "dupsTested" : 0,
                "dupsDropped" : 0,
                "seenInvalidated" : 0
            }
        }
    },
    "serverInfo" : {
        "host" : "qwdfwqwqfqw",
        "port" : 27017,
        "version" : "3.4.7",
        "gitVersion" : "cf38c1b8a0a8dca4a11737581beafef4fe120bcd"
    },
    "ok" : 1
}

2 个答案:

答案 0 :(得分:2)

我更喜欢使用For each循环。您还应该始终完全保证您的参考文献合格。

Private Sub TextBox1_Change()
    Application.ScreenUpdating = False
    Dim cell As Range, Target As Range
    With Worksheets("Sheet1")
        Set Target = .Range("E8", .Range("E" & .Rows.Count).End(xlUp))
        Target.Interior.ColorIndex = 24

        For Each cell In Target
            If cell.Value Like "*" & TextBox1 & "*" Then cell.Interior.ColorIndex = 43
        Next

    End With
    Application.ScreenUpdating = True
End Sub

答案 1 :(得分:0)

非常感谢TinMan,我刚刚添加了这个If TextBox1 <> "" Then,否则列E的开头已经是绿色。

此外,您是对的,那是我的参考文献http://www.blog-excel.com/creer-un-champ-de-recherche-vba/

祝你有美好的一天:)

纪尧姆