If Hyperlink is Broken, Copy Entire Row To Destination Sheet - excel

I want to copy entire row to destination sheet if either: 1) there are no hyperlinks in the row, or 2) the hyperlinks in the row are all broken links (e.g., they return an error when accessed).
Sub Find_Value()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim rFind As Range
Dim i As Long
Set sh1 = Sheets("data")
Set sh2 = Sheets("copy")
For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
If Cells(i, 1).Hyperlinks.Count = 0 Then
Cells(i, "A").EntireRow.Copy Destination:=sh2.Cells(i, "A").End(xlUp).Offset(1)
Else
End If
Next i
End Sub
I can do it for if there are no hyperlinks (shown in my code above), but how can I check if the links are broken?

Check If No Hyperlink Or Link Is Broken
Option Explicit
Sub FindValue()
Dim sws As Worksheet: Set sws = ThisWorkbook.Worksheets("data")
Dim srg As Range
Set srg = sws.Range("A1", sws.Cells(sws.Rows.Count, "A").End(xlUp))
Dim dws As Worksheet: Set dws = ThisWorkbook.Worksheets("copy")
Dim dCell As Range: Set dCell = dws.Cells(dws.Rows.Count, "A").End(xlUp)
Dim sCell As Range
Dim rCount As Long
Dim Invalid As Boolean
For Each sCell In srg.Cells
If sCell.Hyperlinks.Count = 0 Then
Invalid = True
Else
If IsLinkBroken(sCell.Hyperlinks(1).Address) Then Invalid = True
End If
If Invalid Then
Set dCell = dCell.Offset(1)
sCell.EntireRow.Copy Destination:=dCell
rCount = rCount + 1
Invalid = False
End If
Next sCell
MsgBox "Rows copied: " & rCount, vbInformation
End Sub
Function IsLinkBroken(ByVal url As String) As Boolean
On Error GoTo ClearError
With CreateObject("MSXML2.ServerXMLHTTP")
.Open "HEAD", url, False
.send
If .Status = 200 Then Exit Function
End With
ProcExit:
IsLinkBroken = True
Exit Function
ClearError:
Resume ProcExit
End Function
Compact
Not quite sure if this may be much faster (or even correct).
Sub FindValueCompact()
Dim sws As Worksheet: Set sws = ThisWorkbook.Worksheets("data")
Dim srg As Range
Set srg = sws.Range("A1", sws.Cells(sws.Rows.Count, "A").End(xlUp))
Dim dws As Worksheet: Set dws = ThisWorkbook.Worksheets("copy")
Dim dCell As Range: Set dCell = dws.Cells(dws.Rows.Count, "A").End(xlUp)
Dim xhr As Object: Set xhr = CreateObject("MSXML2.ServerXMLHTTP")
Dim sCell As Range
Dim rCount As Long
Dim ErrNum As Long
Dim Invalid As Boolean
For Each sCell In srg.Cells
If sCell.Hyperlinks.Count = 0 Then
Invalid = True
Else
xhr.Open "HEAD", sCell.Hyperlinks(1).Address, False
On Error Resume Next
xhr.send
ErrNum = Err.Number
On Error GoTo 0
If ErrNum = 0 Then
If xhr.Status <> 200 Then Invalid = True
Else
Invalid = True
ErrNum = 0
End If
End If
If Invalid Then
Set dCell = dCell.Offset(1)
sCell.EntireRow.Copy Destination:=dCell
rCount = rCount + 1
Invalid = False
End If
Next sCell
MsgBox "Rows copied: " & rCount, vbInformation
End Sub

Related

Find from InputBox, copy row of found cell without using .Select

I have a spreadsheet with over 10000 rows. I need to search it using InputBox (UPC field, input is from a barcode scanner).
I need to copy the row of the found cell, and paste it to another sheet.
This process should loop until the user cancels the InputBox.
I have done this, but it gives me an error on the SelectCells.Select line, but not every time.
Sub Scan()
Do Until IsEmpty(ActiveCell)
Dim Barcode As Double
Barcode = InputBox("Scan Barcode")
Dim ws As Worksheet
Dim SelectCells As Range
Dim xcell As Object
Set ws = Worksheets("Sheet1")
For Each xcell In ws.UsedRange.Cells
If xcell.Value = Barcode Then
If SelectCells Is Nothing Then
Set SelectCells = Range(xcell.Address)
Else
Set SelectCells = Union(SelectCells, Range(xcell.Address))
End If
End If
Next
SelectCells.Select
Set SelectCells = Nothing
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Loop
End Sub
Copy Rows
Option Explicit
Sub Scan()
Const sName As String = "Sheet1"
Const Header As String = "Barcode"
Const dName As String = "Sheet2"
Const dFirstCellAddress As String = "A2"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim surg As Range: Set surg = sws.UsedRange
Dim slCell As Range
Set slCell = surg.Cells(surg.Rows.Count, surg.Columns.Count)
Dim shCell As Range
Set shCell = surg.Find(Header, slCell, xlFormulas, xlWhole, xlByRows)
If shCell Is Nothing Then
MsgBox "The cell containing the header '" & Header _
& "' was not found.", vbCritical
Exit Sub
End If
Dim sfCol As Long: sfCol = surg.Column
Dim srg As Range
Set srg = sws.Range(sws.Cells(shCell.Row + 1, sfCol), slCell)
Dim scColIndex As Long: scColIndex = shCell.Column - sfCol + 1
Dim scrg As Range: Set scrg = srg.Columns(scColIndex)
Dim SelectedRows As Range
Dim Barcode As Variant
Dim srIndex As Variant
Do
Barcode = InputBox("Scan Barcode")
If Len(CStr(Barcode)) = 0 Then Exit Do
If IsNumeric(Barcode) Then
srIndex = Application.Match(CDbl(Barcode), scrg, 0)
If IsNumeric(srIndex) Then
If SelectedRows Is Nothing Then
Set SelectedRows = srg.Rows(srIndex)
Else
Set SelectedRows = Union(SelectedRows, srg.Rows(srIndex))
End If
End If
End If
Loop
If SelectedRows Is Nothing Then
MsgBox "No scan results.", vbExclamation
Exit Sub
End If
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
Dim dfCell As Range: Set dfCell = dws.Range(dFirstCellAddress)
Dim durg As Range: Set durg = dws.UsedRange
Dim dlRow As Long: dlRow = durg.Row + durg.Rows.Count - 1
Dim dlCell As Range
If dlRow < dfCell.Row Then
Set dlCell = dfCell
Else
Set dlCell = dws.Cells(dlRow + 1, dfCell.Column)
End If
SelectedRows.Copy dlCell
MsgBox "Rows copied.", vbInformation
End Sub
You can try something like this:
Sub Scan()
Dim Barcode As String, rngData As Range, m, rngDest As Range
'Column with barcodes
With Worksheets("Sheet1")
Set rngData = .Range("D1", .Cells(Rows.Count, "D").End(xlUp))
End With
'First paste postion
Set rngDest = Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1)
Do
Barcode = InputBox("Scan Barcode")
If Len(Barcode) = 0 Then Exit Do
'm = Application.Match(Barcode, rngData, 0) 'Barcodes formatted as text
m = Application.Match(CDbl(Barcode), rngData, 0) 'Barcodes formatted as numbers
If Not IsError(m) Then
rngData.Rows(m).EntireRow.Copy rngDest 'copy to Sheet2
Set rngDest = rngDest.Offset(1)
Else
'if no match then what?
Debug.Print "no match"
End If
Loop
End Sub
Depending on how your barcodes are stored (as text, or a numeric values) you may need to use CDbl(Barcode) inside the call to Match()

Error showing Run-time error '424' for index match vba

I would like to have a index match vba to be executed for one cell (C14) whereby the lookup cell would be C15. Please help advise where did the code went wrong?
Source data would be export workbook sheet1.
Sub Index_Match()
Dim custName As Range 'sourceRange
Dim BRN As Range 'lookupRange
Dim ws As Worksheet 'current sheet
Dim exportWb As Workbook
Dim exportWs As Worksheet
Set ws = Sheet1
Set exportWb = Workbooks.Open("C:\Users\hrhquek\desktop\export.xlsx")
Set exportWs = exportWb.Worksheets("Sheet1")
Set exportWb = ActiveWorkbook
ThisWorkbook.Activate
Set custName = exportWs.Cells(exportWs.Rows.Count, "B").End(xlUp).Row
Set BRN = exportWs.Cells(exportWs.Rows.Count, "E").End(xlUp).Row
ws.Cells(3, 14).Value = Application.WorksheetFunction.Index(custName,
Application.WorksheetFunction.Match(Cells(3, 15), BRN, 0))
End Sub
A VBA Lookup: INDEX/MATCH in VBA
Sub VBALookup()
' Source
Dim swb As Workbook
Set swb = Workbooks.Open("C:\Users\hrhquek\desktop\export.xlsx")
Dim sws As Worksheet: Set sws = swb.Worksheets("Sheet1")
Dim slRow As Long: sws.Cells(sws.Rows.Count, "E").End(xlUp).Row
Dim slrg As Range: Set slrg = sws.Range("E2:E" & slRow)
Dim svrg As Range: Set svrg = sws.Range("B2:B" & slRow)
' Destination
Dim dws As Worksheet: Set dws = Sheet1 ' code name in 'ThisWorkbook'
Dim dlCell As Range: Set dlCell = dws.Range("O3")
Dim dvCell As Range: Set dvCell = dws.Range("N3")
Dim dValue As Variant: dValue = dlCell.Value
' Attempt to find a match.
Dim MatchFound As Boolean
If Not IsError(dValue) Then
If Len(CStr(dValue)) > 0 Then
Dim sIndex As Variant: sIndex = Application.Match(dValue, slrg, 0)
If IsNumeric(sIndex) Then MatchFound = True
End If
End If
' Write result.
If MatchFound Then
dvCell.Value = svrg.Cells(sIndex).Value
MsgBox "Match found.", vbInformation
Else
dvCell.Value = Empty
MsgBox "No match found.", vbExclamation
End If
End Sub

VBA to create sheets based on a list

I would like to automatically create sheets based on a list in sheet "Clients". This sheet has the names of clients (starting from cell A2) and the VBA code is reading this list and creates a sheet per cell value.
I found some code on this forum but it throws a 'Run-time error 450: Wrong number of arguments or invalid property assignment' on row 9 (Set MyRange2 = .Range(MyRange, .Rows.Count, "A").End(xlUp)). I'm not a VBA developer so searching for this error didn't really mean a lot to me. What could be wrong with this code?
Sub insertSheets()
Dim myCell As Range
Dim MyRange As Range
Dim MyRange2 As Range
With Sheets("Clients")
Set MyRange = .Range("A2")
Set MyRange2 = .Range(MyRange, .Rows.Count, "A").End(xlUp)
End With
For Each myCell In MyRange2
If Not myCell.Value = vbNullString Then
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = myCell.Value
End If
Next myCell
End Sub
Thanks for the help
Add Worksheets From a List
The Mistake
Set MyRange2 = .Range(MyRange, .Cells(.Rows.Count, "A").End(xlUp))
' or (no need for 'Set MyRange = .Range("A2")'):
'Set MyRange2 = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
An Improvement
Option Explicit
Sub InsertSheets()
Dim sws As Worksheet: Set sws = ThisWorkbook.Worksheets("Clients")
Dim srg As Range
Set srg = sws.Range("A2", sws.Cells(sws.Rows.Count, "A").End(xlUp))
Dim sCell As Range
Dim sValue As Variant
Dim dws As Worksheet
Dim wsCount As Long
Dim ErrNum As Long
For Each sCell In srg.Cells
sValue = sCell.Value
If Not IsError(sValue) Then ' ignore error values
sValue = CStr(sValue)
If Len(sValue) > 0 Then ' ignore blanks
On Error Resume Next
Set dws = ThisWorkbook.Worksheets(sValue)
On Error GoTo 0
If dws Is Nothing Then
Set dws = ThisWorkbook.Worksheets.Add( _
After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
On Error Resume Next
dws.Name = sValue
ErrNum = Err.Number
On Error GoTo 0
If ErrNum = 0 Then ' valid name
wsCount = wsCount + 1
Else ' invalid name; delete the worksheet
Application.DisplayAlerts = False
dws.Delete
Application.DisplayAlerts = True
End If
' Else ' worksheet already exists; do nothing
End If
Set dws = Nothing
' Else ' is blank; do nothing
End If
' Else ' is error value; do nothing
End If
Next sCell
MsgBox "Worksheets created: " & wsCount
End Sub

How to apply vlookup only for empty cells using vba and another workbook

I want to apply vlookup only on the blank cells through VBA. I am using the below code, but it gives me a Run-time error 13 "Type mismatch".When I run the code step by step via F8, I also get an error 2042 at position If i = "" Then, which also indicates "#N/A".
Dim FileName3 As String
FileName3 = "C:xxxxxx.xlsx"
Dim wb As Workbook: Set wb = ThisWorkbook
Dim lastrow As Long
Dim ws As Worksheet: Set ws = wb.Sheets("Data")
lastrow = ws.cells(Rows.Count, 1).End(xlUp).Row
Dim wb2 As Workbook: Set wb2 = Workbooks.Open(Filename:=FileName3, ReadOnly:=True)
Dim rng As Range: Set rng = ThisWorkbook.Worksheets("Data").Range("S2" & lastrow)
Dim lookupRange As Range: Set lookupRange = wb2.Sheets("Page 1").Range("B2" & lastrow)
Dim i As Range
For Each i In rng
If i = "" Then
i = Application.VLookup(ThisWorkbook.Sheets("Data").Range("A" & i.Row), lookupRange, 2, False)
'MsgBox c.Row
End If
'
Next i
'/////// paste by value
Sheets("Data").Columns(52).Copy
Sheets("Data ").Columns(52).PasteSpecialxlPasteValues
wb2.Close False
ThisWorkbook.Save
I had tried it before with WorksheetFunction.VlookUp, but the same error comes up.
The VlookUp should be executed in the datasheet ("Data") in column "S" for all empty cells.
The LookUp Values are located in another workbook file. I would appreciate it very much if someone could help me.
VBA VLookup For Blank Cells
Option Explicit
Sub VLookupBlanks()
Const sFilePath As String = "C:\xxxxxx.xlsx"
Application.ScreenUpdating = False
Dim swb As Workbook
Set swb = Workbooks.Open(Filename:=sFilePath, ReadOnly:=True)
Dim sws As Worksheet: Set sws = swb.Worksheets("Page 1")
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, "B").End(xlUp).Row
Dim srg As Range: Set srg = sws.Range("B2:C" & slRow)
Dim dwb As Workbook: Set dwb = ThisWorkbook
Dim dws As Worksheet: Set dws = dwb.Worksheets("Data")
Dim dlRow As Long: dlRow = dws.Cells(dws.Rows.Count, "A").End(xlUp).Row
Dim drg As Range: Set drg = dws.Range("S2:S" & dlRow)
Dim dCell As Range
Dim dValue As Variant
For Each dCell In drg.Cells
If Len(CStr(dCell.Value)) = 0 Then
dValue = Application.VLookup( _
dCell.EntireRow.Columns("A").Value, srg, 2, False)
If Not IsError(dValue) Then dCell.Value = dValue
End If
Next dCell
swb.Close SaveChanges:=False
With drg.EntireRow.Columns("AZ")
.Value = .Value
End With
dwb.Save
Application.ScreenUpdating = True
MsgBox "Columns updated.", vbInformation
End Sub
Please, try removing of:
Dim rng As Range: Set rng = ThisWorkbook.Worksheets("Data").Range("S2" & lastrow)
Dim lookupRange As Range: Set lookupRange = wb2.Sheets("Page 1").Range("B2" & lastrow)
Dim i As Range
For Each i In rng
If i = "" Then
i = Application.VLookup(ThisWorkbook.Sheets("Data").Range("A" & i.Row), lookupRange, 2, False)
'MsgBox c.Row
End If
'
Next i
with:
Dim rngV As Range
Dim rng As Range: Set rng = ws.Range("S2:S" & lastrow)
Dim lookupRange As Range: Set lookupRange = wb2.Sheets("Page 1").Range("B2:C" & lastrow)
On Error Resume Next 'only to avoid an error if no any empty cell exists in rng
Set rngV = rng.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If rngV Is Nothing Then Exit Sub 'no any empty cell...
rngV.Formula = "=Vlookup(A" & rngV.cells(1).row & ", " & lookupRange.Address(external:=True) & ", 2, False)"

Search for cell that contains text and paste value in next blank cell

Im new to VBA and am struggling a bit. I need to search column Q on sheet 2 for the cell that contains “text”, then copy the data in the cell to the right, then paste this value in the next blank cell in column B on sheet1. I have been trying to do this using IF THEN code but keep getting errors. It seems simple but am struggling, can anyone advise?
I need the results to post next to week 4, when using .end(XLup) the code runs but posts the results under the 46. When switching to XLdown to run from the top I get an error.
Sub question68784119()
Const SED As String = "tokyo" 'the text you're searching for"
Dim aCell As Range, wsPull As Worksheet, theCellValue As Variant, wsPaste As Worksheet
Set wsPull = ThisWorkbook.Sheets("flavors_of_cacao")
Set wsPaste = ThisWorkbook.Sheets("Sheet1")
For Each aCell In Intersect(wsPull.UsedRange, wsPull.Range("F:G")).Cells
theCellValue = aCell.Value2
If InStr(1, theCellValue, SED, vbTextCompare) > 0 Then
theCellValue = aCell.Offset(0, 1).Value
wsPaste.Cells(Rows.Count, 5).End(xlDown).Offset(1, 0).Value = theCellValue
End If
Next aCell
MsgBox "Done!"
End Sub
A VBA Lookup
Range.End property
Range.Find method
Optionally, a formula, instead of the value, can be written to the destination cell (each 3rd solution).
Readable:
Sub VBALookupFind()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet2")
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, "Q").End(xlUp).Row
If slRow < 2 Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(2, "Q"), sws.Cells(slRow, "Q"))
Dim slCell As Range
Set slCell = slrg.Find("text", slrg.Cells(slrg.Cells.Count), _
xlFormulas, xlWhole)
If slCell Is Nothing Then Exit Sub ' no match
Dim svCell As Range: Set svCell = slCell.EntireRow.Columns("R")
Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet1")
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, "B").End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, "B")
dCell.Value = svCell.Value
End Sub
Sub VBALookupMatch()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet2")
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, "Q").End(xlUp).Row
If slRow < 2 Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(2, "Q"), sws.Cells(slRow, "Q"))
Dim rIndex As Variant: rIndex = Application.Match("text", slrg, 0)
If IsError(rIndex) Then Exit Sub ' no match
Dim svrg As Range: Set svrg = slrg.EntireRow.Columns("R")
Dim svCell As Range: Set svCell = svrg.Cells(rIndex)
Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet1")
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, "B").End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, "B")
dCell.Value = svCell.Value
End Sub
Sub VBALookupFormula()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet2")
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, "Q").End(xlUp).Row
If slRow < 2 Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(2, "Q"), sws.Cells(slRow, "Q"))
Dim svrg As Range: Set svrg = slrg.EntireRow.Columns("R")
Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet1")
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, "B").End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, "B")
Dim dFormula As String
dFormula = "=IFERROR(INDEX('" & "Sheet2" _
& "'!" & svrg.Address(, 0) _
& "," & "MATCH(""" & "text" _
& """,'" & "Sheet2" _
& "'!" & slrg.Address(, 0) _
& ",0)),"""")"
dCell.Formula = dFormula
End Sub
Maintainable, Using Constants (Study With Debug.Print)
Sub VBALookupFindConstants()
Const sName As String = "Sheet2" ' Source Worksheet Name
Const sfRow As Long = 2 ' Source First Row
Const slCol As String = "Q" ' Source Lookup Column
Const slValue As String = "text" ' Source Lookup Value
Const svCol As String = "R" ' Source Value Column
Const dName As String = "Sheet1" ' Destination Worksheet Name
Const dCol As String = "B" ' Destination Column
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, slCol).End(xlUp).Row
If slRow < sfRow Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(sfRow, slCol), sws.Cells(slRow, slCol))
Debug.Print "Source Lookup Range Address " & slrg.Address(, 0)
Dim slCell As Range
Set slCell = slrg.Find(slValue, slrg.Cells(slrg.Cells.Count), _
xlFormulas, xlWhole)
If slCell Is Nothing Then Exit Sub ' no match
Debug.Print "Source Lookup Cell Address " & slCell.Address(0, 0)
Dim svCell As Range: Set svCell = slCell.EntireRow.Columns(svCol)
Debug.Print "Source Value Cell Address " & svCell.Address(0, 0)
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, dCol).End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, dCol)
Debug.Print "Destination Cell Address " & dCell.Address(0, 0)
' Value...
dCell.Value = svCell.Value
Debug.Print "Destination Cell Value " & dCell.Value
End Sub
Sub VBALookupMatchConstants()
Const sName As String = "Sheet2" ' Source Worksheet Name
Const sfRow As Long = 2 ' Source First Row
Const slCol As String = "Q" ' Source Lookup Column
Const slValue As String = "text" ' Source Lookup Value
Const svCol As String = "R" ' Source Value Column
Const dName As String = "Sheet1" ' Destination Worksheet Name
Const dCol As String = "B" ' Destination Column
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, slCol).End(xlUp).Row
If slRow < sfRow Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(sfRow, slCol), sws.Cells(slRow, slCol))
Debug.Print "Source Lookup Range Address " & slrg.Address(, 0)
Dim rIndex As Variant: rIndex = Application.Match(slValue, slrg, 0)
If IsError(rIndex) Then Exit Sub ' no match
Debug.Print "Match Index " & rIndex
Dim svrg As Range: Set svrg = slrg.EntireRow.Columns(svCol)
Debug.Print "Source Value Range Address " & svrg.Address(, 0)
Dim svCell As Range: Set svCell = svrg.Cells(rIndex)
Debug.Print "Source Value Cell Address " & svCell.Address(0, 0)
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, dCol).End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, dCol)
Debug.Print "Destination Cell Address " & dCell.Address(0, 0)
' Value...
dCell.Value = svCell.Value
Debug.Print "Destination Cell Value " & dCell.Value
End Sub
Sub VBALookupFormulaConstants()
Const sName As String = "Sheet2" ' Source Worksheet Name
Const sfRow As Long = 2 ' Source First Row
Const slCol As String = "Q" ' Source Lookup Column
Const slValue As String = "text" ' Source Lookup Value
Const svCol As String = "R" ' Source Value Column
Const dName As String = "Sheet1" ' Destination Worksheet Name
Const dCol As String = "B" ' Destination Column
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, slCol).End(xlUp).Row
If slRow < sfRow Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(sfRow, slCol), sws.Cells(slRow, slCol))
Debug.Print "Source Lookup Range Address " & slrg.Address(, 0)
Dim svrg As Range: Set svrg = slrg.EntireRow.Columns(svCol)
Debug.Print "Source Value Range Address " & svrg.Address(, 0)
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, dCol).End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, dCol)
Debug.Print "Destination Cell Address " & dCell.Address(0, 0)
Dim dFormula As String
dFormula = "=IFERROR(INDEX('" & sName _
& "'!" & svrg.Address(, 0) _
& "," & "MATCH(""" & slValue _
& """,'" & sName _
& "'!" & slrg.Address(, 0) _
& ",0)),"""")"
Debug.Print "Destination Cell Formula " & dFormula
dCell.Formula = dFormula
Debug.Print "Destination Cell Value " & dCell.Value
End Sub
You should post whatever attempts you made in your question, but this will do what you specified.
Sub question68784119()
Const tangoText As String = "text" 'the text you're searching for
Dim aCell As Range, wsPull As Worksheet, theCellValue As String, wsPaste As Worksheet
'make sure these are exactly the same as your workbook. Case sensative
Set wsPull = ThisWorkbook.Sheets("sheet 2")
Set wsPaste = ThisWorkbook.Sheets("sheet1")
For Each aCell In Intersect(wsPull.UsedRange, wsPull.Range("Q:Q")).Cells
theCellValue = aCell.Value2
If InStr(1, theCellValue, tangoText, vbTextCompare) > 0 Then
aCell.Offset(0, 1).Value2 = theCellValue
wsPaste.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Value = theCellValue
End If
Next aCell
End Sub

Resources