+ Reply to Thread
Results 1 to 6 of 6

Macro to switch two cells value

Hybrid View

  1. #1
    Valued Forum Contributor meabrams's Avatar
    Join Date
    10-03-2014
    Location
    USA
    MS-Off Ver
    2007, 2010, 2013, 2016
    Posts
    451

    Macro to switch two cells value

    I need macro that will basically store contents of cell once selected and on next selection store contents of that cell then paste the cell info from first selected cell into the second selected cell and the info from the second selected cell into the first then turn this feature off until active again

    Coding so far
    Public Bt_On As String
    
    Sub Bt1()
    Bt_On = "Yes"
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim x As Range, y As Range
    Dim tmp1 As String, tmp2 As String
    If Bt_On = "Yes" Then
    x = Target.Address
    tmp1 = x.Value
    
    'Not sure what to put to make it work on next selection
    
    Bt_On = ""
    End If
    
    End Sub
    edit: fix code brackets

  2. #2
    Registered User
    Join Date
    12-14-2012
    Location
    Czech Republic
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: Macro to switch two cells value

    Hello Meabrams,

    you can try this:
    Option Explicit
    
    Public Bt_On As String
    
    Sub Bt1()
    Bt_On = "Yes"
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim tmp1 As String, tmp2 As String, ad As String, cl As Range
        
        If Target.Cells.Count > 1 Then
            MsgBox "You can only select single cell to make this working."
            Exit Sub
        Else
            If Bt_On = "Yes" Then
                tmp1 = Target.Value
                ad = Target.Address
                Set cl = Application.InputBox("Select another single cell", Type:=8)
                
                Do While cl.Cells.Count > 1
                    Set cl = Application.InputBox("You need to pick single cell", Type:=8)
                Loop
            tmp2 = cl.Value
            cl = tmp1
            Range(ad).Value = tmp2
            Bt_On = ""
            End If
        End If
    End Sub

  3. #3
    Valued Forum Contributor meabrams's Avatar
    Join Date
    10-03-2014
    Location
    USA
    MS-Off Ver
    2007, 2010, 2013, 2016
    Posts
    451

    Re: Macro to switch two cells value

    Is there a way to have this without the input box

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Macro to switch two cells value

    Code in normal module :
    Public Switcher_Status As Boolean
    Public Switcher_FirstRange As Range
    Sub Switcher_Start()
      Switcher_Status = True
      Set Switcher_FirstRange = Nothing
    End Sub
    Code in sheet module :
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      Dim arr()
      If Switcher_Status = True Then
         If Switcher_FirstRange Is Nothing Then
            Set Switcher_FirstRange = Target
         Else
            If Target.Count = 1 Then
               ReDim arr(1 To 1, 1 To 1)
               arr(1, 1) = Target.Formula
            Else
               arr = Target.Formula
            End If
    
            Target.ClearContents
            Target.Resize(Switcher_FirstRange.Rows.Count, Switcher_FirstRange.Columns.Count).Formula = Switcher_FirstRange.Formula
    
            Switcher_FirstRange.ClearContents
            Switcher_FirstRange.Resize(UBound(arr, 1), UBound(arr, 2)).Formula = arr
    
            Switcher_Status = False
         End If
      End If
    End Sub
    You need to call Sub Switcher_Start() everytime you want to do the switch (you can assign a keyboard shortcut to this sub if you use it a lot).

  5. #5
    Valued Forum Contributor meabrams's Avatar
    Join Date
    10-03-2014
    Location
    USA
    MS-Off Ver
    2007, 2010, 2013, 2016
    Posts
    451

    Re: Macro to switch two cells value

    Works like a champ thank you

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Macro to switch two cells value

    You are welcome, glad to help.


    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Switch macro
    By drakaz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-07-2015, 01:01 AM
  2. Switch values of 2 cells
    By sepus in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-07-2013, 07:54 PM
  3. Macro Help to Autumatically Switch Cells when Conditional is True
    By VICEROY in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-13-2013, 01:46 AM
  4. Switch the contents of two cells around
    By nebb in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-27-2011, 01:00 PM
  5. Excel 2007 : IF Formula / Macro to switch order of cells
    By dadevil316 in forum Excel General
    Replies: 1
    Last Post: 04-13-2011, 12:44 PM
  6. Is there a way to switch data between two cells?
    By lhassell in forum Excel General
    Replies: 1
    Last Post: 05-26-2006, 01:25 PM
  7. [SOLVED] [SOLVED] Switch cells on input
    By Dan in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-18-2005, 12:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1