2013-01-04 41 views
0

我在Excel 2010中使用此網頁抓取VBA代碼以將網頁的標題拉開。我希望它在代碼運行後將標題粘貼爲值。Excel 2010 VBA函數粘貼爲計算後的值

這裏是我的代碼:

Function GetTitleFromURL(sURL As String) 
Dim wb As Object 
Dim doc As Object 

Set wb = CreateObject("InternetExplorer.Application") 

wb.Navigate sURL 

While wb.Busy 
    DoEvents 
Wend 

GetTitleFromURL = wb.Document.Title 

wb.Quit 

Set wb = Nothing 

'trying to paste as value after get title 
'Application.CutCopyMode = False 
' Selection.Copy 
' Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
'  :=False, Transpose:=False 

End Function 

感謝您的幫助!

+0

你試圖像UDF一樣運行嗎?如果是這樣,您將無法以這種方式在XL上執行操作。 –

+0

我不知道,UDF是什麼? –

+0

UDF ='用戶定義函數「,就是在工作表上的單元格中調用VBA函數的地方,就像這個'= GetTitleFromURL(」www.MyUrl.com「)'。如果你從其他VBA代碼調用它,那麼它的確定(但不是很好的設計)。 –

回答

0

也許這樣的事情可能適合?

Sub GetTitleFromURL() 

    Dim source_wb As Object 
    Dim target_wb As Object 
    Dim target_sheet As Object 
    Dim title As String 
    Dim sURL As String 

Set target_wb = ActiveWorkbook 
    Set target_sheet = target_wb.ActiveSheet 

    sURL = target_sheet.Range("A1").Value 
    Set source_wb = CreateObject("InternetExplorer.Application") 
     source_wb.Navigate sURL 
     While source_wb.Busy 
      DoEvents 
     Wend 
    title = source_wb.Document.title 
    source_wb.Quit 

target_sheet.Range("B1").Value = title 

Set source_wb = Nothing 
    Set target_sheet = Nothing 
Set target_wb = Nothing 

End Sub 
+0

謝謝!這工作完美。 –