Hi I have the following code:
Private Sub Search_Click()
Dim Name As String
Dim f As Range
Dim r As Long
Dim ws As Worksheet
Dim s As Integer
Dim FirstAddress As String
Dim str() As String
Name = surname.Value
With ws
Set f = Range("A:A").Find(what:=Name, LookIn:=xlValues)
If Not f Is Nothing Then
With Me
firstname.Value = f.Offset(0, 1).Value
tod.Value = f.Offset(0, 2).Value
program.Value = f.Offset(0, 3).Value
email.Value = f.Offset(0, 4).Text
SetCheckBoxes f.Offset(0, 5) '<<< replaces code below
officenumber.Value = f.Offset(0, 6).Text
cellnumber.Value = f.Offset(0, 7).Text
r = f.Row '<<<<<<<<< using this to locate the row of "found"
End With
findnext
FirstAddress = f.Address
Do
s = s + 1
Set f = Range("A:A").findnext(f)
Loop While Not f Is Nothing And f.Address <> FirstAddress
If s > 1 Then
Select Case MsgBox("There are " & s & " instances of " & Name, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
findnext
Case vbCancel
End Select
End If
Else: MsgBox Name & "Not Listed"
End If
End With
End Sub
and i want to use an update button:
private Sub update_Click()
Dim Name As String
Dim f As Range
Dim ws As Worksheet
With ws
Set f = .Cells(r, 1) '<<<<<<<<<<<<< Mismatch type error
f.Value = surname.Value
f.Offset(0, 1).Value = firstname.Value
f.Offset(0, 2).Value = tod.Value
f.Offset(0, 3).Value = program.Value
f.Offset(0, 4).Value = email.Value
f.Offset(0, 5).Value = GetCheckBoxes
f.Offset(0, 6).Value = officenumber.Value
f.Offset(0, 7).Value = cellnumber.Value
End With
End Sub
so i want to locate the row of the found cell and replace all the cells to whatever is written in the textbox (updating the previous infomration with new information) – however i get an error on set f = .cells(r,1) how can i fix this?
Try to use global variable r
: instead declaring Dim r As Long
in Private Sub Search_Click()
, use Public r As Long
outside all functions in the very top of module :
Public r As Long
Public Sub Search_Click()
'your code
End Sub
Public Sub update_Click()
'your code
End Sub
Now, after calling Search_Click
r
would be inizialized and then you could call update_Click
.
P.S. Don’t forget to remove Dim r As Long
from Private Sub Search_Click()
.
BTW, in your update_Click
and Search_Click
subs you haven’t initialized ws
variable: Set ws = ThisWorkbook.Worksheets("Sheet1")
. After adding this line change Range("A:A")
to .Range("A:A")
in Search_Click
Tags: excelexcel