+ Reply to Thread
Results 1 to 2 of 2

Macro Edit - Need to swap a range of cells and cannot modify VBA - Please Help

  1. #1
    Registered User
    Join Date
    04-09-2012
    Location
    Galway, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    1

    Macro Edit - Need to swap a range of cells and cannot modify VBA - Please Help

    Hi All,

    Really appreciate some help here....I'm stuck!

    I found this code on the net and have been trying to amend it, without success, to suit my requirements.

    Instead of booths, i have employee names, and to the right of the employee name, I have several columns of data that I want to be able to switch. I want the employee and the next 3 columns to remain static, but from column 4 to 7, I want these cells swapped.

    In summary when the code runs, it creates a dialog/inpuit box that allows me to enter 2 employee names, and swap the contents of cells to the right of those names.

    What I want to do is change the input method. I want to have 2 cells at the bottom of my spreadsheet that will have a data validation list (all my employee names), be smart enough not to fall over if the user in error selects the same name from the drop down list for both cells, and will then swap the values that are in the 4th through to the 7th column - how do I tell the code to swap only from the 4th column to the 7th?


    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

    Many thanks in advance if anybody can help me on this one.

    Thanks.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro Edit - Need to swap a range of cells and cannot modify VBA - Please Help

    Hi Mark.

    Post up your workbook so we can see the actual source data for these names and the locations of the boxes where you will be selecting those names.

    Click GO ADVANCED and use the paperclip icon to post up a copy of your workbook.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ 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