Consulting

Results 1 to 12 of 12

Thread: Swapping multiple cells accross a row

  1. #1

    Swapping multiple cells accross a row

    I am attaching a sample sheet as a reference.

    in fact the sheet could contain hundreds if not thousands of rows.

    Problem...

    Look at column Player 1, the last 3 characters are AoS.
    I want to find any rows where Player 1 has those three characters ( AoS ) and swap them with the contents of the Player 2.

    BUT if that swap happens then the following swaps must occur as well:

    Contents of Column "Side 1" swaps with Contents of Colum "Side 2"
    Contents of Column "Player 1 Pts" swaps with Contents of Column "Player 2 Pts"

    Ultimately I want this extended so that is the letters AoS appear in any of the following columns ( Player 4, Player 6, Player 8, Player 10)
    the following swaps happens:

    Contents of Column "Player 3" swaps with Contents of Column "Player 4"
    Contents of Column "Side 3" swaps with Contents of Column "Side 4"
    Contents of Column "Player 3 Pts" swaps with Contents of Column "Player 4 Pts"

    Contents of Column "Player 5" swaps with Contents of Column "Player 6"
    Contents of Column "Side 5" swaps with Contents of Column "Side 6"
    Contents of Column "Player 5 Pts" swaps with Contents of Column "Player 6 Pts"

    Contents of Column "Player 7" swaps with Contents of Column "Player 8"
    Contents of Column "Side 7" swaps with Contents of Column "Side 8"
    Contents of Column "Player 7 Pts" swaps with Contents of Column "Player 8 Pts"

    Contents of Column "Player 9" swaps with Contents of Column "Player 10"
    Contents of Column "Side 9" swaps with Contents of Column "Side 10"
    Contents of Column "Player 9 Pts" swaps with Contents of Column "Player 10 Pts"

    Appreciate any help

    John

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub ProcessData()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Long, j As Long
    Dim LastRow As Long

    With Application

    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = 2 To LastRow

    DoSwap i, 3
    For j = 16 To 27 Step 4

    DoSwap i, j
    Next j
    Next i

    End With

    With Application

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With

    End Sub

    Private Sub DoSwap(ActiveRow As Long, ActiveCol As Long)
    Dim tmp As Variant

    With ActiveSheet.Cells(ActiveRow, ActiveCol)

    If .Offset(0, 1).Value Like "*AoS" Then

    tmp = .Offset(0, 1).Value
    .Offset(0, 1).Value = .Offset(0, 3).Value
    .Offset(0, 3).Value = tmp
    tmp = .Value
    .Value = .Offset(0, 2).Value
    .Offset(0, 2).Value = tmp
    End If
    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Quote Originally Posted by xld
    [vba]

    Public Sub ProcessData()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Long, j As Long
    Dim LastRow As Long

    With Application

    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = 2 To LastRow

    DoSwap i, 3
    For j = 16 To 27 Step 4

    DoSwap i, j
    Next j
    Next i

    End With

    With Application

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With

    End Sub

    Private Sub DoSwap(ActiveRow As Long, ActiveCol As Long)
    Dim tmp As Variant

    With ActiveSheet.Cells(ActiveRow, ActiveCol)

    If .Offset(0, 1).Value Like "*AoS" Then

    tmp = .Offset(0, 1).Value
    .Offset(0, 1).Value = .Offset(0, 3).Value
    .Offset(0, 3).Value = tmp
    tmp = .Value
    .Value = .Offset(0, 2).Value
    .Offset(0, 2).Value = tmp
    End If
    End With

    End Sub
    [/vba]
    Great.. Thansk... Tried running it but I get a type mismatch on line:

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I've just opened your workbook, plugged my code in, and it ran flawlessly.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Quote Originally Posted by xld
    I've just opened your workbook, plugged my code in, and it ran flawlessly.
    I assigned your code to a button in a seperate sheet in the smae workbook.

    Only other differance is that there is now much more data to work on.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    But you are talking about a compile error, nothing to do with the amount of data.

    When you say a button on another sheet, what sort of button?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Quote Originally Posted by xld
    But you are talking about a compile error, nothing to do with the amount of data.

    When you say a button on another sheet, what sort of button?
    From the Forms Toolbar

    I was also gettng the error ion the VB Editor window When I ran the code

    The error I get is "Run Time Error 13 Type Mismatch"

    I have set watchs on all the vaiables and when it hits the line

    [VBA]
    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    [/VBA]

    The values of teh watched variables become <out of Context>

  8. #8
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    Start by changing this Const TEST_COLUMN As String = "Aos" back to
    this:Const TEST_COLUMN As String = "A" you must have changed by mistake.

    Also, that data is really hard to look at I can't tell what is supposed to be where...

  9. #9
    VBAX Newbie
    Joined
    May 2008
    Posts
    1
    Location
    This has been cross-posted at Ozgrid. Can't provide the link as my post count is too small.

    Thanks for wasting my time.

  10. #10
    Quote Originally Posted by david000
    Start by changing this Const TEST_COLUMN As String = "Aos" back to
    this:Const TEST_COLUMN As String = "A" you must have changed by mistake.

    Also, that data is really hard to look at I can't tell what is supposed to be where...
    Data cleaned up and AoS set back to A

    I get no more run tim,e aerrors but it is also not doing what I need.

    Note that the AoS ( see my original post ) is always the last 3 characters

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by StephenR
    This has been cross-posted at Ozgrid. Can't provide the link as my post count is too small.

    Thanks for wasting my time.
    Why I dropped off ... http://www.ozgrid.com/forum/showthread.php?t=91426
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    Quote Originally Posted by StephenR
    This has been cross-posted at Ozgrid. Can't provide the link as my post count is too small.

    Thanks for wasting my time.
    I posted it in Ozgrid to get a wider audiance... My apllogies

Posting Permissions

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