Hi guys,
Long time reader, first time I've registered. I was wondering if anyone can be so kind as to help me pick the obvious error I'm missing trying to set up an automated workbook query.
It's set up as a userform that will have a button/.LostFocus event to prompt a query checking all sheets for the user's assets.
Currently the click is giving an error at the line "For Each X In GetSearchArray(myValue)"
Here's my code, apologies, I know it needs a tidy.
Long time reader, first time I've registered. I was wondering if anyone can be so kind as to help me pick the obvious error I'm missing trying to set up an automated workbook query.
It's set up as a userform that will have a button/.LostFocus event to prompt a query checking all sheets for the user's assets.
Currently the click is giving an error at the line "For Each X In GetSearchArray(myValue)"
Here's my code, apologies, I know it needs a tidy.
Code:
Private Sub ComboType_Change()
If ComboType.Text = "Phone" Then
LabelIdentify.Caption = "IMEI:"
Else
LabelIdentify.Caption = "Asset:"
End If
End Sub
Sub Check_Click()
myValue = TextName.Value
[I] For Each X In GetSearchArray(myValue)[/I]
If InStr(X, "chris") Or InStr(X, "DET") Then
Else
Debug.Print X
List = List & X & vbNewLine
End If
Next
End Sub
Private Sub CommandButton1_Click()
SelectFirstBlankCell (1) 'Input Column number
End Sub
Private Sub UserForm_Initialize()
ComboType.AddItem "Phone"
ComboType.AddItem "Laptop"
ComboType.AddItem "Desktop"
End Sub
Public Sub SelectFirstBlankCell(SourceCol)
Dim rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
If ComboType.Text = "Phone" Then
Sheet = "Phone"
ElseIf ComboType.Text = "Laptop" Then
Sheet = "Laptop"
ElseIf ComboType.Text = "Desktop" Then
Sheet = "Desktop"
Else
Exit Sub
End If
'SourceCol = 6 'column F has a value of 6
Worksheets(Sheet).Activate
rowCount = Cells(Rows.Count, SourceCol).End(xlUp).Row + 1
'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, SourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, SourceCol).Select
EmptyRow = ActiveCell.Row
EmptyColumn = ActiveCell.Column
ProcessInformation (EmptyRow)
Exit For
End If
Next
End Sub
Private Sub ProcessInformation(Row As Integer)
Dim Column As Integer
Dim Identifier As String
'Identifier = TextIdentifier.Text
Column = 1
Cells(Row, Column).Value = TextName.Text
Cells(Row, Column + 1).Value = ComboType.Text
Cells(Row, Column + 2).NumberFormat = "@"
Cells(Row, Column + 2).Value = TextIdentifier.Value
Unload Me
End Sub
Function GetSearchArray(strSearch)
Dim strResults As String
Dim DeviceModel As String
Dim SHT As Worksheet
Dim rFND As Range
Dim sFirstAddress
For Each SHT In ThisWorkbook.Worksheets
Set rFND = Nothing
With SHT.UsedRange
Set rFND = .Cells.Find(What:=strSearch, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlRows, SearchDirection:=xlNext, MatchCase:=False)
If Not rFND Is Nothing Then
sFirstAddress = rFND.Address
Do
If DeviceModel = vbNullString Then
If SHT.Name = "Mobiles" Then
DeviceModel = ThisWorkbook.Sheets("Mobiles").Cells(rFND.Row, rFND.Column + 2)
ElseIf SHT.Name = "Laptops" Then
DeviceModel = ThisWorkbook.Sheets("Laptops").Cells(rFND.Row, rFND.Column + 3) & " " & ThisWorkbook.Sheets("Laptops").Cells(rFND.Row, rFND.Column + 4)
ElseIf SHT.Name = "4G Services" Then
DeviceModel = ThisWorkbook.Sheets("4G Services").Cells(rFND.Row, rFND.Column + 5)
End If
End If
If strResults = vbNullString Then
Address = " [" & rFND.Address & "]"
strResults = SHT.Name & " - "
strResults = strResults & DeviceModel & Address
DeviceModel = vbNullString
Else
Address = " [" & rFND.Address & "]"
strResults = strResults & "|" & SHT.Name & " - "
strResults = strResults & DeviceModel & Address
DeviceModel = vbNullString
End If
Set rFND = .FindNext(rFND)
Loop While Not rFND Is Nothing And rFND.Address <> sFirstAddress
End If
End With
Next
If strResults = vbNullString Then
GetSearchArray = Null
ElseIf InStr(1, strResults, "|", 1) = 0 Then
GetSearchArray = Array(strResults)
Else
GetSearchArray = Split(strResults, "|")
End If
'MsgBox GetSearchArray
End Function