I have "object doesn't support this method" error when trying to do a comparison of column cross sheets. . If both column A and B in sheet 1 matches both column A and B in sheet 2, it will display the match in sheet 3.
There were a number of issues in your code. In the future, please post the actual code (not screenshot).
Watch out for "And" vs "&" in your if statement.
".value" not ".values" in your vars.
"Dim as string" since we're working with the cell.
"Worksheet." not "Worksheets."
I don't think you need a "Set" for these. (could be wrong)
Try the code below, it works for me. You may need to modify the lines writing to the "match" sheet.
Sub find()
Dim a As String
Dim b As String
Dim c As String
Dim d As String
a = Worksheets("sheet1").Range("a1").Value
b = Worksheets("sheet2").Range("a1").Value
c = Worksheets("sheet1").Range("b1").Value
d = Worksheets("sheet2").Range("b1").Value
If a = b And c = d Then
Worksheets("match").Range("A65536").End(xlUp).Offset(1, 0).Value = c
Worksheets("match").Range("A65536").End(xlUp).Offset(0, 1).Value = c
End If
End Sub
Related
I have an excel sheet table with the following data:
In VBA how to search and match for both values in columns A and B and return row value in column C.
Example:
I need to search for the exact match of c+c1 and have as result yy
Many thanks for the help
Use If statements when you loop through the rows like so:
If ws.cells(i,1).Value = c And ws.cells(i,2).value = c1 Then
result = yy
End If
Hope it helps!
U can create a 4th column, this column will be your key column to use in VBA. In this column you will concatenate the A and B values, after that we create a code that search the concat and return the 4th cell on the right.
.
Sub Example()
Dim keyRange As Range
Set keyRange = Planilha1.Range("A2:A8")
Dim SearchValue1, SearchValue2 As String
SearchValue1 = "a"
SearchValue2 = "a2"
Dim lin As Integer
lin = Application.WorksheetFunction.Match(SearchValue1 & SearchValue2, keyRange, 0)
Dim answer As String
answer = Planilha1.Range("A2:D8").Cells(lin, 4)
Debug.Print answer
End Sub
I have a bunch of rows and 25 columns in a worksheet, and need to find the value in the 4th column based on columns B and C using VBA. I am using a combination of index and multiple condition match functions.
I tried to follow along via https://www.mrexcel.com/forum/showthread.php?650832-VBA-Multiple-Criteria-Index-Match and pass an integer variable into vba array formula to no avail.
I made this macro which works:
Sub VariablesInArrayFormula()
SA = "Apples"
C1 = "Oranges"
Range("D27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27=b4)*(C2:C27= c6),0),4)"
Range("E27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27=""Apples"")*(C2:C27= ""Oranges""),0),4)"
f = Evaluate("index(A2:G27,match(1,(B2:B27=""Apples"")*(C2:C27= ""Oranges""),0),4)")
Range("G27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27="" & SA & "")*(C2:C27= "" C1 ""),0),4)"
End Sub
I want to assign the value to a variable for future use.
When I assign it to D27, it works because the I refer to cell references b4 and c6.
Assigning it to cell E27 also works, but then I need to refer directly to Apples and Oranges, where as I would prefer to pass in a variables
assigning it to a variable f works when I pass in the words Apples and Oranges
when I attempt to pass a reference to Apples and Oranges (SA and C1 respectively), I receive a #N/A error.
Can anyone suggest a way that I can pass in the variables to this function.
NB I tried using worksheetfunction.index and worksheetfunction.match and kept receiving errors as well. Specifically, I tried:
gr4 = WorksheetFunction.Index(Range("A2:G27"), WorksheetFunction.Match(1, ((Range("B2:B27") = SA) * (Range("C2:C27") = C1)), 0), 4)
which returned a run time error #13: type mismatch.
Odd that using 2 matches failed, as when I use a single column to check with the match function worked
Sub vfhj()
SA = "Apples"
C1 = "Oranges"
gr3 = WorksheetFunction.Index(Range("C2:C27"), WorksheetFunction.Match(C1, Range("C2:C27"), 0))
End Sub
If I understand correctly, I think your syntax was just off slightly - you omitted some ampersands and overlooked the rule about doubling up the quotes. Also no need to Select.
Range("G27").FormulaArray = "=index(A2:G27,match(1,(B2:B27=""" & SA & """)*(C2:C27=""" & C1 & """),0),4)"
May be a bit quicker using memory arrays:
Sub VariablesInArrayFormula()
Dim SA As String
SA = "Apples"
Dim C1 As String
C1 = "Oranges"
With Worksheets("Sheet1") 'Change to your worksheet
Dim DtaArray As Variant
DtaArray = .Range("B2:D27").Value
Dim i As Long
For i = LBound(DtaArray, 1) To UBound(DtaArray, 1)
Dim ans
If DtaArray(i, 1) = SA And dtaaray(i, 2) = C1 Then
ans = DtaArray(i, 3)
Exit For
End If
Next i
.Range("G1").Value = ans
End With
End Sub
I have a bunch of rows and 25 columns in a worksheet, and need to find the value in the 4th column based on columns B and C using VBA. I am using a combination of index and multiple condition match functions.
I tried to follow along via https://www.mrexcel.com/forum/showthread.php?650832-VBA-Multiple-Criteria-Index-Match and pass an integer variable into vba array formula to no avail.
I made this macro which works:
Sub VariablesInArrayFormula()
SA = "Apples"
C1 = "Oranges"
Range("D27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27=b4)*(C2:C27= c6),0),4)"
Range("E27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27=""Apples"")*(C2:C27= ""Oranges""),0),4)"
f = Evaluate("index(A2:G27,match(1,(B2:B27=""Apples"")*(C2:C27= ""Oranges""),0),4)")
Range("G27").Select
Selection.FormulaArray = "=index(A2:G27,match(1,(B2:B27="" & SA & "")*(C2:C27= "" C1 ""),0),4)"
End Sub
I want to assign the value to a variable for future use.
When I assign it to D27, it works because the I refer to cell references b4 and c6.
Assigning it to cell E27 also works, but then I need to refer directly to Apples and Oranges, where as I would prefer to pass in a variables
assigning it to a variable f works when I pass in the words Apples and Oranges
when I attempt to pass a reference to Apples and Oranges (SA and C1 respectively), I receive a #N/A error.
Can anyone suggest a way that I can pass in the variables to this function.
NB I tried using worksheetfunction.index and worksheetfunction.match and kept receiving errors as well. Specifically, I tried:
gr4 = WorksheetFunction.Index(Range("A2:G27"), WorksheetFunction.Match(1, ((Range("B2:B27") = SA) * (Range("C2:C27") = C1)), 0), 4)
which returned a run time error #13: type mismatch.
Odd that using 2 matches failed, as when I use a single column to check with the match function worked
Sub vfhj()
SA = "Apples"
C1 = "Oranges"
gr3 = WorksheetFunction.Index(Range("C2:C27"), WorksheetFunction.Match(C1, Range("C2:C27"), 0))
End Sub
If I understand correctly, I think your syntax was just off slightly - you omitted some ampersands and overlooked the rule about doubling up the quotes. Also no need to Select.
Range("G27").FormulaArray = "=index(A2:G27,match(1,(B2:B27=""" & SA & """)*(C2:C27=""" & C1 & """),0),4)"
May be a bit quicker using memory arrays:
Sub VariablesInArrayFormula()
Dim SA As String
SA = "Apples"
Dim C1 As String
C1 = "Oranges"
With Worksheets("Sheet1") 'Change to your worksheet
Dim DtaArray As Variant
DtaArray = .Range("B2:D27").Value
Dim i As Long
For i = LBound(DtaArray, 1) To UBound(DtaArray, 1)
Dim ans
If DtaArray(i, 1) = SA And dtaaray(i, 2) = C1 Then
ans = DtaArray(i, 3)
Exit For
End If
Next i
.Range("G1").Value = ans
End With
End Sub
I have 2 sheets: sheet1 and sheet2. I have a value in cell A3 (sheet1) which is not constant. And many files in sheets2.
What I would like to do, is when the value in cell A3 (Sheet1) is the same as the value in the column A (Sheet2), it will delete the entire row where is find this value (Sheet2).
This is my attempt. It doesn't work: no rows are deleted.
If Worksheets("Sheet1").Range("A3").Text = Worksheets("Sheet2").Range("A:A").Text Then
Dim f As String
f = Worksheets("Sheet1").Range("A3")
Set c = Worksheets("Sheet2").Range("A:A").Find(f)
Worksheets("Sheet2").Range(c.Address()).EntireRow.Delete
End If
My guess is that you're not finding anything with the .Find(). Since you're not checking it for is Nothing you don't know. Also, .Find() retains all the search parameters set from the last time you did a search - either via code or by hand in your spreadsheet. While only the What parameter is required, it's always worth setting the most critical parameters (noted below) for it, you may want to set them all to ensure you know exactly how you're searching.
Dim f As String
If Worksheets("Sheet1").Range("A3").Text = Worksheets("Sheet2").Range("A:A").Text Then
f = Worksheets("Sheet1").Range("A3")
Set c = Worksheets("Sheet2").Range("A:A").Find(What:=f, Match:=[Part|Whole], _
LookIn:=[Formula|value])
if not c is Nothing then
Worksheets("Sheet2").Range(c.Address()).EntireRow.Delete
else
MsgBox("Nothing found")
End If
End If
Go look at the MS docs to see what all the parameters and their enumerations are.
Sub Test()
Dim ws As Worksheet
For x = 1 To Rows.Count
If ThisWorkbook.Sheets("Sheet2").Cells(x, 1).Value = ThisWorkbook.Sheets("Sheet1").Cells(3, 1).Value Then ThisWorkbook.Sheets("Sheet2").Cells(x, 1).EntireRow.Delete
Next x
End Sub
I'm trying to pull all hyperlinks in an excel spreadsheet into a new worksheet. I want column A to show the text from the hyperlink, and column B to show the hyperlink address.
I've written the code below, and all of column B works fine, however the values in column A are not all coming over, and they don't match the hyperlink addresses in column B. What am I doing wrong?
Thanks in advance
Sub extract_links()
Dim hyp As Hyperlink
Dim ReadCols As Long
Dim ReadWriteRow As Long
ReadWriteRow = 1
ReadCols = 6
ActiveWorkbook.Sheets(2).Range("a:b").Clear
For c = 1 To ReadCols
For Each hyp In ActiveWorkbook.Sheets(1).Columns(c).Hyperlinks
ActiveWorkbook.Sheets(2).Range("a" & ReadWriteRow).Value = ActiveWorkbook.Sheets(1).Cells(ReadWriteRow, c).Value
ActiveWorkbook.Sheets(2).Range("b" & ReadWriteRow).Value = hyp.Address
ReadWriteRow = ReadWriteRow + 1
Next
Next c
End Sub
This time you need to change this:
ActiveWorkbook.Sheets(2).Range("a" & ReadWriteRow).Value = ActiveWorkbook.Sheets(1).Cells(ReadWriteRow, c).Value
into this:
ActiveWorkbook.Sheets(2).Range("a" & ReadWriteRow).Value = hyp.Range.Value