Hello,
I have a search functionality in an Excel workbook which searches according to a cell value from worksheet, "Search students", searches within worksheet, "2017-18 student list", then copies and pastes the find results in a third worksheet, "Search Results".
My problem concerns the copying. I'm trying to copy multiple ranges within a row, rather than just one range or the entire row. My code below successfully copies results from one range, columns 1-38.
However, this includes some columns I need to exclude. Instead, I want to copy columns 1-7, 10-17, 19-24, 29-30, 37-38. (Ranges "A:G, J:Q, S:X, AC:AD, AK:AL")
This is my code:
I have tried using union but I can't get it to work while using the cells property method. I get a run-time error '13' type mismatch error. (Possibly because the cells property needs r to be an integer not a range?) Here is my code attempt using union:
Any help to get this working would be greatly appreciated!
I have a search functionality in an Excel workbook which searches according to a cell value from worksheet, "Search students", searches within worksheet, "2017-18 student list", then copies and pastes the find results in a third worksheet, "Search Results".
My problem concerns the copying. I'm trying to copy multiple ranges within a row, rather than just one range or the entire row. My code below successfully copies results from one range, columns 1-38.
Code:
.Range(.Cells(i, 1), .Cells(i, 38)).Copy
However, this includes some columns I need to exclude. Instead, I want to copy columns 1-7, 10-17, 19-24, 29-30, 37-38. (Ranges "A:G, J:Q, S:X, AC:AD, AK:AL")
This is my code:
Code:
Public Sub Faculty_only()
Dim Findfaculty As String
Dim ws As Worksheet
Findfaculty = Sheets("Search Students").Range("K12").Value
Set ws = ThisWorkbook.Sheets("2017-18 student list")
Finalrow = Sheets("2017-18 student list").Range("A902").End(xlUp).Row
With ws
For i = 7 To Finalrow
If .Cells(i, 1).Value = Findfaculty Then
.Range(.Cells(i, 1), .Cells(i, 38)).Copy
Sheets("Search Results").Range("A901").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Next i
Sheets("Search Results").Select
Range("A1").Select
End With
End Sub
I have tried using union but I can't get it to work while using the cells property method. I get a run-time error '13' type mismatch error. (Possibly because the cells property needs r to be an integer not a range?) Here is my code attempt using union:
Code:
Public Sub Faculty_only()
Dim Findfaculty As String
Dim ws As Worksheet
Dim range1 As Range
Dim range2 As Range
Dim range3 As Range
Dim range4 As Range
Dim range5 As Range
Dim r As Range
Findfaculty = Sheets("Search Students").Range("K12").Value
Set range1 = Sheets("2017-18 student list").Range("A:G")
Set range2 = Sheets("2017-18 student list").Range("J:Q")
Set range3 = Sheets("2017-18 student list").Range("S:X")
set range4 = Sheets("2017-18 student list").Range("AC:AD")
set range5 = Sheets("2017-18 student list").Range("AK:AL")
Set r = Union(range1, range2, range3, range4, range5)
Set ws = ThisWorkbook.Sheets("2017-18 student list")
Finalrow = Sheets("2017-18 student list").Range("A902").End(xlUp).Row
With ws
For i = 7 To Finalrow
If .Cells(i, 1).Value = Findfaculty Then
.Range(.Cells(i, r)).Copy
Sheets("Search Results").Range("A901").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Next i
Sheets("Search Results").Select
Range("A1").Select
End With
End Sub
Any help to get this working would be greatly appreciated!