搜索范围内的数组数据类型字符串时出错

时间:2015-05-09 12:52:04

标签: arrays excel vba excel-vba

我有一个字符串数组。我还有一个包含两列的工作表,第一列包含我的数组中的字符串,第二列包含与第一列关联的数字代码。 我需要一个数组的子集来查找它们的相关代码。我尝试了以下但它不起作用。

 Dim Data(1000, 1000) as string

.Range("B:B").Find(what:=Data(j,1), LookIn:=xlValues, SearchOrder:=xlByRows, MatchCase:=False).Row

我检查了为什么我会收到错误,我注意到它无法识别Data(j,1),因为它是字符串值。 Data(j,1)中的值(对于j = 1)是Sch_agr_Tor。如果我用字符串“Sch_agr_Tor”替换Data(j,1)就可以了,但这不实用,因为我想使用循环而不能手动使用“find”。

在即时窗口中,我检查并获得以下内容:

? TypeName(Data(j,1))
String
? Data(j,1)
Sch_agr_Tor
? Data(j,1)="Sch_agr_Tor"
False
? Data(j,1)=Sch_agr_Tor
False

我认为这可能是Find的内部错误。所以我写了自己的find函数。

Function FindRow(Rng As Range, Exp As String) As Long
  Dim vArr As Variant
  Dim j As Long
  Dim n As Long
  Dim c As Range
  n = 0
  For Each c In Rng

     If Exp = c.value Then
         n = c.Row
         Exit For
     End If

  Next c
 FindRow = n

End Function

但是现在当我将FindRow函数中的“Exp As String”参数更改为“Exp As Variant”时,它将返回零。

n = FindRow(UserSheet.Range(Cells(1, 2), Cells(Cells(Rows.Count,  2).End(xlUp).Row, 2)), Data(j, 1))

如果我输入

 n = FindRow(UserSheet.Range(Cells(1, 2), Cells(Cells(Rows.Count,  2).End(xlUp).Row, 2)), "Sch_agr_Tor")

我得到了正确答案,即39。 这是标题和代码的其余部分:

Option Explicit

Option Base 1

Sub main()



Dim MainWorkbook As Workbook
Dim MainSheet, UserTableSheet, InputSheet, OutputSheet, TradesSheet,  InitialSheet As Worksheet
Dim targetCellLoc As String
Dim fileName As String
Dim addressName As String
Dim originCellLoc, Str As String
Dim i, j, NumRuns As Integer
Dim t_start, t_end As Double
Dim FirstCol, LastCol, n As Integer
Dim Data() As Variant



' Initialize the variables
 Set MainWorkbook = Application.ThisWorkbook
 Set InitialSheet = MainWorkbook.ActiveSheet
 Set MainSheet = MainWorkbook.Sheets("Sheet1")




 i = 2
 Do While MainSheet.Cells(11, i).value <> ""
 Set UserTableSheet = MainWorkbook.Sheets(MainSheet.Cells(11, i).value)
 Set InputSheet = MainWorkbook.Sheets(MainSheet.Cells(12, i).value) 

 With InputSheet
 FirstCol = .Range("1:1").Find(what:="Collateral Agreement Group:",  LookIn:=xlValues, SearchOrder:=xlByColumns, MatchCase:=False).Column
 LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
  End With

 'creating an array for our data with the right dimension
  ReDim Data(LastCol - FirstCol + 1, 6)


  For j = 1 To UBound(Data, 1)

  Data(j, 1) = Mid(InputSheet.Cells(1, FirstCol + j - 1).value, 28, 1 +  Len(InputSheet.Cells(1, FirstCol + j - 1).value) - 28)
  MainWorkbook.Sheets("Sheet4").Cells(j, 1) = Data(j, 1) 

 n = FindRow(UserTableSheet.Range(Cells(2, 2), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)), Data(j, 1))


  Data(j, 2) = UserTableSheet.Cells(n, 4)

  Next j


  i = i + 1
   Loop

  End Sub

1 个答案:

答案 0 :(得分:0)

确保您使用正确的工作表。您的代码改编似乎有效:

Sub qwerty()
    Dim Data(1000, 1000) As String
    Dim r As Range
    Data(1, 1) = "Sch_agr_Tor"
    j = 1
    Set r = Range("B:B").Find(what:=Data(j, 1), LookIn:=xlValues, SearchOrder:=xlByRows, MatchCase:=False)
    MsgBox r.Row
End Sub

enter image description here

修改#1:

除了在正确的工作表上,还有其他两件事可能出错:

  • 从错误的位置开始
  • 尝试匹配整个单元格而不是单元格的一部分


例如:

Sub qwerty()
    Dim Data(1000, 1000) As String
    Dim r As Range
    Data(1, 1) = "Sch_agr_Tor"
    j = 1

    Set r = Range("B:B").Find( _
        what:=Data(j, 1), _
        LookIn:=xlValues, _
        SearchOrder:=xlByRows, _
        MatchCase:=False, _
        After:=Range("B1"), _
        LookAt:=xlPart)

    MsgBox r.Row
End Sub