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

问题描述:

我有一个字符串数组。我也有一个包含两列的工作表,第一列包含我的数组中的字符串,第二列包含与第一列关联的数字代码。 我需要为数组的一个子集找到它们的相关代码。我尝试了以下,但它不起作用。搜索范围内的数组数据类型字符串时出错

Dim Data(1000, 1000) as string 

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

我查看了为什么出现错误,我发现它不能识别数据(j,1),因为它是字符串值。 Data(j,1)(对于j = 1)中的值是Sch_agr_Tor。如果我将数据(j,1)替换为字符串“Sch_agr_Tor”,那很好,但是这不实际,因为我想使用循环并且不能手动使用“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函数。

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 

但是现在,当我改变“EXP作为字符串”的论调中,将返回0,FindRow功能“EXP为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 
+0

你得到什么错误,你有什么调用'.Range'上? – Comintern

+0

错误类型不匹配。范围之前有父母指的是正确的工作表。 –

+0

然后你应该在更多的上下文中发布代码。我无法用上面的代码复制错误。 – Comintern

确保您在正确的工作表。代码的这种调整似乎工作:

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 
+0

将字符串直接输入到数组中。我不知道这是为什么它使它正确。我发布了整个代码。 –