I have a spreadsheet that is used to enter instrument data and associated readings. There are multiple instruments entered from a validated list into a named range of "All_Inst". When an instrument is changed I need to clear out data in the row associated with that entry that is no longer applicable. At this data differs by instrument, I stored the various combinations in a simple 2 column table named "Delete_Data_TBL". From there I'm trying to use a VLookup to feed a case statement to clear the applicable cells.
I've successfully tested the vlookup outside of the intersect using a hardcoded value for the lookup value. I think the problem is using the c.value in the vlookup, but I can't find anything on what syntax to use. I tried assigning c.value to a variable and passing the variable to the vlookup, but wasn't able to get that working either, though again I'm unsure of the syntax.
A somewhat simplified version of what I've tried is as follows;
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Dim d As Integer
'***Check if any ALL_Inst cells have been changed
If Not Intersect(Target, Range("All_Inst")) Is Nothing Then
'Evaluate the cell that has changed to determine what should be set to ""
For Each c In Intersect(Target, Range("All_Inst")).Cells
d = Application.WorksheetFunction.VLookup(c.Value, Sheets("Inst_Tables").Range("Delete_Data_TBL"), 2, False)
Select Case d
Case 0, 5
c.Offset(0, 1).Value = ""
c.Offset(0, 4).Value = ""
c.Offset(0, 7).Value = ""
c.Offset(0, 11).Value = ""
c.Offset(0, 13).Value = ""
c.Offset(0, 15).Value = ""
c.Offset(0, 17).Value = ""
Case 3
c.Offset(0, 15).Value = ""
c.Offset(0, 17).Value = ""
Case 4
c.Offset(0, 1).Value = ""
c.Offset(0, 4).Value = ""
c.Offset(0, 7).Value = ""
c.Offset(0, 11).Value = ""
c.Offset(0, 13).Value = ""
Case 6, 7
c.Offset(0, 4).Value = ""
Case 9
c.Offset(0, 1).Value = ""
c.Offset(0, 4).Value = ""
c.Offset(0, 7).Value = ""
Case Else
'Do Nothing
End Select
Next
End If
End Sub
I apologize to stackexchange, i don't have enough reputation to make a comment but I think your problem is when cl.value is not found in the range. you can add in some error handling as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Dim d As Integer`
'***Check if any ALL_Inst cells have been changed
If Not Intersect(Target, Range("All_Inst")) Is Nothing Then
'Evaluate the cell that has changed to determine what should be set to ""
For Each c In Intersect(Target, Range("All_Inst")).Cells
on error resume next
d = Application.WorksheetFunction.VLookup(c.Value,Sheets("Inst_Tables").Range("Delete_Data_TBL"), 2, False)
if isempty(d) = false then
Select Case d
Case 0, 5
c.Offset(0, 1).Value = ""
c.Offset(0, 4).Value = ""
c.Offset(0, 7).Value = ""
c.Offset(0, 11).Value = ""
c.Offset(0, 13).Value = ""
c.Offset(0, 15).Value = ""
c.Offset(0, 17).Value = ""
Case 3
c.Offset(0, 15).Value = ""
c.Offset(0, 17).Value = ""
Case 4
c.Offset(0, 1).Value = ""
c.Offset(0, 4).Value = ""
c.Offset(0, 7).Value = ""
c.Offset(0, 11).Value = ""
c.Offset(0, 13).Value = ""
Case 6, 7
c.Offset(0, 4).Value = ""
Case 9
c.Offset(0, 1).Value = ""
c.Offset(0, 4).Value = ""
c.Offset(0, 7).Value = ""
Case Else
'Do Nothing
End Select
end if
Next
End Sub
Related
I have a userform that enters data into columns I to S but sometimes all data points does not need to be entered. My probably is I only have the last row counting up on column I so if I have data in J through S, they would get replaced with the next set of data that has data in column I.
What I need help is coding for the last row of all columns or the next blank row of all columns. Thanks.
My code:
Private Sub cmd_EnterData_Click()
Dim iRow As Long
Dim Lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("FirstShift")
Lastrow = ws.Range("i101").End(xlUp).Row
'find first empty row in database
For iRow = 16 To Lastrow
If ws.Cells(iRow, 9) = "" And ws.Cells(iRow, 10) = "" Then
ws.Cells(iRow, 9).Value = Me.textbox_Lane1.Value
ws.Cells(iRow, 10).Value = Me.textbox_Lane2.Value
ws.Cells(iRow, 11).Value = Me.textbox_Lane3.Value
ws.Cells(iRow, 12).Value = Me.textbox_Lane4.Value
ws.Cells(iRow, 13).Value = Me.textbox_Lane5.Value
ws.Cells(iRow, 14).Value = Me.textbox_Lane6.Value
ws.Cells(iRow, 15).Value = Me.textbox_Lane7.Value
ws.Cells(iRow, 16).Value = Me.textbox_Length.Value
ws.Cells(iRow, 17).Value = Me.textbox_SheetCount.Value
ws.Cells(iRow, 18).Value = Me.cbchecktype.Value
ws.Cells(iRow, 19).Value = Me.cbchecktype1.Value
End If
Next iRow
If checkbox_Retest.Value = False And Me.textbox_Lane1.Value = "" Then
'do nothing
Me.textbox_Lane1.SetFocus
MsgBox "ENTER LANE 1 WIDTH!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.textbox_Length.Value = "" Then
'do nothing
Me.textbox_Length.SetFocus
MsgBox "ENTER YOUR LENGTH!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.textbox_SheetCount.Value = "" Then
'do nothing
Me.textbox_SheetCount.SetFocus
MsgBox "ENTER THE SHEETCOUNT!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.cbchecktype.Value = "" Then
'do nothing
Me.cbchecktype.SetFocus
MsgBox "ENTER 'PASS' OR 'FAIL' FOR PERF CHECK!!"
Exit Sub
Select Case checktype
Case Trim(Me.cbchecktype.Value) = "PASS"
checktype = "PASS"
Case Trim(Me.cbchecktype.Value) = "FAIL"
checktype = "FAIL"
End Select
End If
If checkbox_Retest.Value = False And Me.cbchecktype1.Value = "" Then
'do nothing
Me.cbchecktype1.SetFocus
MsgBox "ENTER 'PASS' OR 'FAIL' FOR SLITHER CHECK!!"
Exit Sub
Select Case checktype1
Case Trim(Me.cbchecktype1.Value) = "PASS"
checktype1 = "PASS"
Case Trim(Me.cbchecktype1.Value) = "FAIL"
checktype1 = "FAIL"
End Select
End If
With ws
.Unprotect Password:="password"
.Cells(iRow, 9).Value = Me.textbox_Lane1.Value
.Cells(iRow, 10).Value = Me.textbox_Lane2.Value
.Cells(iRow, 11).Value = Me.textbox_Lane3.Value
.Cells(iRow, 12).Value = Me.textbox_Lane4.Value
.Cells(iRow, 13).Value = Me.textbox_Lane5.Value
.Cells(iRow, 14).Value = Me.textbox_Lane6.Value
.Cells(iRow, 15).Value = Me.textbox_Lane7.Value
.Cells(iRow, 16).Value = Me.textbox_Length.Value
.Cells(iRow, 17).Value = Me.textbox_SheetCount.Value
.Cells(iRow, 18).Value = Me.cbchecktype.Value
.Cells(iRow, 19).Value = Me.cbchecktype1.Value
.Protect Password:="password"
End With
'clear the data
Me.textbox_Lane1.Value = ""
Me.textbox_Lane2.Value = ""
Me.textbox_Lane3.Value = ""
Me.textbox_Lane4.Value = ""
Me.textbox_Lane5.Value = ""
Me.textbox_Lane6.Value = ""
Me.textbox_Lane7.Value = ""
Me.textbox_Length.Value = ""
Me.textbox_SheetCount.Value = ""
Me.cbchecktype.Value = ""
Me.cbchecktype1.Value = ""
Me.checkbox_Retest.Value = False
Me.Hide
End Sub
A.S.H, I've tried your code and same thing is happening BUT I don't think its the code. Please see pictures of before and after. I think the problem is my IF STATEMENT:
"If ws.Cells (iRow, 9) = "" And ws.Cells(iRow, 10) = "" Then
As you can see, the function works fine when I have an item in columns 9 (I) and 10 (J), but when I put no data in those two columns then it gets replaced with whatever data I've entered on my userform as long as it includes data in columns 9 or 10..Thoughts on correcting this?
The following gets you the last non-empty row considering all columns:
Lastrow = ws.UsedRange.Find("*", , , , xlByRows, xlPrevious).Row
You can also restrict it to a set of columns, by replacing .UsedRange to the specific columns range, i.e. the following gets you the last non-empty row in columns G to AB:
Lastrow = ws.Range("G:AB").Find("*", , , , xlByRows, xlPrevious).Row
Add these two dim statements.
Dim ColumnCount As Integer
Dim x As Long
Change your code:
Lastrow = ws.Range("i101").End(xlUp).Row
to:
Lastrow = 0
For ColumnCount = 0 To 10
x = ws.Range("I101").Offset(0, ColumnCount).End(xlUp).Row
If x > Lastrow Then Lastrow = x
Next ColumnCount
edit:
This for loop always start on 16. Is it maybe supposed to start on Lastrow+1?
For iRow = 16 To Lastrow
I am in need of a little help. I creater a UserForm and am attempting to get the form (on one worksheet) to write to another worksheet. I feel like I am getting close but I keep getting the following error:
I keep getting the Run-time 2110 Error in excel.
Beyond that I am trying to get the data in my userform to post to a worksheet titled Hourly CI Data.
The following code is from my UserForm object:
Private Sub cmdbtnCancel_Click()
' Clear data fields and reset the form
Me.formField1.Value = ""
Me.formField2.Value = ""
Me.formField3.Value = ""
Me.formField4.Value = ""
Me.formField5.Value = ""
Me.formField6.Value = ""
Me.formField7.Value = ""
Me.formField8.Value = ""
Me.formField9.Value = ""
Me.formField10.Value = ""
Me.formField11.Value = ""
Me.formField1.SetFocus
Unload Me
End Sub
Sub cmdbtnSave_Click()
Dim iRow As Long
Dim ws As Worksheet
Unload Me
Set ws = Worksheets("Hourly Slot CI - Data")
' Find the next empty row
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
' Input the data in the Data Table
ws.Cells(iRow, 1).Value = Me.formField1.Value
ws.Cells(iRow, 2).Value = Me.formField2.Value
ws.Cells(iRow, 3).Value = Me.formField3.Value
ws.Cells(iRow, 4).Value = Me.formField4.Value
ws.Cells(iRow, 5).Value = Me.formField5.Value
ws.Cells(iRow, 6).Value = Me.formField6.Value
ws.Cells(iRow, 7).Value = Me.formField7.Value
ws.Cells(iRow, 8).Value = Me.formField8.Value
ws.Cells(iRow, 9).Value = Me.formField9.Value
ws.Cells(iRow, 10).Value = Me.formField10.Value
ws.Cells(iRow, 11).Value = Me.formField11.Value
ws.Cells(iRow, 12).Value = Me.formField12.Value
ws.Cells(iRow, 13).Value = Me.formField13.Value
ws.Cells(iRow, 14).Value = Me.formField14.Value
ws.Cells(iRow, 15).Value = Me.formField15.Value
ws.Cells(iRow, 16).Value = Me.formField16.Value
ws.Cells(iRow, 17).Value = Me.formField17.Value
ws.Cells(iRow, 18).Value = Me.formField18.Value
ws.Cells(iRow, 19).Value = Me.formField19.Value
ws.Cells(iRow, 20).Value = Me.formField20.Value
ws.Cells(iRow, 21).Value = Me.formField21.Value
ws.Cells(iRow, 22).Value = Me.formField22.Value
ws.Cells(iRow, 23).Value = Me.formField23.Value
ws.Cells(iRow, 24).Value = Me.formField24.Value
ws.Cells(iRow, 25).Value = Me.formField25.Value
ws.Cells(iRow, 26).Value = Me.formField26.Value
ws.Cells(iRow, 27).Value = Me.formField27.Value
ws.Cells(iRow, 28).Value = Me.formField28.Value
ws.Cells(iRow, 29).Value = Me.formField29.Value
ws.Cells(iRow, 30).Value = Me.formField30.Value
ws.Cells(iRow, 31).Value = Me.formField31.Value
ws.Cells(iRow, 1).Activate
' Clear all fields and reset the form
Me.formField1.Value = ""
Me.formField2.Value = ""
Me.formField3.Value = ""
Me.formField4.Value = ""
Me.formField5.Value = ""
Me.formField6.Value = ""
Me.formField7.Value = ""
Me.formField8.Value = ""
Me.formField9.Value = ""
Me.formField10.Value = ""
Me.formField11.Value = ""
Me.formField12.Value = ""
Me.formField13.Value = ""
Me.formField14.Value = ""
Me.formField15.Value = ""
Me.formField16.Value = ""
Me.formField17.Value = ""
Me.formField18.Value = ""
Me.formField19.Value = ""
Me.formField20.Value = ""
Me.formField21.Value = ""
Me.formField22.Value = ""
Me.formField23.Value = ""
Me.formField24.Value = ""
Me.formField25.Value = ""
Me.formField26.Value = ""
Me.formField27.Value = ""
Me.formField28.Value = ""
Me.formField29.Value = ""
Me.formField30.Value = ""
Me.formField31.Value = ""
Me.formField1.SetFocus
End Sub
I am unsure on exactly how to set this focus correctly so any input is helpful. Thank you in advance!
You're unloading the form before you actually try to set the values. You need to unload the form after you're completely done with it at the very end. In your case, on the CmdbtnSave_Click sub, you'd want to put it right before End Sub, and make sure you remove it towards the beginning.
EDIT: Also, if you're unloading the form, you shouldn't need to clear out the different formfields. Unloading will remove it(and the formfield values) from the computer's memory.
I'm breaking my head over this one and I hope someone can help. I have a procedure that adds a new worksheet into an Excel workbook and adds basic information of this worksheet into an overview in another worksheet (same workbook). It all works fine as it should, but unfortunately with one exception. There is one cell that should have the value of a cell in the newly created Worksheet. I've used this line for it:
c.Offset(0, 27).Value = "=" & Left(AccName.Value, 20) & "!N16"
Here the "Left(AccName.value,20)" equals the worksheet name. Unfortunately here the code opens a dialog window where I can open a file. I have no idea why and thus no idea how I can fix this. Does anybody here have any idea?
Edit: Here's the entire sub:
Sub FillBestandsübersicht()
Dim c As Range
Dim i As Integer
i = 3
'Find next empty row
Set c = Sheets("Bestandsübersicht").Range("A3")
Do Until c.Value = ""
Set c = c.Offset(1, 0)
i = i + 1
Loop
'Fill Bestandsübersicht
c.Value = AccName.Value
c.Offset(0, 1).Value = ProgRef.Value
c.Offset(0, 2).Value = QuoteNr.Value
c.Offset(0, 3).Value = PolicyNr.Value
If LdrY.Value = True Or LocY.Value = False Then c.Offset(0, 4).Value = "n.a."
c.Offset(0, 5).Value = ddUnderwriters.Value
c.Offset(0, 6).Value = IncDate.Value
c.Offset(0, 7).Value = ExpDate.Value
If LdrY.Value = True Then
c.Offset(0, 8).Value = "Lead"
Else
c.Offset(0, 8).Value = "Follow"
End If
c.Offset(0, 10).Value = PMNPL.Value
If LdrY.Value = True And LocY.Value = True Then
c.Offset(0, 11).Value = AmountLoc.Value
Else
c.Offset(0, 11).Value = 0
End If
If CoiY.Value = True Then
c.Offset(0, 12).Value = AmountCOI.Value
Else
c.Offset(0, 12).Value = 0
End If
c.Offset(0, 14).Value = "n"
c.Offset(0, 15).Value = "n"
If DocY.Value = False Then c.Offset(0, 16).Value = "x" Else c.Offset(0, 16).Value = "n"
If LdrY.Value = False Or LocY.Value = False Or CoiY.Value = False Then _
c.Offset(0, 17).Value = "x" Else c.Offset(0, 17).Value = "n"
If FacY.Value = False Then c.Offset(0, 18).Value = "x" Else c.Offset(0, 18).Value = "n"
If LdrY.Value = True Or LocY.Value = False Then c.Offset(0, 19).Value = "x" Else c.Offset(0, 19).Value = "n"
If LdrY.Value = False Or LocY.Value = False Then c.Offset(0, 20).Value = "x" Else c.Offset(0, 20).Value = "n"
c.Offset(0, 21).Value = "n"
c.Offset(0, 26).Value = Left(AccName.Value, 20)
c.Offset(0, 27).Value= "=" & Left(AccName.Value, 20) & "!N16"
'Sort Bestandsübersicht
Range("A3:AB10000").Sort key1:=Range("A3:A10000"), order1:=xlAscending, Header:=xlNo
'AutoFit rows
Sheets("Bestandsübersicht").Rows("3:" & i).EntireRow.autofit
End Sub
I think there is no sheet within your workbook which name equals to result of this function/calculation: Left(AccName.Value, 20)
I'm in the process of making a userform. I have managed to setup a find function using the code below which then also loops and counts the number of cases in the spreadsheet.
I have also created a function to find the next item which is operated by a separate command button but it does not display the records in the userform so it can be amended.
Does anyone have any ideas on how to fix this?
Private Sub FindNext_Click()
Cells.FindNext(After:=ActiveCell).Activate
End Sub
Private Sub Find_Click()
Worksheets("Master").Activate
Dim strFind As String
Dim FirstAddress As String
Dim rSearch As Range
Set rSearch = Range("a1", Range("e65536").End(xlUp))
Dim f As Integer
strFind = Me.TextBox1.Value
With rSearch
Set c = .Find(strFind, LookIn:=xlValues)
If Not c Is Nothing Then
c.Select
With Me
.TextBox2.Value = c.Offset(0, 1).Value
.TextBox3.Value = c.Offset(0, 2).Value
.TextBox4.Value = c.Offset(0, 3).Value
.TextBox5.Value = c.Offset(0, 4).Value
.TextBox6.Value = c.Offset(0, 5).Value
.TextBox7.Value = c.Offset(0, 6).Value
.TextBox8.Value = c.Offset(0, 7).Value
.TextBox9.Value = c.Offset(0, 8).Value
.TextBox10.Value = c.Offset(0, 9).Value
.TextBox11.Value = c.Offset(0, 10).Value
.TextBox12.Value = c.Offset(0, 11).Value
.TextBox13.Value = c.Offset(0, 12).Value
.TextBox14.Value = c.Offset(0, 13).Value
.TextBox20.Value = c.Offset(0, 14).Value
.TextBox21.Value = c.Offset(0, 15).Value
.TextBox15.Value = c.Offset(0, 16).Value
.TextBox22.Value = c.Offset(0, 17).Value
.TextBox16.Value = c.Offset(0, 18).Value
.TextBox18.Value = c.Offset(0, 19).Value
.TextBox19.Value = c.Offset(0, 20).Value
.update.Enabled = True
.Add.Enabled = False
f = 0
End With
FirstAddress = c.Address
Do
f = f + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
If f > 1 Then
Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
Case vbCancel
End Select
Me.Height = frmMax
End If
Else: MsgBox strFind & " not listed"
End If
End With
End Sub
Thanks
You need to encapsulate the update code into its own method (sub) then you can call it for both Find and Find Next. Like:
Private Sub FindNext_Click()
Dim nextCell As Range
Set nextCell = Cells.FindNext(After:=ActiveCell)
'FindNext loops round to the initial cell if it finds no other so we test for it
If Not nextCell.Address(external:=true) = ActiveCell.Address(external:=true) Then
updateFields anchorCell:=nextCell
End If
End Sub
Private Sub Find_Click()
Worksheets("Master").Activate
Dim strFind As String
Dim FirstAddress As String
Dim rSearch As Range
Set rSearch = Range("a1", Range("e65536").End(xlUp))
Dim f As Integer
strFind = Me.TextBox1.Value
With rSearch
Set c = .Find(strFind, LookIn:=xlValues)
If Not c Is Nothing Then
updateFields anchorCell:=c
FirstAddress = c.Address
Do
f = f + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
If f > 1 Then
Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
Case vbCancel
End Select
Me.Height = frmMax
End If
Else: MsgBox strFind & " not listed"
End If
End With
End Sub
Private Sub updateFields(anchorCell As Range)
anchorCell.Select
With Me
.TextBox2.Value = anchorCell.Offset(0, 1).Value
.TextBox3.Value = anchorCell.Offset(0, 2).Value
.TextBox4.Value = anchorCell.Offset(0, 3).Value
.TextBox5.Value = anchorCell.Offset(0, 4).Value
.TextBox6.Value = anchorCell.Offset(0, 5).Value
.TextBox7.Value = anchorCell.Offset(0, 6).Value
.TextBox8.Value = anchorCell.Offset(0, 7).Value
.TextBox9.Value = anchorCell.Offset(0, 8).Value
.TextBox10.Value = anchorCell.Offset(0, 9).Value
.TextBox11.Value = anchorCell.Offset(0, 10).Value
.TextBox12.Value = anchorCell.Offset(0, 11).Value
.TextBox13.Value = anchorCell.Offset(0, 12).Value
.TextBox14.Value = anchorCell.Offset(0, 13).Value
.TextBox20.Value = anchorCell.Offset(0, 14).Value
.TextBox21.Value = anchorCell.Offset(0, 15).Value
.TextBox15.Value = anchorCell.Offset(0, 16).Value
.TextBox22.Value = anchorCell.Offset(0, 17).Value
.TextBox16.Value = anchorCell.Offset(0, 18).Value
.TextBox18.Value = anchorCell.Offset(0, 19).Value
.TextBox19.Value = anchorCell.Offset(0, 20).Value
.Update.Enabled = True
.Add.Enabled = False
End With
End Sub
Afternoon
I'm a mere novice of an amateur in the world of VB.
I'm currently creating a userform in Excel and to search for records I decided to use a listbox option to allow a user to scroll through the search results.
However, I've encountered a run-time 380 error invalid property value due to the listbox exceeding ten entries.
I have managed to find a solution using rowsource command but I can't find how to use it in my code. Any advice is welcome and if anyone can think of a better way I would be grateful.
`enter code here
Dim MyData As Range
Dim c As Range
Dim rFound As Range
Dim r As Long
Dim rng As Range
Const frmMax As Long = 640
Const frmHt As Long = 210
Const frmWidth As Long = 280
Dim sFileName As String
Dim oCtrl As MSForms.Control
Private Sub Add_Click()
Set c = Range("a65536").End(xlUp).Offset(1, 0)
Application.ScreenUpdating = False
With Me
c.Value = .TextBox1.Value
c.Offset(0, 1).Value = .TextBox2.Value
c.Offset(0, 2).Value = .TextBox3.Value
c.Offset(0, 3).Value = .TextBox4.Value
c.Offset(0, 4).Value = .TextBox5.Value
c.Offset(0, 5).Value = .TextBox6.Value
c.Offset(0, 6).Value = .TextBox7.Value
c.Offset(0, 7).Value = .TextBox8.Value
c.Offset(0, 8).Value = .TextBox9.Value
c.Offset(0, 9).Value = .TextBox10.Value
c.Offset(0, 10).Value = .TextBox11.Value
ClearControls
End With
Application.ScreenUpdating = True
End Sub
Private Sub Find_Click()
Worksheets("Master").Activate
Dim strFind As String
Dim FirstAddress As String
Dim rSearch As Range
Set rSearch = Range("a1", Range("e65536").End(xlUp))
Dim f As Integer
strFind = Me.TextBox1.Value
With rSearch
Set c = .Find(strFind, LookIn:=xlValues)
If Not c Is Nothing Then
c.Select
With Me
.TextBox2.Value = c.Offset(0, 1).Value
.TextBox3.Value = c.Offset(0, 2).Value
.TextBox4.Value = c.Offset(0, 3).Value
.TextBox5.Value = c.Offset(0, 4).Value
.TextBox6.Value = c.Offset(0, 5).Value
.TextBox7.Value = c.Offset(0, 6).Value
.TextBox8.Value = c.Offset(0, 7).Value
.TextBox9.Value = c.Offset(0, 8).Value
.TextBox10.Value = c.Offset(0, 9).Value
.update.Enabled = True
.Add.Enabled = False
f = 0
End With
FirstAddress = c.Address
Do
f = f + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
If f > 1 Then
Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
FindAll
Case vbCancel
End Select
Me.Height = frmMax
End If
Else: MsgBox strFind & " not listed"
End If
End With
If Sheet2.AutoFilterMode Then Sheet2.Range("A8").AutoFilter
End Sub
Private Sub TextBox11_Change()
End Sub
Private Sub update_Click()
Application.ScreenUpdating = False
If rng Is Nothing Then GoTo skip
For Each c In rng
If r = 0 Then c.Select
r = r - 1
Next c
skip:
Set c = ActiveCell
c.Value = Me.TextBox1.Value
c.Offset(0, 1).Value = Me.TextBox2.Value
c.Offset(0, 2).Value = Me.TextBox3.Value
c.Offset(0, 3).Value = Me.TextBox4.Value
c.Offset(0, 4).Value = Me.TextBox5.Value
c.Offset(0, 5).Value = Me.TextBox6.Value
c.Offset(0, 6).Value = Me.TextBox7.Value
c.Offset(0, 7).Value = Me.TextBox8.Value
c.Offset(0, 8).Value = Me.TextBox9.Value
c.Offset(0, 9).Value = Me.TextBox10.Value
c.Offset(0, 10).Value = Me.TextBox11.Value
With Me
.update.Enabled = False
.Add.Enabled = True
ClearControls
End With
If Sheet1.AutoFilterMode Then Sheet1.Range("A8").AutoFilter
Application.ScreenUpdating = True
On Error GoTo 0
End Sub
Sub FindAll()
Worksheets("Master").Activate
Dim strFind As String
Dim rFilter As Range
Set rFilter = Sheet2.Range("a1", Range("Z65536").End(xlUp))
Set rng = Sheet2.Range("a1", Range("a65536").End(xlUp))
strFind = Me.TextBox1.Value
With Sheet2
If Not .AutoFilterMode Then .Range("A2").AutoFilter
rFilter.AutoFilter Field:=1, Criteria1:=strFind
Set rng = rng.Cells.SpecialCells(xlCellTypeVisible)
Me.ListBox1.Clear
For Each c In rng
With Me.ListBox1
.AddItem c.Value
.List(.ListCount - 1, 1) = c.Offset(0, 1).Value
.List(.ListCount - 1, 2) = c.Offset(0, 2).Value
.List(.ListCount - 1, 3) = c.Offset(0, 3).Value
.List(.ListCount - 1, 4) = c.Offset(0, 4).Value
.List(.ListCount - 1, 5) = c.Offset(0, 5).Value
.List(.ListCount - 1, 6) = c.Offset(0, 6).Value
.List(.ListCount - 1, 7) = c.Offset(0, 7).Value
.List(.ListCount - 1, 8) = c.Offset(0, 8).Value
.List(.ListCount - 1, 9) = c.Offset(0, 9).Value
.List(.ListCount - 1, 10) = c.Offset(0, 10).Value
End With
Next c
End With
End Sub
Private Sub ListBox1_Click()
If Me.ListBox1.ListIndex = -1 Then 'not selected
MsgBox " No selection made"
ElseIf Me.ListBox1.ListIndex >= 1 Then 'User has selected
r = Me.ListBox1.ListIndex
With Me
.TextBox1.Value = ListBox1.List(r, 0)
.TextBox2.Value = ListBox1.List(r, 1)
.TextBox3.Value = ListBox1.List(r, 2)
.TextBox4.Value = ListBox1.List(r, 3)
.TextBox5.Value = ListBox1.List(r, 4)
.TextBox6.Value = ListBox1.List(r, 5)
.TextBox7.Value = ListBox1.List(r, 6)
.TextBox8.Value = ListBox1.List(r, 7)
.TextBox9.Value = ListBox1.List(r, 8)
.TextBox10.Value = ListBox1.List(r, 9)
.update.Enabled = True 'allow amendment or
.Add.Enabled = False 'don't want duplicate
End With
End If
End Sub
Sub ClearControls()
With Me
For Each oCtrl In .Controls
Select Case TypeName(oCtrl)
Case "TextBox": oCtrl.Value = Empty
Case "OptionButton": oCtrl.Value = False
End Select
Next oCtrl
End With
End Sub
Private Sub UserForm_Click()
End Sub
You might take a look at the ListView Control (Right-click on the toolbox and search for additional controls, look for Microsoft ListView Control, version 6.0).
Not being the most modern and polished, it may still be very fitting for your immediate needs.
Some sample might look like this:
You build the columns by adding the ColumnHeaders first. Then you add ListItems (=first column) which also each allocates a set óf SubItems (=2nd to last column, index from 1).
Dim l As ListItem
With Me.ListView1
.FullRowSelect = True
.LabelEdit = lvwManual
.View = lvwReport
For i = 1 To 11
.ColumnHeaders.Add , , CStr(i)
Next
.HideColumnHeaders = False
Set l = .ListItems.Add(, , c.Text)
For i = 1 To 10
l.SubItems(i) = c.Offset(0, i).Text
Next
End With