数组中元素的返回索引-Word VBA

问题描述

一个变暗为变量Dim valuesAll As Variant的数组,该数组是使用ADO连接Excel文件创建的,并将记录集加载到该数组中。

此数组从Excel文件加载到Word VBA中,并且从Word VBA编辑器中午餐搜索此数组中的元素索引的宏,这就是为什么我不能使用:

Dim pos,arr,val

arr=Array(1,2,4,5)
val = 4

pos=Application.Match(val,False)

if not iserror(pos) then
   MsgBox val & " is at position " & pos
else
   MsgBox val & " not found!"
end if

我收到编译错误:找不到方法或数据成员。

enter image description here

我需要一种可以在Word VBA下使用的解决方案。

该数组是二维数组,我正在搜索的值的(strPESELfromWord)索引位于数组的第二行(源数据的第二列)中。我根本不想打开Excel文件-这就是为什么我使用ADO连接到Excel文件的原因,因此无需打开它。

enter image description here

enter image description here

在我的宏中,我要查找其索引的值(strPESELfromWord)在名为strPESELfromWord的变量中。

这是我到目前为止得到的代码

Sub GetRows_returns_a_variant_array()

Dim connection As ADODB.connection
Dim recSetAll As ADODB.Recordset
Dim recSetHalf As ADODB.Recordset
Dim exclapp As Excel.Application     'This code is written and lunched from Word VBA Editor
Dim exclWorkbk As Excel.Workbook
Dim mySheet As Excel.Worksheet

Dim wordDoc As Word.Document
Dim strPESELfromWord As String
Dim strQuery0 As String
Dim strQuery1 As String
Dim strQuery2 As String
Dim strQuery3 As String
Dim strSexDigit As String
Dim valuesAll As Variant                'Should there be valuesAll() ??
Dim valuesHalf As Variant
Dim txt As String
Dim lngPos As Long
Dim intRemainder As Integer
Dim r As Integer
Dim c As Integer

    Set wordDoc = Word.ActiveDocument
    Debug.Print wordDoc.Name
    Set connection = New ADODB.connection
    Debug.Print "ADODB.connection.Version = " & connection.Version
    strPESELfromWord = Trim(Selection.Text)
    Debug.Print "Wybrano PESEL " & strPESELfromWord & "."

    strSexDigit = Mid(strPESELfromWord,10,1)      'Extract 10th digit from PESEL number
    Debug.Print strSexDigit
    intRemainder = strSexDigit Mod 2                'If 1 is remaider it means it's a male,and if there's no remainder,than it's a female.
    Debug.Print "Remainder equals " & intRemainder & "."

    'Open the database connection.
    connection.Open "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\custdb.xlsm;" & _
                "Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1;"";"                 'Now it works

    'Select the data.
    strQuery0 = "SELECT * FROM Books ORDER BY Title,Year"
                        'Example,reading an unnamed range of cells:  "SELECT * FROM [Sheet1$A1:B10]"
    strQuery1 = "SELECT * FROM [data$]"      '[data$]   is the table name; in this case it's the sheet name;
                        'Once connected to an Excel workbook,a worksheet or range is the equivalent of a table or view.
                        'The table name of a worksheet is the name of the worksheet with a dollar sign ("$") appended to it,and surrounded with square brackets ("[" and "]").
    strQuery2 = "SELECT * FROM [data$] WHERE pesel <> ''"      'col B = pesel; col C = data_urzodzenia; col D = imie_nazwisko
    strQuery3 = "SELECT index,pesel,data_urodzenia,imie_nazwisko FROM [data$]"

    'Get the records.
    Set recSetAll = connection.Execute(strQuery2,adCmdText)    'Set recordset = connection.Execute (CommandText,RecordsAffected,Options)
    Set recSetHalf = connection.Execute(strQuery3,adCmdText)   'RecordsAffected = A Long variable to which the provider returns the number of records that the operation affected.
                                                                  'Evaluates CommandText as a textual deFinition of a command or stored procedure call.

    'Load the values into a variant array.
    valuesAll = recSetAll.getRows            'GetRows returns a variant array holding the Recordset's valuesAll.
    valuesHalf = recSetHalf.getRows

   ' Close the recordset and connection.
    recSetAll.Close
    connection.Close

   'lngPos = Application.Match(strPESELfromWord,valuesAll,0)
    lngPos = Application.Match(strPESELfromWord,Application.Index(valuesAll,0),0)
      

功能如何?会做吗? 我找到了here.

Function FindLoop(arr,val) As Boolean
    Dim r As Long,c As Long
    For r = 1 To UBound(arr,1)
    For c = 1 To UBound(arr,2)
        If arr(r,c) = val Then
            FindLoop = True
            Exit Function
        End If
    Next c
    Next r
End Function

如何从主过程中调用函数以及应该在哪里调用它?

这是源数据表的外观:

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)