JustCode : Find first cell with specific content

This function will find the first cell of the active worksheet with a specific content and return the column or row number.

' ****************************************
' Designed by : Smarter Dimensions
' Last Updated : 5th July 2009
' Function to find first cell with specific content
' ****************************************

Dim oExcel,oWorkBook,oSheet
dim iCel,oCell,iStartCol,iEndCol

Set oExcel = createobject("Excel.Application")

oExcel.Workbooks.Open("C:\TEMP\ExcelExport.xls")

oExcel.Visible = True

oWorkBook = oExcel.ActiveWorkbook

Set oSheet = oWorkBook.ActiveSheet

iStartCol = 1
iEndCol = FindFirstCell(3,9,"C","",oSheet)


Function FindFirstCell(iCol,iRow,sDirection,sLookFor,oSheet)

     Do while oSheet.Cells[iRow][iCol].text <> sLookFor
         Select Case sDirection
             Case = "C"
                 iCol = iCol + 1
             Case = "R"
                 iRow = iRow + 1
        End Select
     Loop

     Select Case sDirection
         Case = "C"
             FindFirstCell = iCol - 1
         Case = "R"
             FindFirstCell = iRow - 1
     End Select

End Function

Leave a Comment