VBA De-select

Katy Jordan

Well-known Member
Joined
Jun 28, 2008
Messages
596
Hi, the below retains the selection in worksheet array, how can i get the code to de-select

i tried adding .Range("A5").Select, but the selection is still in place

Code:
 With Worksheets(WSary(a)).Range("A6:M" & NR & ",Q6:R" & NR)
        .Borders.LineStyle = xlNone
        .Borders.LineStyle = xlContinuous
        .Interior.ColorIndex = 0
        .Borders.Weight = xlHairline
       [COLOR=Red] .Range("A5").Select[/COLOR]
       End With
 
Each sheet always has the selection. Selection can be only changed in a sheet but can’t be excluded from the sheet.
You can protect sheet with disabled cells selection options.
It will look like there is no selection, but this just hide it till unprotection
 
Last edited:
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
We may need to see the full code (if it isn't too long) to understand what is happening.
 
Upvote 0
Each sheet always has the selection. Selection can be only changed in a sheet but can’t be excluded from the sheet.

Vlad, In the Worksheets(Wsary(a)) , i have ranges which are in selection, i stepped through code and found the selection took place after this copy and paste, i just cant get rid of the selection between the last copy and paste code

Code:
.Columns("A:C").Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).Copy
      Worksheets(WSary(a)).Range("A" & NR).PasteSpecial Paste:=xlPasteValues
      .Columns("F:O").Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).Copy
      Worksheets(WSary(a)).Range("D" & NR).PasteSpecial Paste:=xlPasteValues
      NR = Worksheets(WSary(a)).Range("A" & Rows.Count).End(xlUp).Offset(1).Row

Code:
With ShtRaw
  LR = .Cells(Rows.Count, 1).End(xlUp).Row
  .AutoFilterMode = False
  With .Range("A5:O" & LR)
    For a = LBound(WSary) To UBound(WSary)
      .AutoFilter Field:=2, Criteria1:=WSary(a)
      NR = Worksheets(WSary(a)).Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      'Copy Data to destination worksheets
      On Error Resume Next
      .Columns("A:C").Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).Copy
      Worksheets(WSary(a)).Range("A" & NR).PasteSpecial Paste:=xlPasteValues
      .Columns("F:O").Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).Copy
      Worksheets(WSary(a)).Range("D" & NR).PasteSpecial Paste:=xlPasteValues
      NR = Worksheets(WSary(a)).Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      
      'Apply Formatting
      With Worksheets(WSary(a)).Range("A6:M" & NR & ",Q6:R" & NR)
        .Borders.LineStyle = xlNone
        .Borders.LineStyle = xlContinuous
        .Interior.ColorIndex = 0
        .Borders.Weight = xlHairline
       End With
 
Upvote 0
Katy, you can save old selection, then do something and at the end restore old selection like this:
Rich (BB code):

Sub test()
  
  Dim OldSel As Range
  
  ' Save old selection
  Set OldSel = Selection
  
  ' Your code like this
  Range("A1:B20").Copy
  Range("D2:D21").PasteSpecial xlPasteValues
  Application.CutCopyMode = False ' <-- disable selection mode
  ' End of your code
  
  ' Restore old selection
  OldSel.Select
  
End Sub
 
Upvote 0
Nope that did not work, note i have worksheet array, the application. cut does not work

WSary = Array("Sheet1", "Sheet2")


Katy, you can save old selection, then do something and at the end restore old selection like this:
Rich (BB code):

Sub test()
  
  Dim OldSel As Range
  
  ' Save old selection
  Set OldSel = Selection
  
  ' Your code like this
  Range("A1:B20").Copy
  Range("D2:D21").PasteSpecial xlPasteValues
  Application.CutCopyMode = False ' <-- disable selection mode
  ' End of your code
  
  ' Restore old selection
  OldSel.Select
  
End Sub
 
Upvote 0
The same as above:
Rich (BB code):

Sub Test1()
  
  With Selection  ' <-- Save old selection in a stack
    
    ' Your code like this
    Range("A1:B20").Copy
    Range("D2:D21").PasteSpecial xlPasteValues
    Application.CutCopyMode = False ' <-- Disable selection mode
    ' End of your code
    
    .Select       ' <-- Restore old selection from the stack
  
  End With
  
End Sub
 
Upvote 0
Nope that did not work, note i have worksheet array, the application. cut does not work

WSary = Array("Sheet1", "Sheet2")
Do save - restore selection for each processed sheet
 
Upvote 0
Do save - restore selection for each processed sheet


I'm a bit confused, i can see what you did in your post above, but how do i implement your method to this as the data is copy from Rawdata and then pasting to Worksheet(Wsary(a))

Code:
.Columns("A:C").Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).Copy
      Worksheets(WSary(a)).Range("A" & NR).PasteSpecial Paste:=xlPasteValues
      .Columns("F:O").Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).Copy
      Worksheets(WSary(a)).Range("D" & NR).PasteSpecial Paste:=xlPasteValues
      NR = Worksheets(WSary(a)).Range("A" & Rows.Count).End(xlUp).Offset(1).Row
 
Upvote 0
I'd suggest to do saving - restoring of each sheet selection in the separate subrotines.
Just call SaveSelections ActiveWorkbook before your code and call RestoreSelections at the end.

The code:
Rich (BB code):

Dim SelectionsCol As Collection

Sub Test3()
  
  ' Save selection of each sheet
  Application.ScreenUpdating = False
  SaveSelections ActiveWorkbook
  
  ' Put your code here, below is just for testing
  For Each Sh In Worksheets
    Sh.Activate
    Selection.Offset(10, 10).Select
  Next
  
  ' Restore each sheet selection
  RestoreSelections
  Application.ScreenUpdating = True

End Sub

Private Sub SaveSelections(Wb As Workbook)
  Dim Sh As Worksheet, ScUpd
  Set SelectionsCol = New Collection
  With Application
    ScUpd = .ScreenUpdating
    If ScUpd Then .ScreenUpdating = False
    For Each Sh In Wb.Worksheets
      Sh.Activate
      SelectionsCol.Add Selection
    Next
    If ScUpd Then .ScreenUpdating = ScUpd
  End With
End Sub

Private Sub RestoreSelections()
  Dim x, ScUpd
  On Error Resume Next
  With Application
    ScUpd = .ScreenUpdating
    If ScUpd Then .ScreenUpdating = False
    For Each x In SelectionsCol
      x.Parent.Activate
      x.Select
    Next
    If ScUpd Then .ScreenUpdating = ScUpd
  End With
  Set SelectionsCol = Nothing
End Sub
 
Last edited:
Upvote 0
Vlad, if i just use this after my code then it works, but your other method did not de-select, i tried as you suggested.

For Each Sh In Worksheets
Sh.Activate
Selection.Cells(1, 1).Select
Next


I'd suggest to do saving - restoring of each sheet selection in the separate subrotines.
Just call SaveSelections ActiveWorkbook before your code and call RestoreSelections at the end.

The code:
Rich (BB code):

Dim SelectionsCol As Collection

Sub Test3()
  
  ' Save selection of each sheet
  Application.ScreenUpdating = False
  SaveSelections ActiveWorkbook
  
  ' Put your code here, below is just for testing
  For Each Sh In Worksheets
    Sh.Activate
    Selection.Offset(10, 10).Select
  Next
  
  ' Restore each sheet selection
  RestoreSelections
  Application.ScreenUpdating = True

End Sub

Private Sub SaveSelections(Wb As Workbook)
  Dim Sh As Worksheet, ScUpd
  Set SelectionsCol = New Collection
  With Application
    ScUpd = .ScreenUpdating
    If ScUpd Then .ScreenUpdating = False
    For Each Sh In Wb.Worksheets
      Sh.Activate
      SelectionsCol.Add Selection
    Next
    If ScUpd Then .ScreenUpdating = ScUpd
  End With
End Sub

Private Sub RestoreSelections()
  Dim x, ScUpd
  On Error Resume Next
  With Application
    ScUpd = .ScreenUpdating
    If ScUpd Then .ScreenUpdating = False
    For Each x In SelectionsCol
      x.Parent.Activate
      x.Select
    Next
    If ScUpd Then .ScreenUpdating = ScUpd
  End With
  Set SelectionsCol = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,614
Messages
6,179,906
Members
452,949
Latest member
beartooth91

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