Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search fast in Excel from Word via VBA?

Status
Not open for further replies.

gtaborda

Programmer
May 13, 2007
75
GB
Hi there

i have some macros in Word which

1. opens Excel sheet from Word (hidden)
2. Searches through sheet for string
3. if found it returns the values of cells in same row
4. Those values are then used in Word


I am currently using a loop to goo through each row.
This works well but...it's too slow, specially when the excel sheet has >500 rows

Is there a faster way to seek and collect data from excel and use it in Word ?

thanks in advance for your help
 




Hi,

How about the Find & FindNext Methods?

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
Hi,

Yes, that's what I am looking for.

I've tried with
Set aRange = xl.ActiveSheet.Find(What:=txtclientName, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate

Somehow it does't work (it locks Excel)

So I am totallly lost. I don't know where to go from here,

I've looked a lot on the internet but I am unable to find anything about it.




'currentcolumn = ActiveCell.Column
' currentrow = ActiveCell.Row
 
You may try to replace this:
After:=ActiveCell
with this:
After:=xl.ActiveCell

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 



Avoid Activate and Select...
Code:
Set aRange = xl.[b]Sheets("YourSheetName").Cells[/b].Find(What:=txtclientName, After:=xl.[b]Sheets("YourSheetName").Cells.[A1][/b], LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
on subsequent searches...
Code:
Set aRange = xl.[b]Sheets("YourSheetName").Cells[/b].Find(What:=txtclientName, After:=[b]aRange[/b], LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)


Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
Hi

I've tried SkipVought's code with no luck

I get an error 438, "the object doesn't admit this property or method"

Same if I use xl.ActiveCell as per PHV comments
 
Hi gtaborda,

Perhaps it's time to post the relevant portions of your code ...

[MS MVP - Word]
 
Find the code so far...

This code runs when a Form is opened (from button on Toolbar)

The form has several fields, which I need to get filled after searching on CLIENTSDATA.XLS file (which is hidden)

If "txtclientName" is found (this variable comes from Word, then the 5 cells next to it in Excel need to automatically be added to the corresponding fields on the Word Form.

When the code gets to "Set aRange... " the code stops running for over 1 minute and finally it gives me error 438

I hope that makes sense

Public Sub getallclientsdata()


Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open (txtBlankDirectory & "\CLIENTSDATA.xls")
Set sh = xl.Worksheets("VARIABLES")
xl.Visible = False

Set aRange = xl.Sheets("VARIABLES").Cells.Find(What:=txtclientName, After:=xl.Sheets("VARIABLES").Cells.[A1], LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)


' THIS IS HOW I DO IT NOW, WORKS BUT IT IS TOO SLOW
For l = 2 To 3000
If txtclientName = sh.Range("A" & l).Value then
txtcode = sh.Range(("C"& l).Value
txtBAFCAF = sh.Range(("D" & l).Value
txtcombineprint = sh.Range(("E" & l).Value
txtPSS = sh.Range(("F" & l).Value
txtCAFon = sh.Range(("G" & l).Value
l = 3000

else
end if
next l


End Sub
 



You set the sheet and then you ignore the sheet object????
Code:
Set sh = xl.Worksheets("VARIABLES")
xl.Visible = False

Set aRange = sh.Cells.Find(What:=txtclientName, After:=sh.Cells.[A1], LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
Hi
Sorry but my knowledge in
sheet objects is limited

I've used Set aRange = sh.Cells.Find(What:=txtclientName, After:=sh.Cells.[A1], LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

however I still get the same error, although it happens instantly (intead of before that the system got locked for 1+ minutes)

438, "the object doesn't admit this property or method
 




The you have to use the Watch Window to discover what OBJECT is burping.

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 




Then you have to use the Watch Window to discover what OBJECT is burping.

Sorry

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
Do you have a reference to Ms Excel library set? If not, all xlConstants are not recognised.

combo
 
Yes combo, I do have reference to Excel ticked.

I'll investigate further

 
What happens after:

dim xl as excel.application
dim wb as excel.workbook
dim sh as excel worksheet
dim aRange as excel range
Set xl = CreateObject("Excel.Application")
set wb=xl.Workbooks.Open (txtBlankDirectory & "\CLIENTSDATA.xls")
...
' xl.Visible = False (do not hide excel, anyway, it should be hidden)
msgbox sh.usedrange.address ' what is the answer?
Set aRange = sh.usedrange.Find(What:=txtclientName, After:=xl.Sheets("VARIABLES").Cells.[A1], LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
...


combo
 
on msgbox sh.usedrange.address I get $A$1:$AP$3962

 
And still error 438? Is the aRange dimmed? If so, it should be as Excel.Range, otherwise it will be assumed to be Word.Range.

combo
 
cOMBO, I didn`t have it dimmed, now I do (Dim aRange As Excel.Range) however the error still there (438)

nuts

Do you have any example you could perhaps send me (that works) then I can just copy it/adapt it to my application?

thanks
 
With some code from excel vba help:
Code:
Sub Test()
Dim xlApp As Excel.Application
Dim xlWbk As Excel.Workbook
Dim xlWks As Excel.Worksheet
Dim xlRange As Excel.Range
Dim txtToFind As String, txtFirstFoundAddress As String

txtToFind = "N"
Set xlApp = New Excel.Application
' xlApp.Visible = True
Set xlWbk = xlApp.Workbooks.Open("D:\x\test.xls")
Set xlWks = xlWbk.Worksheets("test")
With xlWks.UsedRange
    Set xlRange = .Find(What:=txtToFind, After:=.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
        xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    If Not xlRange Is Nothing Then
        txtFirstFoundAddress = xlRange.Address
        MsgBox txtFirstFoundAddress
        Do
            Set xlRange = .FindNext(xlRange)
            If xlRange.Address <> txtFirstFoundAddress Then
                MsgBox xlRange.Address
            End If
        Loop While xlRange.Address <> txtFirstFoundAddress
    End If
End With
xlApp.Quit
End Sub


combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top