I have two spreadsheets (wb1 and wb2). The goal is to select each value in column D of wb1, find the value in column C of wb2, then copy a range of cells (same row as the search value) back to wb1.
Here's the code I've managed to pull together thus far:
Dim rng1 As Range, rng2 As Range
Dim cell as Variant
Dim cell_val as String
Dim wb1 as Workbook, wb2 as Workbook
Dim sh1 as Worksheet, sh2 as Worksheet
Sub Find_Copy_Paste()
set wb1 = Workbooks.Open("c:\explicit\path\to\wb1.xlsm") <---This fails
set wb2 = Workbooks.Open("c:\explicit\path\to\wb2.xlsm") <---This fails
Set sh1 = wb1.Open("Inventory")
set sh2 = wb2.Open ("Sheet1")
set rng1 = wb1.sh1.Range("D6:D1702")
set rng2 = wb2.sh2.Range("C2:C3132")
For Each cell In rng1
ActiveCell.Select
cell_val = Selection.Copy
Windows(wb2).Activate
Cells.Find(What:=(cell_val), After:=ActiveCell,
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset (0,1).Range("A1:AH1").Select
Application.CutCopyMode = False
Selection.Copy
Windows(wb1).Activate
ActiveCell.Offset(0,1).Range("A1").Select
ActiveSheet.Paste
cell_val=""
Next
End Sub
Unfortunately, I'm hitting a challenge, and I suspect it has to do with two things: 1) wb1 and wb2 variables and how I've assigned them, and 2) the variable in the Cells.Find part of my code (but I'm still fairly new to VBA, so my suspicions might be way off).
set wb1 = Workbooks.Open("c:\explicit\path\to\wb1.xlsm") <---This fails
- can you clarify? It fails only if you have wrong path. – Dmitry Pavlivset
's should be proper caseSet
- any chance you've declared a variable calledset
somewhere in your code? – Tim Williams