+ Reply to Thread
Results 1 to 6 of 6

if else formula for dynamic range

Hybrid View

  1. #1
    Registered User
    Join Date
    08-27-2012
    Location
    Hyd
    MS-Off Ver
    Excel 2010
    Posts
    8

    if else formula for dynamic range

    Hi,

    i need help again for a formula to use in a dynamic range.

    Emp Id Experience(yrs) Tenure
    123 4.5 4-5 yrs
    124 0.8 0-1 yrs
    535 6 yrs > 5 yrs


    Basically i have a dynamic range of emp Id and Experience, i need to write a formula to bring values like (0-1 yrs, 1-2 yrs, etc) in column Tenure.

    for the below code (truncated all ifs and else to avoid clutter), error arises: object variable or with block variable not set

    Private Sub MyMacro4()
             
          'Calculate Range of Tenure
          
          Dim ws As Worksheet
            Set ws = Worksheets("Separation")
                  
               Dim exp As Range
               exp = ws.Range("E22:E" & Range("E22").End(xlDown).Row).Value
               
               Dim Tenre As Range
               Tenre = ws.Range("F22:E" & Range("E22").End(xlDown).Row).Value
                          
               For Each exp In ws.Range("E22:E" & Range("E22").End(xlDown).Row)
               If exp >5 Then Tenre = "> 5 yrs"
                
                   Next exp
                           
          End Sub
    Thanks in advance,
    Anjale
    Last edited by anjalive; 04-21-2015 at 01:38 AM.

  2. #2
    Registered User
    Join Date
    12-10-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: if else formula for dynamic range

    You need to 'Set' your ranges like you did with your worksheet

    Set exp = ws.Range("E22:E" & Range("E22").End(xlDown).Row)
    Last edited by sericom; 04-21-2015 at 01:51 AM.

  3. #3
    Registered User
    Join Date
    08-27-2012
    Location
    Hyd
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: if else formula for dynamic range

    Oops.. my bad.. Thanks for pointing it out.
    Quote Originally Posted by sericom View Post
    You need to 'Set' your ranges

  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: if else formula for dynamic range

    Try this untested code

    Private Sub MyMacro4()
    Dim ws As Worksheet, exp As Range, r As Range, sResult As String
    
    Set ws = Worksheets("Separation")
    Set exp = ws.Range("E22:E" & Range("E22").End(xlDown).Row)
                    
    For Each r In exp.Cells
        If IsNumeric(r.Value) Then
            Select Case r.Value
                Case Is > 5
                    sResult = "> 5 yrs"
                Case Is > 1
                    sResult = "1-2 yrs"
                Case Is < 1
                    sResult = "0-1 yr"
            End Select
            r.Offset(, 1).Value = sResult
        End If
    Next r
    
    End Sub


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  5. #5
    Registered User
    Join Date
    08-27-2012
    Location
    Hyd
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: if else formula for dynamic range

    It works like a charm but sResult is calculated till row in excel rather than last row in-sync with dynamic Range E22.
    As sResult is a string, i cant use set.

    Any pointers?

    Quote Originally Posted by :) Sixthsense :) View Post
    Try this untested code

  6. #6
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: if else formula for dynamic range

    I don't know what you are trying to point out

    I am resetting sResult to none for each loop in the below code to avoid false result

    Private Sub MyMacro4()
    Dim ws As Worksheet, exp As Range, r As Range, sResult As String
    
    Set ws = Worksheets("Separation")
    Set exp = ws.Range("E22:E" & Range("E22").End(xlDown).Row)
                    
    For Each r In exp.Cells
        If IsNumeric(r.Value) Then
            sResult = ""
            Select Case r.Value
                Case Is > 5
                    sResult = "> 5 yrs"
                Case Is > 1
                    sResult = "1-2 yrs"
                Case Is < 1
                    sResult = "0-1 yr"
            End Select
            r.Offset(, 1).Value = sResult
        End If
    Next r
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Dynamic Range Formula
    By amartin575 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-07-2014, 04:32 PM
  2. [SOLVED] chart based on dynamic range which resizes as per data in range-formula / vba
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-11-2014, 04:02 PM
  3. [SOLVED] Change Static Range of Formula to Dynamic Range
    By duugg in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-24-2014, 10:15 AM
  4. Dynamic Range and using in a formula
    By bwaard in forum Excel General
    Replies: 2
    Last Post: 03-24-2012, 07:27 PM
  5. Replies: 2
    Last Post: 02-02-2006, 04:10 PM

Tags for this Thread

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