Consulting

Results 1 to 2 of 2

Thread: Sequence Function

  1. #1
    VBAX Newbie
    Joined
    Nov 2016
    Location
    Hyderabad
    Posts
    1
    Location

    Question Sequence Function

    Hi All,

    I'm trying a function to get sequence between 2 values. Below given example for better understand.

    1st Value = A1245
    2nd Value = A1257

    When I give above values in 'AddNum' function. It should give 'A1245,A1246,A1247' as result.

    Below given vba code wrote by myself and i requesting you, please help on this.


    Public x As Double
    Function AddNum(Val1 As Variant, Val2 As Variant)
    If Val1 = "" Or Val2 = "" Then
    Exit Function
    Else
    x = -(Right(Val1, 4) - Right(Val2, 4))
    End If
        Dim myary As Variant
    ReDim myary(x) As Variant
        For y = LBound(myary) To UBound(myary)
    Z = Right(Val1, 4) + y
    myary(y) = Left(Val1, 2) & Z
    ActiveCell.Offset(0, y + 1).Value = myary(y)
    Next y
        End Function
    Last edited by Aussiebear; 11-24-2016 at 07:09 AM. Reason: Added code tags

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Function AddNum(Val1 As String, Val2 As String)
        Dim i As Long
        Dim n As Long
        
        If Val1 = "" Or Val2 = "" Then Exit Function
        
        For i = CLng(Mid(Val1, 2)) To CLng(Mid(Val2, 2))
            n = n + 1
            ActiveCell.Offset(0, n).Value = Left(Val1, 1) & i
        Next i
    
    End Function
    Last edited by mana; 11-24-2016 at 06:33 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •