VBA-Excel -- Variable in Cells.Find - excel

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).

Try this below, I have simulated your goal with only 1 workbook. If the macro & path is not trusted, you may have issue opening the xlsm files. Here I only have one of them in ReadOnly mode (Workbook 2).
Sub Find_Copy_Paste()
Dim wb1 As Workbook, wb2 As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet
Dim rng1 As Range, rng2 As Range
Dim cell As Range, FoundCells As Range
Set wb1 = Workbooks.Open(Filename:="c:\explicit\path\to\wb1.xlsm",ReadOnly:=False)
Set wb2 = Workbooks.Open(Filename:="c:\explicit\path\to\wb2.xlsm",ReadOnly:=True)
Set sh1 = wb1.Worksheets("Inventory")
Set sh2 = wb2.Worksheets("Sheet1")
Set rng1 = sh1.Range("D6:D1702")
Set rng2 = sh2.Range("C2:C3132")
For Each cell In rng1
If Not IsEmpty(cell) Then
Set FoundCells = rng2.Find(cell.Value)
If Not FoundCells Is Nothing Then
Debug.Print """" & cell.Value & """ found at " & FoundCell.Worksheet.Name & "!" & FoundCell.Address
' Copy Found cell to one column on right of cell being searched for
FoundCells.Copy Destination:=cell.Offset(0, 1)
End If
End If
Next
Set rng1 = Nothing
Set rng2 = Nothing
Set sh1 = Nothing
Set sh2 = Nothing
Set wb1 = Nothing
Set wb2 = Nothing
End Sub
There are many good place to start learning VBA, for Excel 2010, check out the Excel Developer Reference.

Related

Referencing other sheets in loop not working

Hoping someone can help me figure out what's missing from my code. I have a validation list on one sheet, and I'm attempting to loop that list on a different sheet and change the filter on pivot table on that different sheet. The loop works through the validation list if I run the macro on the sheet with that validation cell, but on the sheet with my pivot table it only loops through the cells on that sheet. Consequently, if I'm on the sheet with the validation list, the loop works correctly through that validation list, but the pivot table filter doesn't change. I'm sure I'm missing something simple but I can't figure out what it is. Thanks in advance.
Sub Addvalidationlist() ' there's no problems with this macro, not the most elegant code but it works
Dim wb As Workbook
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim lastline As Long, newlastline As Long
Dim rng As Range
Dim rng2 As Range
Dim rng3 As Range
Set wb = ThisWorkbook
Set sh1 = wb.Sheets("qry_FB_forecast_file_NEW")
With sh1
lastline = sh1.Range("A" & Rows.Count).End(xlUp).Row
Set rng = sh1.Range("H1:H" & lastline)
rng.Copy Range("X:X")
sh1.Range("X:X").RemoveDuplicates Columns:=1, Header:=xlYes
newlastline = sh1.Range("X" & Rows.Count).End(xlUp).Row
Set rng2 = sh1.Range("X2:X" & newlastline)
Set rng3 = sh1.Range("Y1")
With rng3.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="='" & sh1.Name & "'!" & rng2.Address
End With
End With
' end of validation list sub
**Public Sub exportbybrand()** *This is the one I'm having trouble with *
Dim wb As Workbook
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim lastline As Long, newlastline As Long
Dim rng As Range
Dim rng2 As Range
Dim rng3 As Range
Dim valcell As Range
Dim valcellsource As Range
Dim Family As String
Set wb = ThisWorkbook
Set sh1 = wb.Worksheets("qry_FB_forecast_file_NEW")
Set valcell = ThisWorkbook.Worksheets("qry_FB_forecast_file_NEW").Range("Y1")
Set valcellsource = Evaluate(valcell.Validation.Formula1)
Set sh2 = wb.Worksheets("MR Front Edit")
On Error Resume Next
With sh1
For Each valcell In valcellsource
valcell.Value = valcell.Value
Family = valcell
MsgBox Family
With sh2
sh1.PivotTables("PivotTable1") _
.PivotFields("FamilyId").CurrentPage = Family
End With
Next
End With
End Sub

Trying to get Macro to automatically adjust DataRange for Pivot Table

I'm sorry if this is basic but I can't seem to figure this out .
Long story short my file has several tabs with lot's of data and each tab has a pivot table. What I need is a Macro that adjusts the data range of the Pivot table to the data in current sheet. I have it now set with a bigger range giving me Blanks which drives me nuts.
Here is my code :
Dim Data_Sheet As Worksheet
Dim Pivot_Sheet As Worksheet
Dim StartPoint As Range
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String
Dim LastCol As Long
Dim lastRow As Long
'Set Pivot Table & Source Worksheet
Set Data_Sheet = ThisWorkbook.Worksheets("1")
Set Pivot_Sheet = ThisWorkbook.Worksheets("1")
'Enter in Pivot Table Name
PivotName = "PivotTable1"
'Defining Staring Point & Dynamic Range
Data_Sheet.Activate
Set StartPoint = Data_Sheet.Range("V2")
LastCol = StartPoint.End(xlToRight).Column
DownCell = StartPoint.End(xlDown).Row
Set DataRange = Data_Sheet.Range(StartPoint, Cells(DownCell, LastCol))
NewRange = Data_Sheet.Name & "!" & DataRange.Address(ReferenceStyle:=xlR1C1)
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange _
, Version:=6)
Now when I try this I get the "Run time error -2147024809" error stating that The pivottable name is invalid .
This updates all the sheets that have 1 pivot table
Sub Update()
Dim wb As Workbook, ws As Worksheet
Dim rng As Range, cell As Range, newrange As String
Set wb = ActiveWorkbook
For Each ws In wb.Sheets
With ws
If .PivotTables.Count = 1 Then
Set cell = .Range("V2").End(xlToRight).End(xlDown)
Set rng = .Range("V2", cell)
newrange = "'" & ws.Name & "'!" & rng.Address(ReferenceStyle:=xlR1C1)
.PivotTables(1).ChangePivotCache _
wb.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=newrange, Version:=6)
End If
End With
Next
End Sub

Conditional copy and paste excel vba

I'm trying to copy and paste a certain value from a cell in one sheet matching a range in another workbook. The code runs fine, doesn't give any run-time errors, but will not paste in the range declared in the other workbook. Code below
Sub ConditionalCopy()
Dim dest As Worksheet
Set dest = ActiveWorkbook.Worksheets("VCP Plan")
Dim rng As Range, cell As Range
Set rng = Range("D:D")
Dim OpenWorkBook As Variant
OpenWorkBook = Application.GetOpenFilename("Excel Files (*.xlsx* (*.xlsx*),")
If OpenWorkBook <> False Then
Workbooks.Open (OpenWorkBook)
End If
For Each cell In rng
If cell.Value = "26ASA00015D007" Then
cell.Offset(0, 3).Copy Destination:=dest.Range("E3")
End If
Next cell
End Sub
It is unclear from your description and your code which workbook/worksheet you want to compare and copy, and which workbook/worksheet you want to copy to.
You'll need to be more specific
I've made a guess at what you are trying to do. If I've got it wrong, simply adjust the references to suit
Something like
Sub ConditionalCopy()
Dim wbSource as Workbook
Dim wsSource as Worksheet
Dim rSource as Range
Dim wbDest as Workbook
Dim wsDest as Worksheet
Dim rDest as Range
Set wbDest = ActiveWorkbook ' Are you sure?
Set wsDest = wbDest.Worksheets("VCP Plan")
Set rDest = ws.Range("E3")
Dim OpenWorkBook As Variant
OpenWorkBook = Application.GetOpenFilename("Excel Files (*.xlsx* (*.xlsx*),")
If OpenWorkBook <> False Then
Set wbSource = Workbooks.Open(OpenWorkBook)
Else
Exit Sub
End If
Set wsSource = wbSource.Worksheets("NameOfSourceSheet")
Dim cell As Range
With wsSource
' Column D from row 1 to last used row
Set rSource = .Range(.Cells(1, 4), .Cells(.Rows.Count, 4).End(xlUp))
End With
For Each cell In rSource
If cell.Value = "26ASA00015D007" Then
cell.Offset(0, 3).Copy Destination:=rDest
' You probably don't want to overwrite each time, so
Set rDest = rDest.Offset(1, 0)
End If
Next cell
End Sub

Importing from multiple sheets

I have posted recently a Importing values from multiple sheets from one file into another file. I think I have the code to solve this but the problem is that I cannot see a place to integrate it.
My actual code is this:
Option Explicit
Sub ImportData()
Application.ScreenUpdating = False
Dim Path As String, Lstrw As Long
Dim SourceWb As Workbook
Dim TargetWb As Workbook
Path = "C:\Users\DZPH8SH\Desktop\Status 496 800 semana 12 2015.xls" 'Change this to your company workbook path
Workbooks.Open (Path)
Set SourceWb = Workbooks("Status 496 800 semana 12 2015.xls") 'Change "Source" to the name of your company workbook
'Part that needs some adjustments in down below
Set TargetWb = Workbooks("Master_Atual_2015.xlsm") 'change the file address
Lstrw = SourceWb.Sheets(1).Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
With SourceWb.Sheets(1)
.Application.Union(.Range("D2:D" & Lstrw), .Range("F2:F" & Lstrw), .Range("I2:I" & Lstrw), .Range("M2:M" & Lstrw)).Copy Destination:=TargetWb.Sheets(1).Range("A3")
End With
SourceWb.Close savechanges:=False
Application.ScreenUpdating = True
End Sub
And what I need to put in my code to make it copy information from both sheets is this:
Sub MoveData()
Dim LastRow As Long, WS1 As Worksheet, WS2 As Worksheet
Set WS1 = Sheets("Sheet1")
Set WS2 = Sheets("Sheet2")
LastRow = WS1.Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
Intersect(WS1.Rows("2:" & LastRow), WS1.Range("D:D,F:F,I:I,M:N")).Copy WS2.Range("A3")
End Sub
this is a quote from the person that gave me the code hope it helps to find a correct place in the code because I'm not being able to find one.
the following code will work from one sheet to a second sheet within the same workbook, so I would think all you have to do is qualify the Sheets properties in the two statements where WS1 and WS2 are Set (highlighted in blue) with the workbook references and the rest of the code should work from there
There is always a different way to do things. Here is another option.
Sub Button1_Click()
Dim WS1 As Worksheet, WS2 As Worksheet
Dim RangeArea As Range, x
Set WS1 = Sheets("Sheet1")
Set WS2 = Sheets("Sheet2")
x = 0
For Each RangeArea In WS1.Range("D:D,F:F,I:I,M:N").SpecialCells(xlCellTypeConstants, 23).Areas
RangeArea.Copy WS2.Range("A3").Offset(0, x)
x = x + 1
Next RangeArea
End Sub

How to copy a line in excel using a specific word and pasting to another excel sheet?

I have checked a bunch of different posts and can't seem to find the exact code I am looking for. Also I have never used VBA before so I'm trying to take codes from other posts and input my info for it to work. No luck yet. At work we have a payroll system in Excel. I am trying to search for my name "Clarke, Matthew" and then copy that row and paste it to the workbook I have saved on my desktop "Total hours".
CODE
Sub Sample()
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyFrom As Range
Dim lRow As Long '<~~ Not Integer. Might give you error in higher versions of excel
Dim strSearch As String
Set wb1 = ThisWorkbook
Set ws1 = wb1.Worksheets("yourSheetName")
strSearch = "Clarke, Matthew"
With ws1
'~~> Remove any filters
.AutoFilterMode = False
'~~> I am assuming that the names are in Col A
'~~> if not then change A below to whatever column letter
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("A1:A" & lRow)
.AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
End With
'~~> Remove any filters
.AutoFilterMode = False
End With
'~~> Destination File
Set wb2 = Application.Workbooks.Open("C:\Sample.xlsx")
Set ws2 = wb2.Worksheets("Sheet1")
With ws2
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lRow = 1
End If
copyFrom.Copy .Rows(lRow)
End With
wb2.Save
wb2.Close
End Sub
SNAPSHOT
Expanding on what timrau said in his comment, you can use the AutoFilter function to find the row with your name in it. (Note that I'm assuming you have the source workbook open)
Dim curBook As Workbook
Dim targetBook As Workbook
Dim curSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastRow As Integer
Set curBook = ActiveWorkbook
Set curSheet = curBook.Worksheets("yourSheetName")
'change the Field number to the correct column
curSheet.Cells.AutoFilter Field:=1, Criteria1:="Clarke, Matthew"
'The Offset is to remove the header row from the copy
curSheet.AutoFilter.Range.Offset(1).Copy
curSheet.ShowAllData
Set targetBook = Application.Workbooks.Open "PathTo Total Hours"
Set targetSheet = targetBook.WorkSheet("DestinationSheet")
lastRow = Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
targetSheet.Cells(lastRow + 1, 1).PasteSpecial
targetBook.Save
targetBook.Close
As you can see I put placeholders in for the specific setup of your workbook.
I know this is old, but for anyone else searching for how to do this, it can be done in a much more direct fashion:
Public Sub ExportRow()
Dim v
Const KEY = "Clarke, Matthew"
Const WS = "Sheet1"
Const OUTPUT = "c:\totalhours.xlsx"
Const OUTPUT_WS = "Sheet1"
v = ThisWorkbook.Sheets(WS).Evaluate("index(a:xfd,match(""" & KEY & """,a:a,),)")
With Workbooks.Open(OUTPUT).Sheets(OUTPUT_WS)
.[1:1].Offset(.[counta(a:a)]) = v
.Parent.Save: .Parent.Close
End With
End Sub

Resources