【问题标题】:Is there a way to store all Excel VBA Google searches in a separate sheet in the Excel file?有没有办法将所有 Excel VBA Google 搜索存储在 Excel 文件的单独工作表中?
【发布时间】:2025-12-24 17:50:11
【问题描述】:

我有下面的代码,它可以按照我想要的方式完美运行。我使用 VBA 在 Excel 弹出窗口中键入某个短语以使用 Google 搜索进行搜索。但是,我希望能够将所有 Excel VBA Google 搜索存储在同一个 Excel 文件(相同/另一个工作表)或另一个文件中。有人知道这是否可能吗?不知道是代码应该修改还是Excel设置某种方式。

Private Const LicenseRegistration As String = "+brott+och+straff"
Private Sub CommandButtonSearch_Click()
   Dim query As String
   Dim search_string As String
   Dim googleChromePath As String
   
   query = InputBox("Enter your keyword", "Google Search")
   search_string = Replace(query, " ", "+") & LicenseRegistration
   
   googleChromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
   
   Shell (googleChromePath & " -url http://google.com/search?q=" & search_string)

With ThisWorkbook.Worksheets("AnotherSheet")
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).Value = search_string
End With

End Sub

会弹出一个标题为“Microsoft Visual Basic”的窗口,上面写着:运行时错误“9”: 下标超出范围

我完全按照您编写的方式复制并粘贴了代码,甚至尝试对其进行了一些修改,但无济于事。 我真的希望你能看到这个链接的图片:https://drive.google.com/file/d/1me7xBn8jGvtmpRADp5QFUFmR9k2oGzBs/view?usp=sharing

【问题讨论】:

  • 假设搜索是指search_string,当然。或许只需将search_string 写入单独的工作表即可。
  • @BigBen ,“将 search_string 写入单独的工作表”是什么意思?我是否复制那部分代码并将其粘贴到其他地方?我真的不明白你写的东西是什么意思..
  • With ThisWorkbook.Worksheets("AnotherSheet").Cells(.Rows.Count, 1).End(xlUp).Offset(1).Value = search_stringEnd With。将搜索字符串写入另一个工作表。
  • @BigBen 再次抱歉,但你的意思是这样的吗? i.imgur.com/8PgzRys.png ,因为它不起作用(我可能做错了什么)
  • @BigBen 我现在已经编辑了我的代码并添加了问题和新链接(希望)对你有用。

标签: excel vba google-chrome search google-search


【解决方案1】:

如果您只是想保留search_string 的运行列表,那么您可以使用:

With ThisWorkbook.Worksheets("AnotherSheet") ' change sheet name as needed
   .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Value = search_string
End With

Shell 调用之前(甚至之后)。

【讨论】:

    最近更新 更多