+ Reply to Thread
Results 1 to 5 of 5

VBA Function - beginner

Hybrid View

  1. #1
    Registered User
    Join Date
    06-28-2010
    Location
    Chester, uk
    MS-Off Ver
    Excel 2003
    Posts
    2

    VBA Function - beginner

    Hi

    I just begun using VBA

    I wrote a function to extract a text item from a single row or single column of data entries. The text item chosesn is a numbered item and all non text is ignored. So if the list consisted of "AA","BB"," ","DD","EE" say then the input agruments (3,list) to the function would generate the result DD and similarly (2,list) would generate the result BB.

    I wrote the function "nonzerotext" and the test harness subroutine "showtext" to display the output in a message box. This worked.

    However, the function does not work alone in the spreadsheet. Any ideas.

    See function and test harness below.

    Private Function nonzerotext(listpos As Integer, List As Range) As String
    
    Dim firstrow As Integer
    Dim lastrow As Integer
    Dim firstcolumn As Integer
    Dim rowcounter As Integer
    Dim numdetected As Integer
    
    firstrow = List.Row
    
    lastrow = List.Row + List.Count - 1
    
    firstcolumn = List.Column
    
    numdetected = 0
    
    For rowcounter = firstrow To lastrow
        
        If WorksheetFunction.IsText(List([rowcounter], [firstcolumn])) Then
            numdetected = numdetected + 1
            If numdetected = listpos Then
                nonzerotext = List([rowcounter], [firstcolumn])
            End If
        End If
    
    Next rowcounter
    
    End Function
    
    
    Sub showtext()
     
     Dim textis As String
        
        textis = nonzerotext(5, Range("A1:A10"))
            
        MsgBox textis
        
    End Sub
    Last edited by Leith Ross; 03-13-2011 at 10:28 PM. Reason: Added Code Tags

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: VBA Function - beginner

    Hi steve427,
    welcome to the forum

    Function nonzerotext(listpos As Integer, List As Range) As String
    no need for private as it "Indicates that the Function procedure is accessible only to other procedures in the module where it is declared." VBA help
    and to return the value in one sting
    nonzerotext = List([rowcounter], [firstcolumn]) & nonzerotext
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: VBA Function - beginner

    just clicked what you are doing. the number "numdetected" needs to progress on every loop
    the
     For rowcounter = firstrow To lastrow
            numdetected = numdetected + 1
            If WorksheetFunction.IsText(List(rowcounter, firstcolumn)) Then
                If numdetected = listpos Then
                    nonzerotext = List(rowcounter, firstcolumn)
                End If
            End If
    
        Next rowcounter

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,924

    Re: VBA Function - beginner

    I don't quite see the need for the loop and counter using your example; however, the code below solves the immediate problem.
        If WorksheetFunction.IsText(List(listpos, firstcolumn)) Then
            nonzerotext = List(listpos, firstcolumn)
        Else
            nonzerotext = "no text in range " & List.Address(0, 0)
        End If
    Your original code did not work with the example because you had an empty cell in the range but your counter is not adjusted to account for it so that after that cell the count is off by one...
    Ben Van Johnson

  5. #5
    Registered User
    Join Date
    06-28-2010
    Location
    Chester, uk
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: VBA Function - beginner

    Many thanks for all the replies.

    Having looked at the code you all posted it gave me an idea how to solve it.

    I changed the way I addressed the range and that seemed to do it.

    Thanks
    Steve

+ 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