Results 1 to 5 of 5

VBA Function - beginner

Threaded 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

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