Solved

Excel: VBA Update Cells On Web Query Refresh

Posted on 2011-09-15
6
191 Views
Last Modified: 2012-05-12
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
Comment
Question by:jfdinneen
  • 3
  • 3
6 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36548128
Which version of Excel?
0
 

Author Comment

by:jfdinneen
ID: 36548633
Rorya,

Excel 2007

Best wishes,

John
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 36559502
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:jfdinneen
ID: 36560391
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36560449
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
 

Author Closing Comment

by:jfdinneen
ID: 36560610
Excellent resolution.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question