Fire North
New Member
- Joined
- Feb 12, 2014
- Messages
- 5
Hello guys,
I'm quite new when it gets to excel/vba so bear with me please.
My excel file is supposed to be a customer database, where the first spreadsheet (called Intro) has a search customer button. My aim here is for the user to write the name of the customer, the macro will then grab the first letter, open the correspondent spreadsheet (e.g. "A","B",...) and search for the first entry that matches the customer's name.
This is what I have got so far (I have the search button opening an userform where the user types the customer's name):
'---------------------------------------------------------
Private Sub CommandButton1_Click()
Dim cname As String
Dim newsheet As String
Dim Rng As Range
Dim objtemp As Range
Dim newworksheet As Worksheet
Dim newactsheet As Worksheet
Dim tempcname As String
Application.ScreenUpdating = False
'Get customer name from Textbox1
cname = TextBox1.Text
'Create new string with just first letter from previous string
newsheet = Left(Trim(cname), 1)
Set newworksheet = ThisWorkbook.Sheets(newsheet)
newworksheet.Activate
'Open sheet with matching letter
Set objtemp = newworksheet.Range("A2:A300")
tempcname = Trim(cname)
If tempcname <> "" Then
With objtemp
Set Rng = .Find(What:=cname, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Could Not Find " & cname
Application.Goto (ThisWorkbook.Sheets("Intro").Range("A1"))
End If
End With
Else
MsgBox "Customer Field is empty. Please insert customer's name!"
End If
Application.ScreenUpdating = True
End
End Sub
'-----------------------------------------------------------------------
My issue is that the program does find the customer, but the screen just becomes frozen. The mouse wheel makes the sidebar move, while the screen remains locked for some reason. Another issue is that the delete button or backspace cease to work as well...It seems the only way to fix this is by selecting another spreadsheet. Ideally I wanted to go to the customer's name on the database and edit the next columns (different jobs for the same customer).
My first clue would be an infinite loop, but I am just stuck now (I am sorry for the amount of variables, this was my attempt to fix it without success)... Could anyone please give me a hand here?
Thanks in advance for the attention given to my problem guys.
I'm quite new when it gets to excel/vba so bear with me please.
My excel file is supposed to be a customer database, where the first spreadsheet (called Intro) has a search customer button. My aim here is for the user to write the name of the customer, the macro will then grab the first letter, open the correspondent spreadsheet (e.g. "A","B",...) and search for the first entry that matches the customer's name.
This is what I have got so far (I have the search button opening an userform where the user types the customer's name):
'---------------------------------------------------------
Private Sub CommandButton1_Click()
Dim cname As String
Dim newsheet As String
Dim Rng As Range
Dim objtemp As Range
Dim newworksheet As Worksheet
Dim newactsheet As Worksheet
Dim tempcname As String
Application.ScreenUpdating = False
'Get customer name from Textbox1
cname = TextBox1.Text
'Create new string with just first letter from previous string
newsheet = Left(Trim(cname), 1)
Set newworksheet = ThisWorkbook.Sheets(newsheet)
newworksheet.Activate
'Open sheet with matching letter
Set objtemp = newworksheet.Range("A2:A300")
tempcname = Trim(cname)
If tempcname <> "" Then
With objtemp
Set Rng = .Find(What:=cname, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Could Not Find " & cname
Application.Goto (ThisWorkbook.Sheets("Intro").Range("A1"))
End If
End With
Else
MsgBox "Customer Field is empty. Please insert customer's name!"
End If
Application.ScreenUpdating = True
End
End Sub
'-----------------------------------------------------------------------
My issue is that the program does find the customer, but the screen just becomes frozen. The mouse wheel makes the sidebar move, while the screen remains locked for some reason. Another issue is that the delete button or backspace cease to work as well...It seems the only way to fix this is by selecting another spreadsheet. Ideally I wanted to go to the customer's name on the database and edit the next columns (different jobs for the same customer).
My first clue would be an infinite loop, but I am just stuck now (I am sorry for the amount of variables, this was my attempt to fix it without success)... Could anyone please give me a hand here?
Thanks in advance for the attention given to my problem guys.