LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Update Userform updating to wrong cell.

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Userform Listbox highlights wrong line. Alan Carpenter Excel Programming 0 May 15th 07 03:48 AM
Date Value in wrong format on userform Corey Excel Programming 19 November 7th 06 01:06 PM
Updating data makes formula wrong a_moron Excel Discussion (Misc queries) 4 January 27th 06 08:03 AM
don't understand whats wrong with this userform Tim[_39_] Excel Programming 4 October 9th 04 01:55 PM
UserForm Pops Up when in the wrong file. Jeff Marshall[_2_] Excel Programming 0 July 29th 03 08:44 PM


All times are GMT +1. The time now is 09:07 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"