2014-02-19 59 views
2

我有一個啓動UserForm的Word宏。使用Word宏查找Excel行並引用該行中的其他單元格

我想添加一個功能來更新外部Excel文檔的某些字段。 不幸的是,Word宏不能識別許多Excel VBA命令。

這裏的理念是:

  1. 搜索excel表格爲關鍵字 「供應商」。
  2. 確定其行(RowCrnt)。
  3. 將該行中特定列的內容帶回單詞宏(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 
+0

您可以通過'WorksheetFunction'對象的方式使用MS Word宏中的MS Excel函數。如果您知道工作表中包含搜索條件的列,那麼您可以使用簡單的'vlookup':http://msdn.microsoft.com/en-us/library/office/ff194701.aspx –

+0

我真誠地希望那'FileName.exe'上面有一個佔位符文本,否則,我們必須假設你將一個*可執行文件*傳遞給Excel。恩呃。不好。 **:D **如何啓用對Excel對象的引用並使用***早期綁定***?如果這是您第一次嘗試MS Office中的應用程序間操作,那麼這可以爲您提供很多幫助。 – Manhattan

+0

哈哈抱歉,本應該是一個xls :) – ukie

回答

0

當然,你可以在你的代碼使用Excel VBA功能 - 您可以使用.Find裏面你有塊,像這樣:

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 
+0

完美謝謝! – ukie

相關問題