+ Reply to Thread
Results 1 to 8 of 8

Macro is inserting cells in one column, need it to insert in 2nd column as well

Hybrid View

  1. #1
    Registered User
    Join Date
    05-31-2012
    Location
    PA
    MS-Off Ver
    Excel 2007
    Posts
    53

    Exclamation Macro is inserting cells in one column, need it to insert in 2nd column as well

    I have a macro that is comparing column A to column B and where B does not match A it is inserting a cell and moving the other cells down a row so that the values match again. I have descriptions in column C that i need to move along with column B. Is there a macro for this?

    Thanks so much!

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Macro is inserting cells in one column, need it to insert in 2nd column as well

    Why dont you post your macro here? We can edit it for you.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Contributor
    Join Date
    01-20-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    186

    Re: Macro is inserting cells in one column, need it to insert in 2nd column as well

    sounds like you could do something simulair with a vlookup function?
    Can you post an example?

  4. #4
    Registered User
    Join Date
    05-31-2012
    Location
    PA
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Macro is inserting cells in one column, need it to insert in 2nd column as well

    Here is my macro. It is comparing values in row P with values in row F. When P does not match F... It is inserting cells and shifting cells down in Row P until they match F again. Row Q has a description in it. I just want Q to move down with the value that also moves in P. How do I add this?
    THANKS!!!!
    Option Explicit
    Sub Isolate()
    Dim lastF, lastP, shortCol, rw As Integer
    'Determine short column so we know when to stop
        lastF = WorksheetFunction.CountA(Range("F:F"))
        lastP = WorksheetFunction.CountA(Range("P:P"))
           If lastF > lastP Then _
              shortCol = 16 Else shortCol = 6
    'Set First Check Row
        rw = 3
    nxtChk:
    'Check Column F against Column P, Row by Row
    'Insert cell at non-matching data
      If Cells(rw, 6) <> "" And Cells(rw, 6) < Cells(rw, 16) Then
         Cells(rw, 16).Insert Shift:=xlDown
    
        Else
         If Cells(rw, 16) <> "" And Cells(rw, 6) > Cells(rw, 16) Then
            Cells(rw, 6).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
    Last edited by arlu1201; 06-01-2012 at 09:39 AM. Reason: Please use code tags in future.

  5. #5
    Forum Contributor
    Join Date
    01-20-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    186

    Re: Macro is inserting cells in one column, need it to insert in 2nd column as well

    What about adding this in for example columns R and S:

    R1 = F1
    S1 = Vlookup(F1; P:Q; 2; False)

  6. #6
    Registered User
    Join Date
    05-31-2012
    Location
    PA
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Macro is inserting cells in one column, need it to insert in 2nd column as well

    No that isn't working. Column Q doesn't relate to F. It is just wording that needs to stay aligned with the values that are in column P.

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Macro is inserting cells in one column, need it to insert in 2nd column as well

    Try changing this line
     Cells(rw, 16).Insert Shift:=xlDown
    to
    range(Cells(rw, 16),cells(rw,17).Insert Shift:=xlDown

  8. #8
    Registered User
    Join Date
    05-31-2012
    Location
    PA
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Macro is inserting cells in one column, need it to insert in 2nd column as well

    This worked!!!!!!!!!!!!! YES!!!!!! I just jumped out of my seat! hahaha! Thank you so so so so much!!!

+ 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