VBA to swap cell values, but enter the swapped value in cell below.

MeaclH

New Member
Joined
Apr 2, 2014
Messages
20
Hi all,
I need some help please with a VBA that has been running successfully for some time, however need to tweak slightly.
Below is the code. Essentially it swaps the 2 selected cell ranges. This is great, however, I need it to function slightly different.
I need to keep the data in each original cell ranges, but copy it to the cell 2 below the target ranges and vice versa
I'd then like the original cells to be formatted with a strikethrough.

Any help would be much appreciated.

Cheers,
Hayden

Photos below for before and after
1714784073787.png

1714784149414.png

VBA Code:
Sub swap()
       Dim sCmt As String
    Dim rCell As Range

    sCmt = InputBox( _
      Prompt:="Enter Comment to Add" & vbCrLf & _
      "Comment will be added to all cells in Selection", _
      Title:="Comment to Add")
    If sCmt = "" Then
        MsgBox "No comment added"
    Else
        For Each rCell In Selection
            With rCell
                .ClearComments
                .AddComment
                .Comment.Text Text:=sCmt
            End With
        Next
    End If
    Set rCell = Nothing
    
    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
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Is this what you wanted?

VBA Code:
Option Explicit

Sub swap()
    Dim sCmt As String
    Dim rCell As Range, range1 As Range, range2 As Range
    sCmt = InputBox( _
      Prompt:="Enter Comment to Add" & vbCrLf & _
      "Comment will be added to all cells in Selection", _
      Title:="Comment to Add")
    If sCmt = "" Then
        MsgBox "No comment added"
    Else
        For Each rCell In Selection
            With rCell
                .ClearComments
                .AddComment
                .Comment.Text Text:=sCmt
            End With
        Next
    End If
    Set rCell = Nothing
    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
    range1.Copy range2.Offset(2)
    range2.Copy range1.Offset(2)
    range1.Font.Strikethrough = True
    range2.Font.Strikethrough = True
End Sub
 
Upvote 0
Solution
Is this what you wanted?

VBA Code:
Option Explicit

Sub swap()
    Dim sCmt As String
    Dim rCell As Range, range1 As Range, range2 As Range
    sCmt = InputBox( _
      Prompt:="Enter Comment to Add" & vbCrLf & _
      "Comment will be added to all cells in Selection", _
      Title:="Comment to Add")
    If sCmt = "" Then
        MsgBox "No comment added"
    Else
        For Each rCell In Selection
            With rCell
                .ClearComments
                .AddComment
                .Comment.Text Text:=sCmt
            End With
        Next
    End If
    Set rCell = Nothing
    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
    range1.Copy range2.Offset(2)
    range2.Copy range1.Offset(2)
    range1.Font.Strikethrough = True
    range2.Font.Strikethrough = True
End Sub
Murray, you're a legend. Thanks mate. Works perfectly. One thing, which was an issue with the original I posted; how can I have only the cell values copy and pasted, not the formatting as well? Any ideas?

Thanks heaps!
 
Upvote 0
You're welcome - I'm glad it worked.

To fix the format issue, change these two lines:
VBA Code:
    range1.Copy range2.Offset(2)
    range2.Copy range1.Offset(2)

to this:
VBA Code:
    range2.Offset(2) = range1.Value
    range1.Offset(2) = range2.Value
 
Upvote 0

Forum statistics

Threads
1,216,086
Messages
6,128,734
Members
449,466
Latest member
Peter Juhnke

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