Run Time Error 91 during Find using Array - excel

Not had idea what the root cause for the error it only occur when the program try to find the value in array at the third value.
Public Sub GetBGA()
Dim PMIC() As String
Dim PartNumber1 As Long
Dim Counter As Long
Worksheets("Test1").Select
PartNumber1 = Range("A1", Range("A1").End(xlDown)).Cells.Count
ReDim PMIC(1 To PartNumber1)
For Counter = 1 To PartNumber1
PMIC(Counter) = Range("A1").Offset(Counter - 1, 0).Value
Next Counter
For Counter = 1 To PartNumber1
Worksheets("Test2").Select
Cell.Find(What:=PMIC(Counter), After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Copy
Worksheets("Test3").Select
Rows(1).Insert
Next Counter
End Sub

Copy Matching Rows
Option Explicit
Sub GetBGA()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Lookup
Dim lws As Worksheet: Set lws = wb.Worksheets("Test1")
Dim lfCell As Range: Set lfCell = lws.Range("A1")
Dim llCell As Range: Set llCell = lws.Cells(lws.Rows.Count, "A").End(xlUp)
Dim lrg As Range: Set lrg = lws.Range(lfCell, llCell)
Dim lrCount As Long: lrCount = lrg.Rows.Count
Dim lData() As Variant
If lrCount = 1 Then
ReDim lData(1 To 1, 1 To 1): lData(1, 1) = lrg.Value
Else
lData = lrg.Value
End If
' Source (don't you know the column?)
Dim sws As Worksheet: Set sws = wb.Worksheets("Test2")
Dim srg As Range
Dim slCell As Range
With sws.UsedRange
Set slCell = .Cells(.Rows.Count, .Columns.Count)
Set srg = sws.Range("A1", slCell)
End With
' Destination
Dim dws As Worksheet: Set dws = wb.Worksheets("Test3")
Dim drrg As Range: Set drrg = dws.Range("A1").Resize(, srg.Columns.Count)
' Loop (are you sure about 'xlPart' and '.Insert'?).
Application.ScreenUpdating = False
Dim lr As Long
Dim srrg As Range
Dim sCell As Range
For lr = lrCount To 1 Step -1 ' switch order with 'For lr = 1 To lrCount'
Set sCell = srg.Find(lData(lr, 1), slCell, xlFormulas, xlPart, xlByRows)
If Not sCell Is Nothing Then
Set srrg = srg.Rows(sCell.Row)
srrg.Copy
drrg.Insert
End If
Next lr
Application.ScreenUpdating = True
' Inform.
MsgBox "Data copied.", vbInformation
End Sub

Related

I can't compare the Dates on VBA

I'm trying to compare the dates that I choose. I mean I'm trying to take the some items which has a date earlier. So I wrote this on VBA. But I noticed that when I run this code the output was the same as input. So it tries to find the earlier items but it couldn't compare so all items are copied.
Private Sub Macro1()
a = Worksheets("SVS").Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To a
If Worksheets("SVS").Cells(i, 22).Value < CDate("28/02/2023") Then
Worksheets("SVS").Rows(i).Copy
Worksheets("Summary").Activate
b = Worksheets("Summary").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Summary").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("SVS").Activate
End If
Next i
Application.CutCopyMode = False
ThisWorkbook.Worksheets("SVS").Cells(1, 1).Select
End Sub
What is missing in the code? I wanna learn.
Check you have a valid date to compare with.
Option Explicit
Private Sub Macro1()
Dim wb As Workbook, ws As Worksheet, v
Dim lastrow As Long, i As Long, b As Long, n As Long
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Summary")
b = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
With wb.Sheets("SVS")
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 3 To lastrow
v = .Cells(i, 22) ' col V
If IsDate(v) Then
If CDbl(v) < DateSerial(2023, 2, 28) Then
b = b + 1
.Rows(i).Copy ws.Cells(b, 1)
n = n + 1
End If
End If
Next i
End With
MsgBox n & " rows copied to Summary", vbInformation, lastrow - 2 & " rows checked"
End Sub
Append If Earlier Date
Option Explicit
Sub AppendEarlierDate()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim sws As Worksheet: Set sws = wb.Sheets("SVS")
Dim srg As Range
Set srg = sws.Range("V3", sws.Cells(sws.Rows.Count, "V").End(xlUp))
Dim surg As Range, sCell As Range, sValue
For Each sCell In srg.Cells
sValue = sCell.Value
If IsDate(sValue) Then
If sValue < DateSerial(2023, 2, 28) Then
If surg Is Nothing Then
Set surg = sCell
Else
Set surg = Union(surg, sCell)
End If
End If
End If
Next sCell
If surg Is Nothing Then Exit Sub
Dim dws As Worksheet: Set dws = wb.Sheets("Summary")
If dws.FilterMode Then dws.ShowAllData
Dim dlCell As Range, dfCell As Range
Set dlCell = dws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious)
If dlCell Is Nothing Then
Set dfCell = dws.Range("A1")
Else
Set dfCell = dws.Cells(dlCell.Row + 1, "A")
End If
surg.EntireRow.Copy dfCell
End Sub

Copy from column A instead of column F?

I want to copy and paste columns from Sheet W2W to Sheet OTD Analysis when column F value doesn’t exist in OTD Analysis.
This code copied column F:AU instead of A:AU.
Sub Transfer()
Application.ScreenUpdating = False
Dim LastRow As Long
LastRow = Sheets("W2W").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim rng As Range
Dim foundVal As Range
For Each rng In Sheets("W2W").Range("F2:F" & LastRow)
Set foundVal = Sheets("OTD Analysis").Range("F:F").Find(rng, LookIn:=xlValues, lookat:=xlWhole)
If foundVal Is Nothing Then
rng.Columns("A:AU").Copy
Sheets("OTD Analysis").Activate
b = Sheets("OTD Analysis").Cells(Rows.Count,1).End(xlUp).Row
Sheets("OTD Analysis").Cells(b + 1, 1).Select
ActiveSheet.Paste
End If
Next rng
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
You want Columns("A:AU") in reference to the entire row.
rng.EntireRow.Columns("A:AU").Copy
Transfer New Entries
Let's assume that rng is cell F2. Then
rng.Columns("A:AU") refers to the range F2:AZ2,
rng.EntireRow refers to the range A2:XFD2,
rng.EntireRow.Columns("A:AU") refers to the range A2:AU2.
Option Explicit
Sub TransferNewEntries()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Source - to be read (copied) from
Dim sws As Worksheet: Set sws = wb.Worksheets("W2W")
Dim slRow As Long
slRow = sws.UsedRange.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
Dim srg As Range: Set srg = sws.Range("A2", sws.Cells(slRow, "AU"))
Dim scrg As Range: Set scrg = sws.Range("F2", sws.Cells(slRow, "F"))
' or e.g. just 'Set scrg = srg.Columns(6)'
' Destination - to be written (pasted) to
Dim dws As Worksheet: Set dws = wb.Worksheets("OTD Analysis")
Dim dlRow As Long
dlRow = dws.UsedRange.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
Dim dcrg As Range: Set dcrg = dws.Range("F2", dws.Cells(dlRow, "F"))
Dim surg As Range
Dim sCell As Range
Dim sr As Long
Dim drIndex As Variant
Dim drCount As Long
For Each sCell In scrg.Cells
sr = sr + 1 ' the n-th cell of the source column range...
' ... more importantly, the n-th row of the source range
drIndex = Application.Match(sCell.Value, dcrg, 0)
If IsError(drIndex) Then ' source value was not found
drCount = drCount + 1 ' count the rows to be copied
If surg Is Nothing Then ' combine the rows into a range...
Set surg = srg.Rows(sr)
Else
Set surg = Union(surg, srg.Rows(sr))
End If
'Else ' source value was found; do nothing
End If
Next sCell
If surg Is Nothing Then
MsgBox "No new entries (no action taken).", vbExclamation
Exit Sub
End If
Dim dfCell As Range: Set dfCell = dws.Cells(dlRow + 1, "A")
surg.Copy dfCell ' ... to be copied in one go
MsgBox "New entries copied: " & drCount, vbInformation
End Sub

VBA how to copy paste specific cells if condition is met not all row

I have a code to copy paste all rows which met condition, but how to copy not all rows but specific cells from those rows? For example just A, C and D cells from row.
Sub CopyRow_Item()
Application.ScreenUpdating = False
Dim LastRow As Long
Dim last_row As Long
Item = ThisWorkbook.Sheets("Sheet1").Range("B1").Value
LastRow = Sheets("Actuals").Cells.Find("*", SearchOrder:=xlByRows,
SearchDirection:=xlPrevious).Row
last_row = Cells(Rows.Count, "A").End(xlUp).Row
Dim x As Long
x = 1
Dim rng As Range
For Each rng In Sheets("Actuals").Range("A2:A" & LastRow)
If rng = Item Then
rng.EntireRow.Copy
Sheets("Sheet1").Cells(last_row + x, 1).PasteSpecial xlPasteValues
x = x + 1
End If
Next rng
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Copy Non-Contiguous Rows
'*** marks the spots.
Option Explicit
Sub CopyRow_Item()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets("Actuals")
Dim slrCell As Range: Set slrCell = sws.Cells _
.Find("*", , xlFormulas, , xlByRows, xlPrevious)
If slrCell Is Nothing Then Exit Sub ' no data
Dim srg As Range: Set srg = sws.Range("A2:A" & slrCell.Row)
Dim svrg As Range: Set svrg = sws.Range("A:A,B:B,D:D") '***
Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet1")
Dim Criterion As Variant: Criterion = dws.Range("B1").Value
Dim dCell As Range: Set dCell = dws.Columns("A") _
.Find("*", , xlFormulas, , xlByRows, xlPrevious)
If dCell Is Nothing Then
Set dCell = dws.Range("A2")
Else
Set dCell = dCell.Offset(1)
End If
Application.ScreenUpdating = False
Dim sCell As Range
For Each sCell In srg.Cells
If sCell.Value = Criterion Then
Intersect(sCell.EntireRow, svrg).Copy '***
dCell.PasteSpecial xlPasteValues
Set dCell = dCell.Offset(1)
End If
Next sCell
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

Matching the Three Criteria and Copy Paste the Data

Below code match the string in the specific range (this range contains Headers) if finds then copy the whole column and paste into Sheet2.
I want to add two more condition in below code that are:
Dim FindValue2 As String
Dim FindValue3 As String
FindValue2 = shSummary.Range("A2").Value
FindValue3 = shSummary.Range("B2").Value
and match in Sheet1 Column A for FindValue3 and Column F for FindValue2 after matching these 3 criteria then copy and paste the data.
Your help will be much appreciated.
Sub find()
Dim foundRng As Range
Dim FindValue As String
Dim lastRow As Long
Set shData = Worksheets("Sheet1")
Set shSummary = Worksheets("Sheet2")
FindValue = shSummary.Range("C2")
Set foundRng = shData.Range("G1:Z1").find(FindValue)
With shData
lastRow = .Cells(.Rows.Count, foundRng.Column).End(xlUp).Row
End With
shData.Rows("2:" & lastRow).Columns(foundRng.Column).Copy shSummary.Range("I3")
End Sub
Apply a filter to columns A and F then copy the visible cells.
Option Explicit
Sub Find3()
Dim wb As Workbook, wsData As Worksheet, wsSummary As Worksheet
Dim rngFound As Range, rngData As Range, rngCopy As Range
Dim FindValue As String, FilterA As String, FilterF As String
Dim lastRow As Long, c As Long
Set wb = ThisWorkbook
Set wsData = wb.Worksheets("Sheet1")
wsData.AutoFilterMode = False
Set wsSummary = wb.Worksheets("Sheet2")
With wsSummary
FindValue = .Range("B2")
FilterA = .Range("C2")
FilterF = .Range("A2")
End With
Set rngFound = wsData.Range("G1:Z1").find(FindValue)
If rngFound Is Nothing Then
MsgBox "'" & FindValue & "' not found", vbCritical
Exit Sub
End If
' column matching FindValue
c = rngFound.Column
lastRow = wsData.Cells(Rows.Count, c).End(xlUp).Row
If lastRow = 1 Then
MsgBox "No data in column " & c, vbCritical
Exit Sub
End If
' filter data on colA and F
With wsData
Set rngData = .Cells(2, c).Resize(lastRow - 1)
.UsedRange.AutoFilter
.UsedRange.AutoFilter Field:=1, Criteria1:=FilterA
.UsedRange.AutoFilter Field:=6, Criteria1:=FilterF
' data to copy
On Error Resume Next
Set rngCopy = rngData.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
' copy data
If rngCopy Is Nothing Then
MsgBox "No data to copy from column " & c, vbCritical
.AutoFilterMode = False
Exit Sub
Else
rngCopy.Copy wsSummary.Range("I3")
End If
.AutoFilterMode = False
End With
MsgBox "Done"
End Sub
Copy Data Columns to Another Worksheet
Adjust the values in the constants section.
Delete (out-comment) the Debug.Print lines when done testing.
Option Explicit
Sub ExportDataColumns()
Const sName As String = "Sheet1"
Const sHeadersAddress As String = "G1:Z1"
Const dName As String = "Sheet2"
Const dReadList As String = "A2,B2,C2"
Const dWriteList As String = "F3,A3,I3"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim wsrCount As Long: wsrCount = sws.Rows.Count
Dim shrg As Range: Set shrg = sws.Range(sHeadersAddress)
Debug.Print "Source Header Range: " & shrg.Address(0, 0)
Dim sfRow As Long: sfRow = shrg.Row + 1 ' first row below the headers
Debug.Print "Source First Row: " & sfRow
If sfRow >= wsrCount Then Exit Sub
Dim slRow As Long: slRow = GetLastRow(shrg)
Debug.Print "Source Last Row: " & slRow
If slRow < sfRow Then Exit Sub
Dim sdrg As Range
Set sdrg = shrg.Resize(slRow - sfRow + 1).Offset(1)
Debug.Print "Source Data Range: " & sdrg.Address(0, 0)
Dim dRead() As String: dRead = Split(dReadList, ",")
Dim dWrite() As String: dWrite = Split(dWriteList, ",")
Dim dUpper As Long: dUpper = UBound(dRead)
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
Dim srg As Range
Dim drg As Range
Dim dcrg As Range
Dim srCount As Long
Dim n As Long
For n = 0 To dUpper
Debug.Print "Item " & n + 1
Dim scIndex As Variant
scIndex = Application.Match(dws.Range(dRead(n)).Value, shrg, 0)
If IsNumeric(scIndex) Then
Set srg = sdrg.Columns(scIndex)
Debug.Print "Source Range: " & srg.Address(0, 0)
srCount = srg.Rows.Count
Set drg = dws.Range(dWrite(n)).Resize(srCount)
Debug.Print "Destination Range: " & drg.Address(0, 0)
drg.Value = srg.Value
Set dcrg = drg.Resize(wsrCount - drg.Row - srCount + 1) _
.Offset(srCount)
Debug.Print "Destination Clear Range: " & dcrg.Address(0, 0)
dcrg.ClearContents
End If
Next n
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose: Returns the worksheet row number of the last non-empty row
' in the range from the first row of a range ('rg')
' through the same sized bottom-most row of the worksheet.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetLastRow( _
ByVal rg As Range) _
As Long
If rg Is Nothing Then Exit Function
Dim lCell As Range
With rg.Rows(1)
Set lCell = .Resize(.Worksheet.Rows.Count - .Row + 1) _
.Find("*", , xlFormulas, , xlByRows, xlPrevious)
End With
If lCell Is Nothing Then Exit Function
GetLastRow = lCell.Row
End Function

Trying to find range of values in another range and have output in another

Thought it would be as simple as, but somewhere i am wrong please help! So I am trying to find the values from rng1 in rng2 and have the output next to rng1
Thanks in advance
Sub FindValuestest()
Dim wb As Workbook, wks1, wks2 As Worksheet, rng1, rng2 As Range
Dim lRow1, lRow2 As Long
Dim v, n, r As Variant
On Error Resume Next
Set wb = ThisWorkbook
Set wks1 = wb.Worksheets("FEPR")
Set wks2 = wb.Worksheets("EQUIP")
lRow1 = wks1.Cells(wks1.Rows.Count, "B").End(xlUp).Row
lRow2 = wks2.Cells(wks2.Rows.Count, "A").End(xlUp).Row
Set rng1 = wks1.Range("B2", Cells(Rows.Count, "B").End(xlUp))
Set rng2 = wks2.Range("A1", Cells(Rows.Count, "A").End(xlUp))
lRow1 = lRow1 - 1
For v = 1 To lRow1
For n = 1 To lRow1
If n = rng2.Find(n, , xlValues, xlWhole, , , False) And rng2.Cells(n, 2) = "Commodity Tracking Bag Scanner" Then
'Debug.Print n
rng1.Cells(n, 2) = rng1.Cells(n) & " Scanner OK"
End If
Next
Next
For v = 1 To lRow1
For n = 1 To lRow1
If n = rng2.Find(n, , xlValues, xlWhole, , , False) And rng2.Cells(, 2) = "Radio" Then
rng1.Cells(n, 3) = rng1.Cells(n) & " Radio OK"
End If
Next
Next
End Sub
Match Values
Option Explicit
Sub FindValuestest()
' Workbook
Dim wb As Workbook: Set wb = ThisWorkbook
' Source
Dim sws As Worksheet: Set sws = wb.Worksheets("EQUIP")
Dim sLast As Long: sLast = sws.Cells(sws.Rows.Count, "A").End(xlUp).Row
Dim srg As Range: Set srg = sws.Range("A1:A" & sLast)
' Destination
Dim dws As Worksheet: Set dws = wb.Worksheets("FEPR")
Dim dLast As Long: dLast = dws.Cells(dws.Rows.Count, "B").End(xlUp).Row
Dim drg As Range: Set drg = dws.Range("B2:B" & dLast)
' Additional Variables
Dim cIndex As Variant
Dim i As Long
' Write
Application.ScreenUpdating = False
For i = 1 To dLast
cIndex = Application.Match(drg.Cells(i).Value, srg, 0)
If IsNumeric(cIndex) Then
If srg.Cells(cIndex).Offset(, 1) _
= "Commodity Tracking Bag Scanner" Then
drg.Cells(i).Offset(, 1).Value = drg.Cells(i) & " Scanner OK"
ElseIf srg.Cells(cIndex).Offset(, 1) = "Radio" Then
drg.Cells(i).Offset(, 2).Value = drg.Cells(i) & " Radio OK"
End If
End If
Next i
Application.ScreenUpdating = True
End Sub

Resources