+ Reply to Thread
Results 1 to 3 of 3

Align 3 columns with 2 columns of matching data

  1. #1
    Registered User
    Join Date
    02-03-2012
    Location
    florida
    MS-Off Ver
    Excel 2003
    Posts
    1

    Align 3 columns with 2 columns of matching data

    I have a 3 column worksheet, that two columns need to be matched and a space added if no match, and the 3rd column is tied into the 2nd column, and needs to stay with it if moved. I have searched for something like this, and only found one with the 2 column align and match. Here's the code for the 2 column one, and I need it to take the 3rd column and tie it too the 2nd and move that one as well when any column moves. Here is the code I already have:

    [code]

    Option Explicit
    Sub Isolate()
    Dim lastA, lastB, shortCol, rw As Integer
    'Determine short column so we know when to stop
    lastA = WorksheetFunction.CountA(Range("A:A"))
    lastB = WorksheetFunction.CountA(Range("B:B"))
    If lastA > lastB Then _
    shortCol = 2 Else shortCol = 1
    'Set First Check Row
    rw = 2
    nxtChk:
    'Check Column A against Column B, Row by Row
    'Insert cell at non-matching data
    If Cells(rw, 1) <> "" And Cells(rw, 1) < Cells(rw, 2) Then
    Cells(rw, 2).Insert shift:=xlDown
    Else
    If Cells(rw, 2) <> "" And Cells(rw, 1) > Cells(rw, 2) Then
    Cells(rw, 1).Insert shift:=xlDown
    End If
    End If
    'If there is nothing left to check in the Short Column, we're done
    If Cells(Rows.Count, shortCol).End(xlUp).Row + 1 = rw Then Exit Sub
    'If not, increment Row counter and loop
    rw = rw + 1
    GoTo nxtChk
    End Sub
    [code\]
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Align 3 columns with 2 columns of matching data

    bearnbillie,

    Welcome to the Excel Forum.


    Detach/open workbook AlignAtoBC - bearnbillie - EF812811 - SDG15.xls and run the AlignAtoBC macro.


    If you want to use the macro on another workbook:


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    1. Copy the below code, by highlighting the code and 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. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
    7. Press the keys ALT + Q to exit the Editor, and return to Excel
    8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


    Please Login or Register  to view this content.

    Then run the AlignAtoBC macro.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Align 3 columns with 2 columns of matching data

    bearnbillie,

    I forgot to check and sort your data, first by column A ascending, and then column B and C ascending.


    Detach/open workbook AlignAtoBC - bearnbillie - EF812811 - SDG15.xls and run the AlignAtoBC macro.



    If you want to use the macro on another workbook:


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    1. Copy the below code, by highlighting the code and 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. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
    7. Press the keys ALT + Q to exit the Editor, and return to Excel
    8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


    Please Login or Register  to view this content.

    Then run the AlignAtoBC macro.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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