Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 262
Default different results when using F5 and F8 in VBE

My workbook contains several worksheets with queries to external SQL Server
database. All queries use background updates. Each query uses a parameter
for the SQL query string. The parameter value is obtained from E2
Reports!B4. Manually changing this cell returns results on worksheet Job
Summary in range("A6:K6"). This works great!

Worksheet Order Details contains a list of job numbers in column B. I wrote
a VBA sub that loops through these jobs, changing E2 Reports!B4 for each job.
I then call the Application.RefreshAll function. Finally, I copy "Job
Summary!A6:K6" to a row on worksheet Order Summary. If the next job is the
same order, I copy with a paste special to add the results.

The problem occurs when running the VBA sub. If I step through the code,
waiting for the RefreshAll function to finish, the correct values are
returned. If I step through fast, without waiting for the RefreshAll, the
values for some jobs are copies of the previous job, but other jobs are
correct. If I hold the F8 key down or hit the F5 key, all values are the
same as the first job.

I've tried DoEvents, turning off screen updates, and turning off background
updates.

Any ideas?

Code:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.Name = "Order Summary" Then
' rj references position on Job Summary sheet
Dim rj As Integer
' ro references position on Order Summary sheet
Dim ro As Integer
rj = 6
ro = 6
' clear all rows
Sheets("Order Summary").Range("a6:a65536").EntireRow.Clear
Application.CutCopyMode = False
' turn off screen updating to avoid flicker
Application.ScreenUpdating = False
' copy unique orders from order details
While Len(Sheets("Order Details").Range("b" & rj)) 0
' copy customer and order #
Sheets("Order Details").Range("a" & rj & ":b" & rj).Copy
Destination:=Sheets("Order Summary").Range("a" & ro)
' get job summary for first job in this order
GetJobSummary Sheets("Order Details").Range("c" & rj)
Sheets("Order Summary").Range("c" & ro).PasteSpecial
xlPasteValuesAndNumberFormats
' escape from copy mode
Application.CutCopyMode = False
rj = rj + 1
' get summary for next jobs for same order
While Sheets("Order Details").Range("b" & rj) = Sheets("Order
Details").Range("b" & rj - 1)
' add next job summary results to this order
GetJobSummary Sheets("Order Details").Range("c" & rj), True
Sheets("Order Summary").Range("d" & ro).PasteSpecial
xlPasteValuesAndNumberFormats, xlPasteSpecialOperationAdd
' escape from copy mode
Application.CutCopyMode = False
rj = rj + 1
Wend
ro = ro + 1
Wend
' turn screen updating back on
Application.ScreenUpdating = True
End If
End Sub

Private Sub GetJobSummary(JobNo As String, Optional NoOrderTotal As Boolean)
If IsMissing(NoOrderTotal) Then NoOrderTotal = False
' load jobno in query parameter field
Sheets("E2 Reports").Range("B4").FormulaR1C1 = JobNo
' refresh all queries
DoEvents
ActiveWorkbook.RefreshAll
' copy data to return to calling procedure
If NoOrderTotal Then
' copy just details
Sheets("Job Summary").Range("B6:K6").Copy
Else
' copy order total with details
Sheets("Job Summary").Range("A6:K6").Copy
End If
End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
different results Dave Excel Discussion (Misc queries) 0 January 23rd 07 12:00 AM
Sum of If Results. How? Corey Excel Worksheet Functions 2 December 14th 06 02:48 AM
Conditional Sum Argument results do not equal cell results Excel Randy R Mullins Excel Worksheet Functions 3 August 9th 06 07:16 PM
Wanting to write results to array instead of sheet, results overwriting.... [email protected] Excel Programming 2 October 31st 05 01:47 PM
How can I list the results of my macro without overwritng previous results? mattip Excel Programming 3 November 28th 03 03:45 AM


All times are GMT +1. The time now is 10:01 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"