0

我在 Word 中有 ContentControl 下拉框。从下拉列表中选择一个项目后,我想在 Excel 文档中搜索该项目并将行号设置为等于变量。下面的代码是我尝试过的,但是 Columns("G:G").Find 部分说它没有定义。

Sub findsomething(curRow) 
    Dim rng As Range
    Dim rownumber As Long

    curPath = ActiveDocument.path & "\" 
    Call Set_Variable(curPath) 
    StrWkShtNm = "Chapters"

    If Dir(StrWkBkNm) = "" Then   
        MsgBox "Cannot find the designated workbook: " & StrWkBkNm, vbExclamation
        Exit Sub 
    End If

    Set rng = Columns("G:G").Find(what:=curRow)
    rownumber = rng.Row
    MsgBox rownumber
    ' Release Excel object memory 
    Set xlWkBk = Nothing
    Set xlApp = Nothing 
    Application.ScreenUpdating = True
End Sub
4

2 回答 2

0

在使用多个 MS Office 应用程序时,最好指定您的目标应用程序:

 Excel.Application.ThisWorkbook.Sheets(1).Range("A1").Select
于 2018-06-20T20:54:08.443 回答
0

这就是最终的工作。您通过引用 Excel 使我走上了正确的轨道。

Sub findsomething(curRow)
Dim rng As Long
Dim rownumber As Long

curPath = ActiveDocument.path & "\"
Call Set_Variable(curPath)
StrWkShtNm = "Chapters"
MsgBox "curRow = " & curRow

If Dir(StrWkBkNm) = "" Then
  MsgBox "Cannot find the designated workbook: " & StrWkBkNm, vbExclamation
  Exit Sub
End If

With xlApp
  .Visible = False
  Set xlWkBk = .Workbooks.Open(FileName:=StrWkBkNm, ReadOnly:=True, AddToMRU:=False)
  With xlWkBk
    With .Worksheets(StrWkShtNm)
        rng = .Range("G:G").Find(what:=curRow)
        MsgBox rng
    End With
    .Close False
  End With
  .Quit
End With


' Release Excel object memory
Set xlWkBk = Nothing: Set xlApp = Nothing
Application.ScreenUpdating = True

End Sub
于 2018-06-21T13:25:36.203 回答