Excel VBA

cunningruss

New Member
Joined
Oct 31, 2012
Messages
2
Learning VBA on the job, I'm not able to get the following code to work. Would appreciate some support. I receive Runtime error '91' at Line 6

Dim xlApp As New Excel.Application
Dim xlBook As Excel.WorkBook
Dim xlSheet As Excel.WorkSheet
Dim FoundCell As Range
xlApp.WorkBooks.Open ("C:\temp\inventory.xlsx")
xlBook.WorkSheets("Sheet1").Activate
Set xlSheet = xlBook.ActiveSheet
With xlSheet.Range("B3:B145")
Set FoundCell = .Find(What:=MyName, After:=.Cells(.Cells.Count), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
End With
.....
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this. This two lines I made changes to have a double asterick (**) at the end:

Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim FoundCell As Range
xlApp.Workbooks.Open ("C:\temp\inventory.xlsx")
Worksheets("Sheet1").Activate '**
'xlBook.Worksheets("Sheet1").Activate
'Set xlSheet = xlBook.ActiveSheet
Set xlSheet = ActiveWorkbook.ActiveSheet '**
With xlSheet.Range("B3:B145")
Set FoundCell = .Find(What:=MyName, After:=.Cells(.Cells.Count), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
End With
 
Upvote 0
You need to set xlBook instead of xlApp.Workbooks.open("C\temp\inventory.xlsx
Code:
Set xlBook = Worbooks.Open("C:\temp\inventory.xlsx")
Code:
 
Upvote 0
Thanks for solution, with reference in hand I can't resolve Run-time error '424' at line 15. Because passing MyName as variant?


Dim MyName As Variant
Dim strName As String
MyName =Application.UserName
Selection.TypeText (MyName)
strName = ActiveDocument.Name
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim FoundCell As Range
xlApp.Workbooks.Open ("C:\temp\inventory.xlsx")
Worksheets("Sheet1").Activate
'Set xlSheet = xlBook.ActiveSheet
With xlSheet.Range("B3:B145")
Set FoundCell = .Find(What:=MyName, After:=.Cells(.Cells.Count), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not FoundCell Is Nothing Then
.Rows(FoundCell).EntireRoll.Copy
ActiveDocument.Bookmarks(Equipment).Select
Selection.Paste
Else
' FindNext code
End If
End With
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,413
Members
449,449
Latest member
Quiet_Nectarine_

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top