将数组中的字符串与范围 Excel VBA 相匹配

发布于 2024-11-28 03:53:38 字数 1405 浏览 4 评论 0原文

我有以下 Public sub:

Public Sub HowToSort()
Dim i As Long, j As Long, h As Long, curCell As Range, cellBelow(1 To 10) As Variant
Dim sortOrder(1 To 10), colIsString(1 To 10) As Variant

For i = 1 To hdrCount
    'Find location of a cell
    Set curCell = Application.WorksheetFunction.Match(headRow(i), Range("a1:z1"))    ' Eventually extend outwards?
    cellBelow(i) = curCell.Offset(0, 1).Value
    If IsNumeric(cellBelow(i)) = False Then
        colIsString(i) = True
        sortOrder(i) = Application.InputBox(prompt:="Alphabetical = 'True' or Reverse Alphabetical = 'False' sorting for " & headRow(i), Type:=4)
    ElseIf IsNumeric(cellBelow(i)) = True Then
        colIsString(i) = False
        sortOrder(i) = Application.InputBox(prompt:="Ascending = True or Descending = False for " & headRow(i), Type:=4)
    Else
        MsgBox ("Program does not recognize value contained in column" & headRow(i))
        End
    End If
Next i
End Sub

它使用名为 headRow 的全局变量,其中包含工作表顶部标题行名称的字符串数组。我正在尝试使用匹配函数来查找标题所在单元格的地址:

Set curCell = Application.WorksheetFunction.Match(headRow(i), Range("a1:z1"))
cellBelow(i) = curCell.Offset(0, 1).Value

然后我想使用该地址,将其向下偏移一个单元格以查找输入的数据类型,该数据将输入到数组中colIsString。但是,.Match 函数无法正常工作,并引用“类型不匹配”错误。我不知道怎么会这样?根据我之前的研究,.Match 命令接受一个范围,然后搜索该范围以匹配单元格值。我尝试了 .Match 命令的几种变体,但没有成功。感谢您的想法...

H3lue

I have the following Public sub:

Public Sub HowToSort()
Dim i As Long, j As Long, h As Long, curCell As Range, cellBelow(1 To 10) As Variant
Dim sortOrder(1 To 10), colIsString(1 To 10) As Variant

For i = 1 To hdrCount
    'Find location of a cell
    Set curCell = Application.WorksheetFunction.Match(headRow(i), Range("a1:z1"))    ' Eventually extend outwards?
    cellBelow(i) = curCell.Offset(0, 1).Value
    If IsNumeric(cellBelow(i)) = False Then
        colIsString(i) = True
        sortOrder(i) = Application.InputBox(prompt:="Alphabetical = 'True' or Reverse Alphabetical = 'False' sorting for " & headRow(i), Type:=4)
    ElseIf IsNumeric(cellBelow(i)) = True Then
        colIsString(i) = False
        sortOrder(i) = Application.InputBox(prompt:="Ascending = True or Descending = False for " & headRow(i), Type:=4)
    Else
        MsgBox ("Program does not recognize value contained in column" & headRow(i))
        End
    End If
Next i
End Sub

Which uses a global variable named headRow, containing an array of strings of names of the header row at the top of the worksheet. I am trying to use the match function to find the address of the cell where the header is located:

Set curCell = Application.WorksheetFunction.Match(headRow(i), Range("a1:z1"))
cellBelow(i) = curCell.Offset(0, 1).Value

I then want to use this address, offset it downwards one cell to find what type of data is entered, this data will be entered in array colIsString. However, the .Match function is not working, citing a 'Type Mismatch' error. I do not know how this could be? From my previous research it appears that the .Match command takes in a range, then searches that range to match a cell value. I have tried several incarnations of the .Match command with no success. Your thoughts appreciated...

H3lue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何处潇湘 2024-12-05 03:53:38

使用 Find() 代替:

Set curCell = Range("a1:z1").Find(headRow(i), , xlValues, xlWhole)
If Not curCell Is Nothing Then
    'found the header
    cellBelow(i) = curCell.Offset(0, 1).Value
    'etc etc
Else
    MsgBox "Header '" & headRow(i) & "' not found!"
End If

一旦你的子程序退出,sortOrder 和 colIsString 就会超出范围......

Use Find() instead:

Set curCell = Range("a1:z1").Find(headRow(i), , xlValues, xlWhole)
If Not curCell Is Nothing Then
    'found the header
    cellBelow(i) = curCell.Offset(0, 1).Value
    'etc etc
Else
    MsgBox "Header '" & headRow(i) & "' not found!"
End If

sortOrder and colIsString will go out of scope as soon as your sub exits though...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文