• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Switching cells macro. How repair ? It's working partly.

sdfjh87687

New Member
Hi coders !

I'm not maker of this, I just needed fast, effective switch of selected cells. Here it is:
Code:
Sub SwapSelectedCells_fast()

    If Selection.Areas.Count <> 2 Then Exit Sub

    Set range1 = Selection.Areas(1)
    Set range2 = Selection.Areas(2)

    If range1.Rows.Count <> range2.Rows.Count Or _
        range1.Columns.Count <> range2.Columns.Count Then Exit Sub

    range1Address = range1.Address
    range1.Cut
    range2.Insert Shift:=xlShiftToRight
    Range(range1Address).Delete Shift:=xlToLeft

    range2Address = range2.Address
    range2.Cut
    Range(range1Address).Insert Shift:=xlShiftToRight
    Range(range2Address).Delete Shift:=xlToLeft

End Sub

Here is video I recorded how it is (non)working. This web have max 1 MB file size, so I upload is somewhere else:
bad macro tested.mp4 - 5.7 MB

In video is new Sheet, normal typed letters, just formatting is for eyes. There is no formulas, hidden cells, ...

Thank you for any help !
 
Try this:

Code:
Sub SwapSelectedCells_fast()

  Dim Rng1() As Variant
  Dim Rng2() As Variant
   
  If Selection.Areas.Count <> 2 Then Exit Sub

  Set Range1 = Selection.Areas(1)
  Set Range2 = Selection.Areas(2)

  If Range1.Rows.Count <> Range2.Rows.Count Or _
  Range1.Columns.Count <> Range2.Columns.Count Then Exit Sub
   
  Rng1 = Range1.Value
  Rng2 = Range2.Value
   
  Selection.Areas(1) = Rng2
  Selection.Areas(2) = Rng1
   
End Sub
 
Hehe, it's awesome !
Now it works and also it does not copy formatting !

Many thanks, for help :)


Ah me stupid I forgot to mention I use Ms Office 2010 Pro, but soon will install 2016. I guess it will work on it too, right ?
But not sure about it's telemetry. I heard it's hardcoded in it and can't be turned off.
 
Back
Top