Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 检查一行中是否有一个字符串,并使用application.vlookup检索坐标_Arrays_Vba_Excel - Fatal编程技术网

Arrays 检查一行中是否有一个字符串,并使用application.vlookup检索坐标

Arrays 检查一行中是否有一个字符串,并使用application.vlookup检索坐标,arrays,vba,excel,Arrays,Vba,Excel,场景:我正在构建一个代码,从工作表中读取一些输入,在其他两个工作表中找到等价物,如果数据可用,则使用它执行一些操作 数据: Worksheet 1 (CAs): name ident date value1 value2 C xxx xx/xx/xx 5000 100 T YYY xx/xx/xx 1000 101 and so on, there are more than 2 tho

场景:我正在构建一个代码,从工作表中读取一些输入,在其他两个工作表中找到等价物,如果数据可用,则使用它执行一些操作

数据:

Worksheet 1 (CAs):

name    ident    date       value1    value2
C       xxx      xx/xx/xx    5000      100
T       YYY      xx/xx/xx    1000      101

and so on, there are more than 2 thousand identifiers (ident) in sheets A and B, while in sheet CAs only 10 of those appear.

Worksheet 2 (A): Monthly dates
             Ident 1     Ident 2    ...    Ident N     row number
20/01/01      1            1                  1             2
20/02/01      1            1                  1             3
20/03/01      1            1                  1             4
...
20/12/12      1            1                  1             N-1 
col number    2            3                  N              N

Worksheet 3 (B): Daily dates
             Ident 1     Ident 2    ...    Ident N     row number
01/01/01      1            1                  1             2
02/01/01      1            1                  1             3
03/01/01      1            1                  1             4
...
12/12/12      1            1                  1             N-1 
col number    2            3                  N              N
Sub Calculations()

Dim lrow As Long, i_number As String, lastRow As Long, lastCol As Long, datefinalmatch As Long, datefinalmatch2 As Long, z1 As Long, ifinalmatch As Long
Dim lastColLetter As String, a As String
Dim p_number As Variant, amount_number As Variant, aaa As Long, bbb As Long
Dim date_number As Date
Dim wb As Workbook
Dim ilist As Variant

Set wb = ThisWorkbook

    For lrow = 2 To wb.Sheets("CAs").UsedRange.Rows.count

        i_number = wb.Sheets("CAs").Range("B" & lrow).Value
        date_number = wb.Sheets("CAs").Range("C" & lrow)
        p_number = wb.Sheets("CAs").Cells(lrow, 5)
        amount_number = wb.Sheets("CAs").Range("D" & lrow)

        lastRow = wb.Sheets("A").Cells(Rows.count, 2).End(xlUp).Row
        lastCol = wb.Sheets("A").Cells(2, Columns.count).End(xlToLeft).Column
        lastColLetter = Col_Letter(lastCol)
        ilist = wb.Sheets("B").Range("B1:" & lastColLetter & "1")

        datefinalmatch = Application.VLookup(wb.Sheets("CAs").Cells(lrow, 3), wb.Sheets("AMT").UsedRange, lastCol, True) + 1 'row
        If IsInArray(i_number, ilist) = True Then
        ifinalmatch = Application.HLookup(i_number, wb.Sheets("A").UsedRange, lastRow, False) 'column

'other sheets date row finder
        datefinalmatch2 = Application.VLookup(wb.Sheets("CAs").Cells(lrow, 3), wb.Sheets("B").UsedRange, lastCol, True) + 1 'row
        End If
    Next lrow 

End Sub

Private Function IsInArray(valToBeFound As String, arr As Variant) As Boolean
Dim element As Variant
    For Each element In arr
        If element = valToBeFound Then
            IsInArray = True
            Exit Function
        End If
    Next element
End Function

Function Col_Letter(lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function
代码的作用:它读取工作表1,获取日期和标识符,使用查找过程在其他工作表中查找该标识符和日期的坐标,并获取交点处的数据

问题:由于某种原因,当我输入要读取的IsInArray函数的标识符时,它总是返回False,尽管它肯定在标识符数组中

问题:你知道我做错了什么吗

代码:

Worksheet 1 (CAs):

name    ident    date       value1    value2
C       xxx      xx/xx/xx    5000      100
T       YYY      xx/xx/xx    1000      101

and so on, there are more than 2 thousand identifiers (ident) in sheets A and B, while in sheet CAs only 10 of those appear.

Worksheet 2 (A): Monthly dates
             Ident 1     Ident 2    ...    Ident N     row number
20/01/01      1            1                  1             2
20/02/01      1            1                  1             3
20/03/01      1            1                  1             4
...
20/12/12      1            1                  1             N-1 
col number    2            3                  N              N

Worksheet 3 (B): Daily dates
             Ident 1     Ident 2    ...    Ident N     row number
01/01/01      1            1                  1             2
02/01/01      1            1                  1             3
03/01/01      1            1                  1             4
...
12/12/12      1            1                  1             N-1 
col number    2            3                  N              N
Sub Calculations()

Dim lrow As Long, i_number As String, lastRow As Long, lastCol As Long, datefinalmatch As Long, datefinalmatch2 As Long, z1 As Long, ifinalmatch As Long
Dim lastColLetter As String, a As String
Dim p_number As Variant, amount_number As Variant, aaa As Long, bbb As Long
Dim date_number As Date
Dim wb As Workbook
Dim ilist As Variant

Set wb = ThisWorkbook

    For lrow = 2 To wb.Sheets("CAs").UsedRange.Rows.count

        i_number = wb.Sheets("CAs").Range("B" & lrow).Value
        date_number = wb.Sheets("CAs").Range("C" & lrow)
        p_number = wb.Sheets("CAs").Cells(lrow, 5)
        amount_number = wb.Sheets("CAs").Range("D" & lrow)

        lastRow = wb.Sheets("A").Cells(Rows.count, 2).End(xlUp).Row
        lastCol = wb.Sheets("A").Cells(2, Columns.count).End(xlToLeft).Column
        lastColLetter = Col_Letter(lastCol)
        ilist = wb.Sheets("B").Range("B1:" & lastColLetter & "1")

        datefinalmatch = Application.VLookup(wb.Sheets("CAs").Cells(lrow, 3), wb.Sheets("AMT").UsedRange, lastCol, True) + 1 'row
        If IsInArray(i_number, ilist) = True Then
        ifinalmatch = Application.HLookup(i_number, wb.Sheets("A").UsedRange, lastRow, False) 'column

'other sheets date row finder
        datefinalmatch2 = Application.VLookup(wb.Sheets("CAs").Cells(lrow, 3), wb.Sheets("B").UsedRange, lastCol, True) + 1 'row
        End If
    Next lrow 

End Sub

Private Function IsInArray(valToBeFound As String, arr As Variant) As Boolean
Dim element As Variant
    For Each element In arr
        If element = valToBeFound Then
            IsInArray = True
            Exit Function
        End If
    Next element
End Function

Function Col_Letter(lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function
OBS:代码运行平稳,没有错误。当逐行运行它时,我看到函数总是返回false,这会提示下一行,最终是代码的结尾,而没有进行任何实际更改(为了简洁起见,我省略了)


OBS2:有些变量已声明但未使用。它们用于代码另一部分中的计算。这就是为什么我从顶部删除了显式选项。

为什么不尝试以下功能

Sub Test()
  Dim arr As Variant
  arr = Split("abc,def,ghi,jkl", ",")
  Debug.Print IsInArray("ghi", arr)
End Sub
看起来您的函数工作不正常。

您可以使用类似以下内容来代替“IsInArray”功能:

    '...

    'If IsInArray(i_number, ilist) = True Then
    Set cell = wb.Sheets("B").Range(Cells(1, 2), Cells(lastcol, 1)).Find(What:=i_number, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
    If Not(cell Is Nothing) Then
        ifinalmatch = Application.HLookup(i_number, wb.Sheets("A").UsedRange, lastRow, False) 'column
    End If

    '...

可能重复的值您是否检查过这些值是否真的相等?特别注意空格、制表符和其他特殊字符以及上下书写。另外,检查数组的大小(可以使用
Debug.print
语句)。如果
valToBeFound
在数组中,它将返回true。@FunThomas是的,我试图通过使用更少的数据(只有20*20)运行一个试验来避免这种情况,在这里我可以看到所有东西的去向。由于某种原因,结果仍然是一样的。我也在检查变量浏览器,它的值就在那里。谢谢你的回答。这是我在搜索这个问题时第一次看到的帖子。我还尝试了该帖子第一个答案中提出的功能。同样的结果。谢谢你的回答。尽管试运行是使用简化的数据样本完成的,但最终运行将包含大量数据,因此我的目标是尽可能使用数组,避免选择。您还可以使用
ilist=wb.Sheets(“B”).Range(“B1:&lastcolleter&“1”)
进行“选择”,虽然这是相同的。关于大量数据,我认为arr中每个元素的解决方案
选择要慢。查找
,但我不确定this@sporcOP特别提到的是
。选择他所指的
。指向某个范围不是“选择”。有关更多信息,请参阅本文。在任何Excel VBA代码中使用或推荐
。选择
。激活
:好的,我没有意识到这一点。我编辑了
。激活
。选择
。也许这样更好。。