VBA Basic Userform - add -update -delete

Drexi

New Member
Joined
Jun 21, 2024
Messages
3
Office Version
  1. 2021
Platform
  1. Web
Please see the “Assignment 4” screencast for a detailed demonstration of what you are trying to create in this assignment.

In the starter file, we have the seven dwarves and their phone numbers:

ASbKGfAYEee7LQrRPHr2wA_e99315a5fcf5065fdf864cab455a56cf_dwarves.png

Obviously, with so few records it would be easy just to edit the cells/spreadsheet directly. However, if you had hundreds or thousands of records (rows) and perhaps a dozen or two columns of data, it would be more difficult to modify/update the data. Therefore, it’s nice to manage/organize the data in a central user form. The drop-down list here could easily be modified to be a search tool by utilizing what you have learned in the course.

Module1 of the file “Assignment 4 – STARTER.xlsm” has 5 separate subroutines:

RunForm – the on-sheet button is linked to this sub, which simply opens up the NameForm user form.

PopulateComboBox – Prior to opening NameForm, this sub should populate ComboBox1 on NameForm with the names in column A of the spreadsheet.

AddName – This sub should enable the user to add another record (row). The name of the new contact is entered into the NewName text box and when the AddButton is clicked, an input box should ask the user for the phone number of the new contact. The new name and phone number should be placed in the next empty row of the spreadsheet with the other data.

DeleteItem – After selecting a name from the combo box, the user can delete that contact by clicking on the DeleteButton, which will run the DeleteItem sub. Make sure to remove the deleted item from ComboBox1; otherwise, the deleted item will remain on the drop-down list until the user form is closed. See the second part of the screencast "Introduction to combo boxes" for hints on how to delete items.

UpdateNumber – The user can select a name from ComboBox1 and modify/update the phone number of that contact by clicking on the PhoneButton. This should run the UpdateNumber sub, which will ask the user for the new number and replace that contact’s old number on the spreadsheet with the new number.

I have made everything work, but the grader isn't reading my code, can anyone help me go through my code?

Option Explicit
Sub RunForm()
PopulateComboBox
NameForm.Show
End Sub

Sub PopulateComboBox()
'Place your code here!
Dim Names() As String, n As Integer, i As Integer
n = WorksheetFunction.CountA(Columns("A:A"))
ReDim Names(n) As String
For i = 1 To n
Names(i) = Range("A1:A" & n).Cells(i, 1)
NameForm.ComboBox1.AddItem Names(i)
Next i
End Sub

Sub AddName()
Dim nRows As Integer, i As Integer, pn As String
'Input validation
If NameForm.NewName = "" Then
MsgBox "Name field cannot be left blank!"
Exit Sub
End If

pn = InputBox("Please Enter " & NameForm.NewName & "'s phone number.", "New Name")
nRows = Application.WorksheetFunction.CountA(Range("A:A"))
Range("A" & nRows + 1) = NameForm.NewName
Range("B" & nRows + 1) = pn

'Place your code here!
End Sub

Sub DeleteItem()
Dim RemoveIndex As Integer, Ans As Integer
Ans = MsgBox("Are you sure you want to delete this record?!", 20)
If Ans = 6 Then

'Place your code here!

'HINT: Whenever you delete an entire row, you'll want to
'add an entire row below all data; otherwise, the on-sheet
'button will move position on the sheet until it is deleted

RemoveIndex = NameForm.ComboBox1.ListIndex
NameForm.ComboBox1.RemoveItem RemoveIndex
Rows(RemoveIndex + 1 & ":" & RemoveIndex + 1).Select
Selection.Delete Shift:=xlUp

End If
Range("A1").Select
NameForm.ComboBox1.Value = Range("A1")
End Sub

Sub UpdateNumber()
Dim Ans As String, Index As Integer
Ans = InputBox("What is " & NameForm.ComboBox1.Value & "'s new phone number?")
If Ans <> "" Then 'Protects against empty input OR cancel button
Index = NameForm.ComboBox1.ListIndex + 1
Cells(Index, 2) = WorksheetFunction.Transpose(Ans)
'Place your code here!
End If
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
VBA Code:
Option Explicit
Sub RunForm()
PopulateComboBox
NameForm.Show
End Sub

Sub PopulateComboBox()
'Place your code here!
Dim Names() As String, n As Integer, i As Integer
n = WorksheetFunction.CountA(Columns("A:A"))
ReDim Names(n) As String
For i = 1 To n
Names(i) = Range("A1:A" & n).Cells(i, 1)
NameForm.ComboBox1.AddItem Names(i)
Next i
End Sub

Sub AddName()
Dim nRows As Integer, i As Integer, pn As String
'Input validation
If NameForm.NewName = "" Then
MsgBox "Name field cannot be left blank!"
Exit Sub
End If

pn = InputBox("Please Enter " & NameForm.NewName & "'s phone number.", "New Name")
nRows = Application.WorksheetFunction.CountA(Range("A:A"))
Range("A" & nRows + 1) = NameForm.NewName
Range("B" & nRows + 1) = pn

'Place your code here!
End Sub

Sub DeleteItem()
Dim RemoveIndex As Integer, Ans As Integer
Ans = MsgBox("Are you sure you want to delete this record?!", 20)
If Ans = 6 Then

'Place your code here!

'HINT: Whenever you delete an entire row, you'll want to
'add an entire row below all data; otherwise, the on-sheet
'button will move position on the sheet until it is deleted

RemoveIndex = NameForm.ComboBox1.ListIndex
NameForm.ComboBox1.RemoveItem RemoveIndex
Rows(RemoveIndex + 1 & ":" & RemoveIndex + 1).Select
Selection.Delete Shift:=xlUp

End If
Range("A1").Select
NameForm.ComboBox1.Value = Range("A1")
End Sub

Sub UpdateNumber()
Dim Ans As String, Index As Integer
Ans = InputBox("What is " & NameForm.ComboBox1.Value & "'s new phone number?")
If Ans <> "" Then 'Protects against empty input OR cancel button
Index = NameForm.ComboBox1.ListIndex + 1
Cells(Index, 2) = WorksheetFunction.Transpose(Ans)
'Place your code here!
End If
End Sub
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: VBA Basic Userform - add -update -delete
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
VBA Code:
Option Explicit
Sub RunForm()
PopulateComboBox
NameForm.Show
End Sub

Sub PopulateComboBox()
'Place your code here!
Dim Names() As String, n As Integer, i As Integer
n = WorksheetFunction.CountA(Columns("A:A"))
ReDim Names(n) As String
For i = 1 To n
Names(i) = Range("A1:A" & n).Cells(i, 1)
NameForm.ComboBox1.AddItem Names(i)
Next i
End Sub

Sub AddName()
Dim nRows As Integer, i As Integer, pn As String
'Input validation
If NameForm.NewName = "" Then
MsgBox "Name field cannot be left blank!"
Exit Sub
End If

pn = InputBox("Please Enter " & NameForm.NewName & "'s phone number.", "New Name")
nRows = Application.WorksheetFunction.CountA(Range("A:A"))
Range("A" & nRows + 1) = NameForm.NewName
Range("B" & nRows + 1) = pn

'Place your code here!
End Sub

Sub DeleteItem()
Dim RemoveIndex As Integer, Ans As Integer
Ans = MsgBox("Are you sure you want to delete this record?!", 20)
If Ans = 6 Then

'Place your code here!

'HINT: Whenever you delete an entire row, you'll want to
'add an entire row below all data; otherwise, the on-sheet
'button will move position on the sheet until it is deleted

RemoveIndex = NameForm.ComboBox1.ListIndex
NameForm.ComboBox1.RemoveItem RemoveIndex
Rows(RemoveIndex + 1 & ":" & RemoveIndex + 1).Select
Selection.Delete Shift:=xlUp

End If
Range("A1").Select
NameForm.ComboBox1.Value = Range("A1")
End Sub

Sub UpdateNumber()
Dim Ans As String, Index As Integer
Ans = InputBox("What is " & NameForm.ComboBox1.Value & "'s new phone number?")
If Ans <> "" Then 'Protects against empty input OR cancel button
Index = NameForm.ComboBox1.ListIndex + 1
Cells(Index, 2) = WorksheetFunction.Transpose(Ans)
'Place your code here!
End If
End Sub
Hi Guys, the code is fine.
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,341
Members
452,638
Latest member
Oluwabukunmi

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