2017-07-24 104 views
0

我有一个excel文件,其中存储了一些文本和关键字列。使用VBA从excel获取数据到办公室字阵列

我想使用Excel中的数据在Word中使用vba进行一些高级搜索。但是,我试图将excel单元格中的数据转换为vba单词中的数组时出错。

我已经使用了转置excel函数,但它不能处理超过255个字符,因此我无法获取超过255个字符的单元格值。

如果有人能帮我一把,我会很感激。

Option Explicit 
    Dim strArray 
    Dim range As range 
    Dim i As Long 
    Dim numberOfUniqMatches As Integer 
    Dim totalMatches As Integer 

Sub HighlightMatchesAndSummarize() 
    totalMatches = 0 
    '************************************ GET DATA FROM EXCEL *************************************** 
    Dim xlApp As Object 
    Dim xlBook As Object 
    Const strWorkBookName As String = "D:\keyword_source_3.xlsx" 
    On Error Resume Next 
    Set xlApp = GetObject(, "Excel.Application") 
    If Err Then 
     Set xlApp = CreateObject("Excel.Application") 
    End If 
    On Error GoTo 0 
    Set xlBook = xlApp.Workbooks.Open(FileName:=strWorkBookName) 
    'xlApp.Visible = True 
    xlApp.Visible = False 
    'transpose excel cells in our arrays 
    strArray = xlApp.Transpose(xlApp.ActiveSheet.range("A1:A20" & AlRow).Value) 
    Set xlBook = Nothing 
    xlApp.Quit 
    Set xlApp = Nothing 
    ' 
    ' End of data extraction 

    '/******************************** SEARCH LOOP START ********************************** 
    For i = 1 To UBound(strArray) 
     numberOfUniqMatches = 0 
     Set range = ActiveDocument.range 

     With range.Find 
     .Text = strArray(i) 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchFuzzy = False 
     .MatchPhrase = True 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
      Do While .Execute(Forward:=True) = True 
        numberOfUniqMatches = numberOfUniqMatches + 1 
        totalMatches = totalMatches + 1 
        range.HighlightColorIndex = wdYellow 
      Loop 
     End With 
    Next 
    ' 
    ' End of search loop 

    ' Display message if no matching word is found 
    If totalMatches <= 0 Then 
     MsgBox "Sorry! No matching keyword found." 
    Else 
     MsgBox "Search ended: " & totalMatches & " matching word(s)." 
    End If 

End Sub 

回答

1

更改此:

strArray = xlApp.Transpose(xlApp.ActiveSheet.range("A1:A20" & AlRow).Value) 

要:

'remove the transpose (and fix the range...) 
strArray = xlApp.ActiveSheet.range("A1:A" & AlRow).Value 

然后在您的循环:

For i = 1 To UBound(strArray, 1) '<<<<<<< 
    numberOfUniqMatches = 0 
    Set range = ActiveDocument.range 

    With range.Find 
    .Text = strArray(i, 1) '<<<<<<< 
    .Format = True 
    .MatchCase = False 
    .MatchWholeWord = False 
    .MatchWildcards = False 
    .MatchFuzzy = False 
    .MatchPhrase = True 
    .MatchSoundsLike = False 
    .MatchAllWordForms = False 
     Do While .Execute(Forward:=True) = True 
       numberOfUniqMatches = numberOfUniqMatches + 1 
       totalMatches = totalMatches + 1 
       range.HighlightColorIndex = wdYellow 
     Loop 
    End With 
Next 
+0

明白了@TimWilliams,我会尝试并回到你身边。感谢您的时间。 – Stackgeek

+0

你好@TimWilliams,希望你做得很好。我只是想感谢你的帮助。有效。我没有想过使用multidim数组。酷:D! 您刚忘记在.Value后删除右括号 – Stackgeek

0

Saerch代码中的ByteLong更换。 Ctrl+HReplace的快捷方式。

+0

我不明白你@Vityata,我在我的代码中没有字节。我已经添加了代码,你可以看到。在任何地方都没有声明字节。 – Stackgeek