Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hello there :)
I have created a userform that is supposed to update or delete records in a named range "Cattle_breeds". The listbox fills, and the selected record populates the textbox, but once I edit the record and click on Update, the value goes into the top of the range, at cell "A16", replacing the cell's contents in A16. The updated entry is supposed to be updated and stay in place in its current cell. Does anyone have any ideas on this? Thank you!!! Here is the code: Option Explicit Private source As Range Private NDEX As Long Private breed As String Private Sub Update_Click() Application.ScreenUpdating = False Dim pointer As String If breedtb.Text = "" Then Exit Sub pointer = lstdata.ListIndex For NDEX = 2 To source.Rows.Count source.Cells(NDEX, 1) = Me.breedtb.Value 'source.Cells(NDEX, 1) = Trim(Me.breedtb.Value) ' not working, do I need this part? Exit For 'End If Next 'add sort once this form is up and running!!! 'Worksheets("Animal Inventory").Range("Cattle_breeds").Sort _ 'Key1:=Worksheets("Animal Inventory").Range("A16"), _ 'Header:=xlGuess LoadData lstdata.ListIndex = pointer Application.ScreenUpdating = True End Sub Private Sub breedcmb_Change() LoadData End Sub Private Sub LoadData() Me.breedtb = "" With lstdata ..Clear breed = Me.breedcmb.Value For NDEX = 2 To source.Rows.Count If breed = source.Cells(NDEX, 1) Then ..AddItem source.Cells(NDEX, 1) ..List(.ListCount - 1, 0) = source.Cells(NDEX, 1) End If Next End With End Sub Private Sub Delete_Click() If lstdata.ListIndex = -1 Then Exit Sub Dim NDEX As String Dim msg As String NDEX = Me.breedtb.Value msg = lstdata.List(lstdata.ListIndex, 0) If MsgBox(msg, vbYesNo + vbDefaultButton2, "DELETE #" & NDEX & " from " & breedcmb) = vbYes Then RemoveItem NDEX End If End Sub Private Sub RemoveItem(NDEX As String) Dim found As Range Dim ok As Boolean With Worksheets("Animal Inventory") For Each found In .Range(.Range("Cattle_breeds"), ..Range("Cattle_breeds").End(xlDown)) If found = NDEX Then ok = True Exit For End If Next End With If ok Then found.Resize(, 1).Delete xlShiftUp LoadData Else MsgBox NDEX & " Not Found! " End If End Sub Private Sub lstdata_Click() With lstdata Me.breedtb.Value = .List(.ListIndex, 0) End With End Sub Private Sub Cancel_Click() Application.ScreenUpdating = False Worksheets("Animal Inventory").Protect Worksheets("Animal Inventory").Visible = False Unload Me Application.ScreenUpdating = True End Sub Private Sub Userform_Initialize() Worksheets("Animal Inventory").Visible = True Worksheets("Animal Inventory").Unprotect With Worksheets("Animal Inventory") Set source = .Range(.Range("Cattle_breeds"), ..Range("Cattle_breeds").End(xlDown)) End With LoadBreeds End Sub Private Sub LoadBreeds() Dim breeds As New Scripting.Dictionary For NDEX = 1 To source.Rows.Count breed = source.Cells(NDEX, "A").Value If Not breeds.Exists(breed) Then breeds.Add breed, breed breedcmb.AddItem breed End If Next End Sub Private Sub Clear_Click() breedtb.Text = "" breedcmb.SetFocus End Sub -- Kristina |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Userform Listbox highlights wrong line. | Excel Programming | |||
Date Value in wrong format on userform | Excel Programming | |||
Updating data makes formula wrong | Excel Discussion (Misc queries) | |||
don't understand whats wrong with this userform | Excel Programming | |||
UserForm Pops Up when in the wrong file. | Excel Programming |