Userform Listbox Edit Rows Within Sheet

pure vito

Board Regular
Joined
Oct 7, 2021
Messages
183
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am trying to fit VBA code I found here to fit my userform, and as you can image I have hit a snag, hopefully the pictures below will help you better understand the issue,

Clicking a row on the list box populates all the text boxes within the userform the goal is to be able to update the sheet "People" with any changes made to these textboxes,

So I would select a row I want to edit make a change to the textbox and have it update the sheet so the code I am using (Private Sub UpdateRow_Click) is working to a degree it is transferring data but to the wrong row and also to the wrong columns,

But it is also giving me a runtime error "70" permission denied,

I have exhausted my knowledge in trying to make this work I can not see or understand the issues within the code,

The next issue I have is that a number of columns G:K have formula so transferring data from the userform textbox's removes this, the data range populating the textboxes is C:AM so the columns with formula fall into this and I can't really change that without making many changes to my document,

Hope this makes sense and I appreciate any help please

I may have other issues as I have only worked on 2 functions of this code aligning it with my userform

Overall the goal is to be able to,

Update (edit) row's
Delete row's
Add row's

Oh and possibly search the Listbox

VBA Code:
Private Sub UserForm_Activate()

Dim sh As Worksheet
Set sh = Sheets("People")
Dim iRow As Long
iRow = sh.Range("C" & Rows.Count).End(xlUp).Row
'to populate listbox  from Database sheet
   With Me.ListBox1
       .ColumnCount = 37
       .ColumnHeads = True
       .ColumnWidths = "150,100,80,80,150,0,0,0,0,80,80,80,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
       .RowSource = "People!C2:AM" & iRow
   End With

'loads userform, selects 1st row of data
'Lastrow = Sheets("People").Cells(Rows.Count, "C").End(xlUp).Row
'Nameinput.ListBox1.ColumnCount = 37
'Nameinput.ListBox1.ColumnWidths = "1cm"
'Nameinput.ListBox1.List = Sheets("People").Range("C2:AM" & Lastrow).Value
'Nameinput.ListBox1.ListIndex = 0
End Sub

Private Sub ListBox1_Click()
Dim i As Integer
'Loads textboxes with selected listbox values
For i = 1 To 37
Nameinput.Controls("TextBox" & i).Value = Nameinput.ListBox1.List(, i - 1)
Next i
End Sub

Private Sub CommandButton1_Click()
'loads textboxes with 1-7 values????
Dim i As Long
For i = 1 To 37
Nameinput.Controls("TextBox" & i).Value = i
Next i
Nameinput.ListBox1.ListIndex = -1
End Sub

Private Sub UpdateRow_Click()
'Loads People with textbox values
'People row update based on listbox selection
'updates listbox. Selects updated row
Dim i As Long, Temp As Integer
If Nameinput.ListBox1.ListIndex < 0 Then
MsgBox "You must select some row in the List Box"
Exit Sub
End If
Lastrow = Sheets("People").Cells(Rows.Count, "C").End(xlUp).Row + 1
For i = 3 To 37
Sheets("People").Cells(Nameinput.ListBox1.ListIndex + 1, i).Value = _
       Nameinput.Controls("TextBox" & i).Value
Next i
Temp = Nameinput.ListBox1.ListIndex
Nameinput.ListBox1.List = Sheets("People").Range("C1:AM" & Lastrow).Value
Nameinput.ListBox1.ListIndex = Temp
End Sub

Private Sub DeleteRow_Click()
'Delete People data based on listbox selection
'updates listbox. Selects same listbox location that was removed
Dim Temp As Integer
If Nameinput.ListBox1.ListIndex < 0 Then
MsgBox "You must select some row in the List Box"
Exit Sub
End If
Temp = Nameinput.ListBox1.ListIndex
Sheets("People").Rows(Nameinput.ListBox1.ListIndex + 1).EntireRow.Delete
Lastrow = Sheets("People").Cells(Rows.Count, "A").End(xlUp).Row + 1
Nameinput.ListBox1.List = Sheets("People").Range("A1:G" & Lastrow).Value
'listbox selection
If Temp + 1 = Lastrow Then
Nameinput.ListBox1.ListIndex = Temp - 1
Else
Nameinput.ListBox1.ListIndex = Temp
End If
End Sub

Private Sub AddRow_Click()
'Adds Row bottom of data on People
'updates listbox and selects new addition
Dim i As Long
Lastrow = Sheets("People").Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To 7
Sheets("People").Cells(Lastrow + 1, i).Value = Nameinput.Controls("TextBox" & i).Value
Next i
Nameinput.ListBox1.List = Sheets("People").Range("A1:G" & Lastrow + 1).Value
Nameinput.ListBox1.ListIndex = Lastrow
End Sub
 

Attachments

  • Capturewwtwt.JPG
    Capturewwtwt.JPG
    237.5 KB · Views: 25
  • ey3ey34.JPG
    ey3ey34.JPG
    140.1 KB · Views: 24
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi all, I’d really appreciate any help with this if anyone has the time please 🙏🏼
 
Upvote 0

Forum statistics

Threads
1,223,929
Messages
6,175,455
Members
452,643
Latest member
gjcase

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top