To use Select/Avivate or Not use Select/Activate

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
558
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Everywhere I read I am being told to avoid using Select or Activate as much as possible. And I have done that in my model. However, I have several routines like this one that refreshes a query.
If I don't use select or activate, the query does not run.

Why is this so?

Thanks in advance for the advice.......

Sub SetCurrentTime()

Sheet10.Activate
Set ws = Sheet10
With ws

If Format(Now(), "hh:mm") < Format(.Range("AQ2").Value, "hh:mm") Then
.Range("BG48").Value = Format(.Range("AQ2").Value, "hh:mm")
Else
.Range("BG48").Value = Format(Now(), "hh:mm")
End If
Set rng = .Range("BA39")
rng.ListObject.QueryTable.Refresh BackgroundQuery:=False
.Range("AR6").Value = .Range("BC48").Value

End With
Exit Sub
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Are you saying that this code works when you utilize the Selection or Activate methods? Then doesn't work when you remove them?
Are you able to post the error you are encountering if any.
I would look at the range and the object. The range "rng" should be associated with a ListObject that contains the QueryTable. The ListObject should have a QueryTable object associated with it. If the range "rng" does not refer to a ListObject, this will cause an error.
 
Upvote 0
There is nothing in that code that would require the sheet to be activated. (the Exit Sub line is completely redundant though)
 
Upvote 0
Solution
Hi Rory, oops, the Exit Sub is just there because I deleted other bits an pieces.Missed that one.
I agree, there is nothing there that requires the select or activate, but the refresh simple does not woprk in this sub and others.
That is why I am confused
 
Upvote 0
Hi Rory, I have been comint at this wrong.
it is this line which is giving the grief

.Range("AR6").Value = .Range("BC48").Value
the value will not copy accross
 
Upvote 0
I can copy/paste and it works. but the line of code wont.
I'll come back later. Just found a glaringly obvious oversite
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top