|
Question : Excel - Using Find Method to Locate a Cell in Another Open Workbook
|
|
Experts,
I am working on some code to extract certain values from an Excel model. I receive this model on a regular basis. There are no named ranges and sometimes rows are added, so direct cell references won't work. Each variable has a title that is always 3 cells to the left. Fortunately, the title doesn't change, so I'm trying to use the Find method to search for this title ("Purchase Price" in this example) and then go 3 cells to the right and get the value and assign it to a named range (ie "Range("PurchasePrice").Value") in my worksheet. I've pieced this much together, but need some help with the next step:
Sub ImportModel()
Dim wkbk1 As Workbook Dim wkbk2 As Workbook
Set wkbk1 = Workbooks(ActiveWorkbook.Name) Set wkbk2 = Workbooks(Range("ModelName").Value & ".xls") Cells.Find(What:="Purchase Price", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate Range("PurchasePrice").Value = wkbk2.Sheets(ActiveCell) Set wkbk1 = Nothing Set wkbk2 = Nothing
End Sub
Thanks for your help!
-Jim
|
|
Answer : Excel - Using Find Method to Locate a Cell in Another Open Workbook
|
|
Hi Jim,
I think what you're looking for is:
Sub ImportModel()
Dim wkbk1 As Workbook Dim wkbk2 As Workbook Dim purchase As Range
Set wkbk1 = ActiveWorkbook Set wkbk2 = Workbooks(Range("ModelName").Value & ".xls") Set purchase = wkbk2.ActiveSheet.Cells.Find(What:="Purchase Price", LookIn:= _ xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext) Range("PurchasePrice").Value = purchase.Offset(0, 3).Value Set wkbk1 = Nothing Set wkbk2 = Nothing Set purchase = Nothing End Sub
We're doing the .Find in wkbk2.ActiveSheet.Cells, rather than just Cells (defaults to the active workbook). You could also substitute Sheets(1) or Sheets("sheet name") for activesheet if you know the name. If the Range("PurchasePrice") gives you any trouble, you could use wkbk1.ActiveSheet.Range("PurchasePrice")
Matt
|
|
|
|