I have the following script that I am trying to fix. Its aim is to copy an entire row from one sheet to another based on an Array search. Currently it doesnt work, and throws an error on line 13 "Type Mismatch" on
If CStr(Range(k).Value) = whatyousearchingfor Then
I am no sure how to correct this error. This has been adapted from a functional script that looked for a String only (whatyousearchingfor) and I am trying to convert this to be able to handle an Array as an input instead
'All this crappy script does is search for shit in column K, if it matches, copy entire damn row to another workbook
Sub CellShift()
'variables.
Dim Range As Range
Dim Cell As Range
Dim i As Long
Dim j As Long
Dim k As Long
Dim whatyousearchingfor
whatyousearchingfor = Array( _
"HP EliteBook 840 G3", _
"HP EliteBook 840 G6", _
"HP EliteBook 840 G5", _
"HP EliteDesk 800 G3 SFF", _
"HP EliteDesk 800 G2 SFF", _
"HP EliteBook 850 G3", _
"HP EliteDesk 800 G2 TWR", _
"HP EliteDesk 800 G4 SFF", _
"HP ProOne 600 G4 21.5-in Touch AiO", _
"HP ZBook 15u G6", _
"HP EliteBook 850 G5", _
"HP ZBook 15u G3", _
"HP EliteDesk 800 G2 DM 35W", _
"HP EliteDesk 800 G3 DM 35W", _
"HP EliteBook 850 G6", _
"HP EliteDesk 800 G4 DM 65W" _
)
'Change " " to anything your sheet is called
i = Worksheets("DONT DELETE - Full System List").UsedRange.Rows.count
j = Worksheets("Cleaned Tables").UsedRange.Rows.count
'Make sure the space is free. If not, find a free space.
If j = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("Cleaned Tables").UsedRange) = 0 Then j = 0
End If
'Set active range as Column, wont work otherwise.
Set Range = Worksheets("DONT DELETE - Full System List").Range("K2:K" & i)
'Set to false to save some compute power if your pc is shit.
Application.ScreenUpdating = True
'Magic goes below here not above.
For k = 1 To Range.count
'Looking for your stuff
If CStr(Range(k).Value) = whatyousearchingfor Then
'Do the shit plz
Range(k).EntireRow.Copy Destination:=Worksheets("Cleaned Tables").Range("A" & j + 1)
'Can add a delete here if you really want. Wouldnt recommend it...kinda destructive...just remove the ' on next line
'Range(k).EntireRow.Delete
'Gotta move onto the next row
If CStr(Range(k).Value) = whatyousearchingfor Then
'now shift that row on the other sheet, otherwise youll loop forever and get nowhere.
j = j + 1
'if you enabled row delete above, turn this on too:
'k = k -1
'Close that if
End If
'Close that if x2
End If
'NEXT!
Next
'Okay, can stop, undo that screen pause
Application.ScreenUpdating = True
'TADA (Hopefully)
End Sub
Any help on this would be phenomenal, thank you for the assistance in advance