VBA to unselect a selected range

entity789

Board Regular
Joined
Jul 15, 2002
Messages
95
Hi all,

This is probably a very simple question, but I just can't seem to get it.

How do I unselect a range within a macro?

The macro takes a range, manipulates it, but then I can't get it to unselect before moving on (there is a loop and if not specifically told to unselect, I will loose data).

apparently, Selection.Unselect does not work, nor does MyRange.Unselect


Thanks in advance

J
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Just select another range or cell.

Just as an aside, it's rare to need to select anything when using VBA. As an example, the following give identical results when copying and pasting on the active sheet-
<pre>
Range("A1").Select
Selection.Copy
Range("B1").Select
ActiveSheet.Paste

Range("A1").Copy Range("B1")
</pre>
 
Upvote 0
I don't think I can do that. The reason being I am using a union command and if there are cell(s) selected other than only the ones I want, they will be thrown into the mix a second time giving me erroneous results.

Here is my entire code:

Sub Select_Values_In_Range()

Dim MyRange As Range
Dim Message, val As Double
Dim MyRange2 As Range
Dim ave As Double
On Error GoTo 10
Message = "Enter cut-off value for screening data"
val = InputBox(Message)
5
Set MyRange = Application.InputBox(prompt:="Select range", Type:=8)
Dim c As Range
Dim Rng As Range
For Each c In MyRange
If c.Value > val Then
If Rng Is Nothing Then
Set Rng = c
Else
Set Rng = Union(Rng, c)
End If
End If
Next c
Rng.Select
Selection.Clear
ave = Application.WorksheetFunction.Average(MyRange)
Set MyRange2 = MyRange.SpecialCells(xlCellTypeBlanks)
MyRange2.Select
Selection.Activate
Selection.Font.ColorIndex = 3
Selection = ave
' RIGHT HERE is where I need to unselect MyRange2 or when it loops I will loose the data

GoTo 5
10
End Sub


Thanks again
 
Upvote 0

Forum statistics

Threads
1,213,522
Messages
6,114,112
Members
448,549
Latest member
brianhfield

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