[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Excel: VBA Update Cells On Web Query Refresh

On a web query refresh, I would like to update cells B18 and C18 on Sheet2 (moving down a row on each new refresh) based on the values of $J$16 and $P$16 respectively of Sheet1.

Please advise?

Best wishes,

John
0
jfdinneen
Asked:
jfdinneen
  • 3
  • 3
1 Solution
 
Rory ArchibaldCommented:
Which version of Excel?
0
 
jfdinneenAuthor Commented:
Rorya,

Excel 2007

Best wishes,

John
0
 
Rory ArchibaldCommented:
Here's a demo using a Querytable object declared WithEvents. Code is:
ThisWorkbook module:
Option Explicit

Private Sub Workbook_Open()
   Sheet7.HookQuery
End Sub

Open in new window


Worksheet code module:
Option Explicit

Private WithEvents qt As QueryTable

Private Sub qt_AfterRefresh(ByVal Success As Boolean)
   Dim lngRow            As Long
   Dim wksOut            As Worksheet
   Set wksOut = Sheets("Sheet2")
   With wksOut
      lngRow = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
      .Cells(lngRow, "B").Value = Me.Range("C1").Value
      .Cells(lngRow, "C").Value = Me.Range("C2").Value
   End With

End Sub

Public Sub HookQuery()
   Set qt = Me.QueryTables(1)
End Sub

Open in new window


Just alter the value cells from C1 and C2 to whatever cells you want.

Webquery.xlsm
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jfdinneenAuthor Commented:
Rorya,

Thanks for the high-quality solution.

One problem - it does not work if I have another spreadsheet in focus - which is critical as I need the updates to take place in the background.

Best wishes,

John
0
 
Rory ArchibaldCommented:
Sorry - lazy coding. Chang ethis line:

   Set wksOut = Sheets("Sheet2")

to this:


   Set wksOut = ThisWorkbook.Sheets("Sheet2")

adjusting the sheet name as required. Better yet, use the sheet's codename.
0
 
jfdinneenAuthor Commented:
Excellent resolution.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now