+ Reply to Thread
Results 1 to 6 of 6

Macro to do logic based on other column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-11-2008
    Location
    India
    MS-Off Ver
    Excel 2013
    Posts
    242

    Macro to do logic based on other column

    Hi,
    Want to create a macro based on column "N" at column at column "AR",I have created the below code but its taking reference to some other column,pl advice.
    Sub logic()
    Dim i As Long
    For i = 2 To Range("AR" & Rows.Count).End(xlUp).Row
        Select Case Range("AQ" & i).Value
            Case Is > 180
                Range("AR" & i).Value = "> 180"
            Case Is >= 90
                Range("AR" & i).Value = "90 - 180"
            Case Is >= 60
                Range("AR" & i).Value = "60 - 90"
            Case Is >= 30
                Range("AR" & i).Value = "30-60"
    Case Is >= 0
                Range("AR" & i).Value = "< 30"
     Case Else
                ' Don't change anything
        End Select
    Next i
    End Sub
    Attached Files Attached Files
    Last edited by arnab0711; 01-25-2011 at 05:27 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,451

    Re: Macro to do logic based on other column

    Hi aranab0711

    Two problems from the above code. You have no data in column AR so it will come back as 1 in the following line.
    For i = 2 To Range("AR" & Rows.Count).End(xlUp).Row
    I believe you want to change all of you "AR"'s in the code above ot "N".

    See if that works for you data.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    02-11-2008
    Location
    India
    MS-Off Ver
    Excel 2013
    Posts
    242

    Re: Macro to do logic based on other column

    Well,the above code is not working,the logic will be some what like this
    =IF(n2<=30,"0<30",IF(n2<=60,"30-60",if(n2<90,"60-90",if(n2<180,"90-180",">180"))))
    But I want to create a macro for this formula.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,451

    Re: Macro to do logic based on other column

    Hi,
    Not working is a little vague. What are your expected results?

  5. #5
    Forum Contributor
    Join Date
    02-11-2008
    Location
    India
    MS-Off Ver
    Excel 2013
    Posts
    242

    Re: Macro to do logic based on other column

    The macro will do somewhat like this -
    1)The macro will lookup for a number in column "N"
    2)In result will be in column "AR"
    3)The logic will be if the number is >180 it will ">180"
    if the # is >90 it will show "90 - 180"
    if the # is >60 it will show "60-90"
    if the # is >30 it will show "30-60"
    if the # is <30 it will show "<30"

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,451

    Re: Macro to do logic based on other column

    Try this;
    Option Explicit
    Sub GreaterThan180()
    Dim i As Long
    For i = 2 To Range("N" & Rows.Count).End(xlUp).Row
        Select Case Range("N" & i).Value
            Case Is > 180
                Range("AR" & i).Value = "> 180"
            Case Is >= 90
                Range("AR" & i).Value = "90 - 180"
            Case Is >= 60
                Range("AR" & i).Value = "60 - 90"
            Case Is >= 30
                Range("AR" & i).Value = "30-60"
    Case Is >= 0
                Range("AR" & i).Value = "< 30"
     Case Else
                ' Don't change anything
        End Select
    Next i
    End Sub
    Look at http://www.cpearson.com/excel/Debug.htm and use BreakPoints and Stepping. This section will help you do these with ease in the future.

+ 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