+ Reply to Thread
Results 1 to 6 of 6

Macro to copy data in rows based on criteria

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Macro to copy data in rows based on criteria

    I am looking for help to write a simple macro. Starting on column B66 is the word "Short" below that in the next 20 cells I would like a macro to write the word "Long Basket". If the word was "Long" then I would like the macro to write the word "Short Basket" for the next 20 cells (ex:B130). I would like a macro to loop through every example and do this. Can anyone help with this? thanks!!
    Attached Files Attached Files
    Last edited by rhudgins; 04-06-2010 at 08:24 AM.

  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 to copy data in rows based on criteria

    This should do it:
    Option Explicit
    
    Sub ShortLongBaskets()
    Dim RNG As Range, LastRw As Long, Rw As Long
    Application.ScreenUpdating = False
    
    LastRw = Range("B" & Rows.Count).End(xlUp).Row
    
    For Rw = 66 To LastRw Step 21
        Select Case LCase(Range("B" & Rw).Text)
            Case "short"
                Range("B" & Rw + 1).Resize(20, 1).Value = "Long Basket"
            Case "long"
                Range("B" & Rw + 1).Resize(20, 1) = "Short Basket"
        End Select
    Next Rw
    
    Application.ScreenUpdating = True
    End Sub
    _________________
    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!)

  3. #3
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Re: Macro to copy data in rows based on criteria

    Thank you so much this is perfect. One more question.

    For each example there is a ticker in column F(ex: F66, F87 ect.) Can you help me write a macro to identify the ticker in each example("ALK" in F66) and then find ALK in AA6:AN6 and copy the tickers below ALK in AB7:AB19 to F67? Loop this procedure through every example 1 through 78? Thank you so much!

  4. #4
    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 to copy data in rows based on criteria

    Try this:
    Option Explicit
    
    Sub ShortLongBaskets()
    Dim RNG As Range, rFind As Range
    Dim LastRw As Long, Rw As Long
    Application.ScreenUpdating = False
    
    LastRw = Range("B" & Rows.Count).End(xlUp).Row
    
    For Rw = 66 To LastRw Step 21
        Select Case LCase(Range("B" & Rw).Text)
            Case "short"
                Range("B" & Rw + 1).Resize(20, 1).Value = "Long Basket"
            Case "long"
                Range("B" & Rw + 1).Resize(20, 1) = "Short Basket"
        End Select
        Range(Range("F" & Rw + 1), Range("F" & Rw + 20)).FormulaR1C1 = _
            "=IF(INDEX(R7C27:R26C40, ROW(R[-" & Rw & "]C1), MATCH(R" & Rw & "C6, R6C27:R6C40, 0))=0,"""",INDEX(R7C27:R26C40, ROW(R[-" & Rw & "]C1), MATCH(R" & Rw & "C6, R6C27:R6C40, 0)))"
    Next Rw
    
    With Range("F:F")
        .Value = .Value
        .SpecialCells(xlCellTypeConstants, 16).Clear
    End With
    
    Application.ScreenUpdating = True
    End Sub
    Last edited by JBeaucaire; 04-05-2010 at 08:57 PM.

  5. #5
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Re: Macro to copy data in rows based on criteria

    Thank you so much this is exactly what I needed! I really appreciate all of the work.

  6. #6
    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 to copy data in rows based on criteria

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ 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