首页 > 解决方案 > 在表中查找特定记录

问题描述

我希望只找到某些记录并相应地处理它们。我有一个名为的表Locations,其中一列标记为Location。目前,当我运行宏时,我的代码会处理所有位置;但是,我想编写只运行某些位置(范围)的宏的变体。位置值都是 2 个字母后跟 5 个数字。示例:CA10020

我想要做的是提示开始和结束位置值,然后只处理那些。示例:CA10001 到 CA13240 这些值之前和之后的所有内容都将被忽略......但这些值以及它们之间的所有内容都将被处理。

通常我很清楚从哪里开始,通过反复试验我可以弄清楚其余的。在这种情况下,我不知道从哪里开始。如您所知,我不是 VBA 专家。

我目前有这段代码可以处理我现在不需要做的所有记录:

For i = 1 To Range("Locations").Rows.Count
    Range("F3").Value = Range("Locations[Location]")(i)
    'I have other code here that handles the new value of cell F3
Next i

更新:我添加了这段代码:

Dim Start_Location As Variant
Dim End_Location As Variant
Start_Location = InputBox("What is the starting location?")
End_Location = InputBox("What is the ending location?")

...但我现在不知道如何处理这些值。

标签: vbaexcel

解决方案


Dim Start_Location As Variant
Dim End_Location As Variant
Start_Location = InputBox("What is the starting location?")
End_Location = InputBox("What is the ending location?")

For i = 1 To Range("Locations").Rows.Count
    If Range("Locations[Location]")(i) >= Start_Location And Range("Locations[Location]")(i) <= End_Location Then

        Range("F3").Value = Range("Locations[Location]")(i)

        'Code to handle the new value of cell F3
    End If

Next i

推荐阅读