如何使用数组中find的列的范围作为input,并将范围与数字进行比较,以在不同列中输出为“true”或“false”?

在下面的代码中,我find了数组中值的列号。 如何使用该范围将值与一个variables进行比较?

Public Sub JpFee() Dim rng As Range, rws As Long, w As Long, Str As Variant, Count As Integer, x(20) As Integer, Lt() As Variant, ht As Variant, Wdt As Variant, W8t As Variant Dim row1, i, y As Integer row1 = ActiveSheet.UsedRange.Rows.Count MsgBox row1 Str = Array("length (cm)", "width (cm)", "height (cm)", "unit weight(kg)", "surface area L+W+H", "size") For w = LBound(Str) To UBound(Str) Set rng = Rows("1:1").Find(What:=Str(w), LookAt:=xlWhole, MatchCase:=False) x(i) = rng.Column i = i + 1 MsgBox rng.Column Next w End Sub 

我会使用一个集合这样的东西。 你也可以使用字典。 (无论你喜欢)。

下面是一个使用你想要做什么的集合的例子:

 Sub tgr() Dim ws As Worksheet Dim HeaderCell As Range Dim cHeaders As Collection Set ws = ActiveWorkbook.ActiveSheet Set cHeaders = New Collection On Error Resume Next 'Collections will error if there are duplicates, this prevents that error 'Loop through each headercell For Each HeaderCell In ws.Range("A1").CurrentRegion.Resize(1).Cells 'If the headercell contains text, add its column number to the collection and set the key to the headertext If Len(HeaderCell.Text) > 0 Then cHeaders.Add HeaderCell.Column, CStr(HeaderCell.Text) Next HeaderCell 'Advance the loop On Error GoTo 0 'Clear the On Error Resume Next condition 'Now you can get the column number of a known header by referencing the header in your collection 'This will show the column number of the header "size" 'Note that the header does not need to be case sensitive, but must be the exact text MsgBox cHeaders("size") 'So if you want to put in a value in the bottom row of a column: ws.Cells(ws.Rows.Count, cHeaders("size")).End(xlUp).Offset(1).Value = 2 End Sub