Solved

Excel: VBA Update Cells On Web Query Refresh

Posted on 2011-09-15
6
186 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

932 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now