Excel 2010 VBA函数在计算后粘贴为值

时间:2013-01-04 22:11:48

标签: excel-vba excel-2010 vba excel

我在Excel 2010中使用此Web抓取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

感谢您的帮助!

1 个答案:

答案 0 :(得分: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