1. John Vanleeuwe
  2. PowerBuilder
  3. Friday, 24 August 2018 12:02 PM UTC

Hi all,

I need to find a certain string in an excel sheet through OLE automation. The value can be anywhere in the sheet.

Here's a small snippet of what i have got till now.

In this example i want to look in the active sheet for the word "#STARTPOSITION"

i can use the function Find for this.

Problem is , how do i know if the word was on the sheet or not ? Can i convert the result of the Find function into a string or ... ?

 

....
ll_last = lole_MainWorkBook.Worksheets.count
lole_MainWorkBook.Activate
lole_sheet = lole_MainWorkBook.WorkSheets(ll_ctr)
lole_sheet.Activate()
lole_sheet.Cells.Find('#STARTPOSITION')

....

 

TIA

John

 

 

 

 

 

Accepted Answer
René Ullrich Accepted Answer Pending Moderation
  1. Tuesday, 28 August 2018 10:20 AM UTC
  2. PowerBuilder
  3. # Permalink

The Excel Find Method returns the first cell where that information was found. If there was no match the result is NULL.

To Find all cells you have to use Find and FindNext.

To get the value of the cell you found you can access the properties of the cell (e.g. the Value property).

 

See here for more infomation:

https://docs.microsoft.com/de-de/office/vba/api/excel.range.find?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev11.query%3FappId%3DDev11IDEF1%26l%3Dde-DE%26k%3Dk(vbaxl10.chm144128)%3Bk(TargetFrameworkMoniker-Office.Version%3Dv16)%26rd%3Dtrue

 

 

Comment
  1. John Vanleeuwe
  2. Tuesday, 28 August 2018 11:35 AM UTC
Rene,



thanks a lot , but the problem is exactly how do i get the return value (column name) from the Find function in a pb variable ? I tried with the any variable, but the messagebox always shows me my search string and not the found column ?



....

ll_last = lole_MainWorkBook.Worksheets.count

lole_MainWorkBook.Activate

lole_sheet = lole_MainWorkBook.WorkSheets(ll_ctr)

lole_sheet.Activate()

any_a = lole_sheet.Cells.Find('#STARTPOSITION')

If IsNull(any_a) Then

messagebox("Nothing Found...","null values")

Else

messagebox("String found", string(any_a)) // any_a will always have the search string , in my example #STARTPOSITION and not the column name ?

End If

....



  1. Helpful
  1. René Ullrich
  2. Tuesday, 28 August 2018 11:45 AM UTC
Find returns a Range object for the Cell.

To get the column or row you have to access the properties of this object.

https://docs.microsoft.com/de-de/office/vba/api/excel.range%28object%29



e.g.

oleobject lole_cell

lole_cell = lole_sheet.Cells.Find("...")

IF NOT IsNull (lole_cell) THEN

MessageBox ("Col", lole_cell.Column) // Column property returns the column number (1 = A, 2 = B, ...)

END IF
  1. Helpful
There are no comments made yet.
DIPANJAN DUTTA Accepted Answer Pending Moderation
  1. Tuesday, 28 August 2018 14:20 PM UTC
  2. PowerBuilder
  3. # 1

Hi John,

Lets try this code, may be its solve your issue. I have also attached excel file.

OleObject lole_MainWorkBook, lole_sheet, lole_cell
Long ll_last, ll_ctr, ll_result
Any la_col
String ls_Address, ls_FirstAddress, ls_SheetName

lole_MainWorkBook = CREATE OLEObject
ll_result = lole_MainWorkBook.ConnectToObject("D:\TestPB\sample.xls")

If ll_result <> 0 then
    MessageBox( "Error", 'Error running MS Excel api.')
    Destroy lole_MainWorkBook
Else
    lole_MainWorkBook.Activate

    ll_last = lole_MainWorkBook.Worksheets.count

   //Code run for all sheet in a workbook

    For ll_ctr = 1 to ll_last
        lole_sheet = lole_MainWorkBook.WorkSheets(ll_ctr)
        lole_sheet.Activate()
        ls_SheetName = String(lole_sheet.name)

//Search '123' from excel
        lole_cell = lole_sheet.Cells.Find("123")

        If Not IsNull(lole_cell) Then
            ls_FirstAddress = String(lole_cell.Address)
            MessageBox("ls_FirstAddress", ls_SheetName + ' - ' + ls_FirstAddress)
            ls_Address = ""

//Running into do while loop until got the first address
                Do While (ls_FirstAddress <> ls_Address)

//Find next if no match found it will return the first match address
                    lole_cell = lole_sheet.Cells.FindNext(lole_cell)
                    ls_Address = String(lole_cell.Address)
                    If ls_FirstAddress <> ls_Address Then MessageBox("ls_Address", ls_SheetName + ' - ' + ls_Address)

               Loop
          End If
    Next
End If

 

Comment
There are no comments made yet.
John Vanleeuwe Accepted Answer Pending Moderation
  1. Tuesday, 28 August 2018 09:20 AM UTC
  2. PowerBuilder
  3. # 2

Hi guys,

 

anyone with some more profound excel ole knowledge please ?

 

TIA

John

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.