+ Reply to Thread
Results 1 to 5 of 5

Swap identical ranges of cell values

Hybrid View

  1. #1
    Registered User
    Join Date
    05-12-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    2

    Swap identical ranges of cell values

    I have a spreadsheet that lists the names and booth assignments (#s) of vendors at a festival. Because the vendor's booth assignments will be switched around as new vendors come in, I'd like a macro that would allow me to do something like this:

    Select cell range in row 2
    Select same-sized cell range in row 10
    Swap their data.

    Column A won't be swapped, as that contains the booth #s. I just want to switch the vendor's data between rows. If it helps there's currently only 3 columns for vendor data, but in the event more are added in the future, I was hoping the macro would accept any selection size so long as the second selection is the same # of cells.

    Thank you! Oh, and I'm using Excel 2007.

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Swap identical ranges of cell values

    Why don't you simply change the value in the cell to reflect the reassigned booth number? You seem to want a more complicated solution than what is required by your explanation, unless there is more to this than what you detailed.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Registered User
    Join Date
    05-12-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Swap identical ranges of cell values

    That would work, yes. I was trying to avoid having to re-sort the list every time a number changed, though, since I won't be the only one working with this list in its final stages and not everyone involved is as familiar with excel.

    I have a broader interest in the macro beyond this workbook anyway; there've been a time or two when being able to flip around cells would've been handy, but I end doing it the old fashioned way.
    Last edited by shg; 05-25-2010 at 08:16 PM. Reason: deleted spurious quote

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Swap identical ranges of cell values

    Hi CCArtsAlliance

    Post a sample of what you have and what you'd like it to be and I'll be glad to look at it with you.

    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Swap identical ranges of cell values

    Hello CCArtsAlliance,

    Welcome to the Forum!

    This macro will do what you asked. Very simple to use. The user inputs 2 booth numbers separated by either a space or comma and clicks "OK" or preses the "Enter" key to switch them. The macro expects the booth numbers to be in column "A". The other information starts at column "B" on to the last column header in row 1.

    Copy this macro into a VBA module. Add a command button to the sheet and attach this macro to it. When the user clicks the button, he or she will be prompted to enter the booth numbers. An example workbook is attached.
    'Thread: http://www.excelforum.com/excel-programming/728960-swap-identical-ranges-of-cell-values.html
    'Poster: CCArtsAlliance
    'Posted: May 25, 2010
    'Author: Leith Ross
    
    Sub SwitchBooths()
    
      Dim Booths As Variant
      Dim Booth1 As Variant
      Dim Booth2 As Variant
      Dim ColCount As Long
      Dim Msg As String
      Dim Temp As Variant
      
        ColCount = Cells(1, Columns.Count).End(xlToLeft).Column - 1
        
    EnterBooths:
        Booths = InputBox("Enter the 2 booths you want to switch." & vbCrLf & "Click OK or press Enter when you are done.")
        If Booths = "" Then Exit Sub
        
          If Booths Like "*,*" Then
             Booths = Split(Booths, ",")
          Else
             If Booths Like "* *" Then Booths = Split(Booths, " ")
          End If
          
          If VarType(Booths) = 8 Then
             MsgBox "Please enter 2 booth numbers separated by a space or comma."
             GoTo EnterBooths
          End If
             
            With Columns("A")
              .EntireColumn.NumberFormat = "@"
              Booth1 = Trim(Booths(0))
                Set Booth1 = .Cells.Find(Booth1, , xlValues, xlWhole, xlByRows, xlNext, False)
                If Not Booth1 Is Nothing Then
                   Set Booth1 = Booth1.Offset(0, 1).Resize(1, ColCount)
                Else
                   Msg = Booths(0) & ","
                End If
              Booth2 = Trim(Booths(1))
                Set Booth2 = .Cells.Find(Booth2, , xlValues, xlWhole, xlByRows, xlNext, False)
                If Not Booth2 Is Nothing Then
                   Set Booth2 = Booth2.Offset(0, 1).Resize(1, ColCount)
                Else
                   Msg = Msg & Booths(1) & ","
                End If
              .EntireColumn.NumberFormat = "General"
           End With
        
         If Msg <> "" Then
            MsgBox "Did Not Find " & Left(Msg, Len(Msg) - 1)
         Else
            Temp = Booth2.Value
            Booth2.Value = Booth1.Value
            Booth1.Value = Temp
         End If
         
    End Sub


    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time.
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.


    Adding Buttons to the Worksheet
    1. Click View on the Excel Menu Bar or use ALT+V.
    2. Click Toolbars or type T to display the available toolbars.
    3. Click on Forms or use the Down Arrow to select this toolbar.
    4. Look for the rectangular icon. When you place the mouse on it you will see Button displayed.
    5. Click on this icon.
    6. Move the cursor to the cell where you want the button. Left Click and Hold the button down while you move the mouse. This will draw the button. Release the button when you are done.
    7. A dialog box will appear asking you to assign a macro. Close the dialog. You can add the macro later.
    8. Repeat steps from 4 on to add more buttons.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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