vba - 使用 Word 宏查找 Excel 行并引用该行中的其他单元格

标签 vba excel ms-word

我有一个启动用户窗体的 Word 宏。

我正在尝试添加一项功能来更新外部 excel 文档中的某些字段。
不幸的是,Word 宏不能识别许多 Excel VBA 命令。

这是想法:

  • 在 Excel 电子表格中搜索关键字“供应商”。
  • 确定其行 (RowCrnt)。
  • 将该行中特定列的内容带回单词宏 (docField)。

  • 这是我所在的位置:
    Private Sub updateForm_Click()
    
    'Start by parsing the Test Tracking spreadsheet
    Set appExcel = CreateObject("Excel.Application")
    
    Dim testTrack_File As Variant
    Dim Rng As Range
    Dim RowCrnt As Long
    
    testTrack_File = "FileName.exe"
    appExcel.Workbooks.Open testTrack_File
    
    'Search Test Tracking spreadsheet for the Vendor
    
    With appExcel.Sheets("Testing_Queue")
    
    'Code Needed here
     docField = 
    
    End With
    
    appExcel.ActiveWorkbook.Close
    appExcel.Quit
    Set appExcel = Nothing
    
    End Sub
    

    最佳答案

    当然你可以在你的代码中使用 Excel VBA 函数——你可以使用 .Find在你的 with block 内,像这样:

    With appExcel.Sheets("Testing_Queue")
    
        Dim xlCell As Object
        Set xlCell = .Cells.Find("asdf")
    
        'Get the row where the value was found
        Dim xlRow As Integer
        xlRow = xlCell.Row
    
        'change the target column to whichever you want
        Dim xlCol As Integer
        xlCol = 6
    
        Dim targetCellValue
        targetCellValue = .Cells(xlRow, xlCol).Value
    
        MsgBox (targetCellValue)
    
    End With
    

    关于vba - 使用 Word 宏查找 Excel 行并引用该行中的其他单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21893792/

    相关文章:

    performance - 如何在 Excel VBA 中更好地管理内存以防止宏变慢?

    Python - IronPython 困境

    excel - 使用可见的查询和连接栏刷新 Power Query

    vba - 使用 VBA 在 Office 中压缩图像

    vb.net - 通过 vb.net 将表格插入到 word 中

    vba - 将 PPT 文件中所有形状的一种主题/强调色更改为另一种强调色

    php - 发布/获取 excel 数据的最佳方式是什么

    excel - 如何使 Excel MIN 函数忽略集合中的零?

    excel - VBA - 识别空字符串

    excel - 检查word文件是否已经打开vba