Insert Name Alphabetically into Column with VBA

ciavala

Board Regular
Joined
Dec 7, 2003
Messages
223
Office Version
  1. 2010
Platform
  1. Windows
Hello Everyone,

Need some help with some VBA. I want to take the value of an input box, which is going to be a name of a new employee, and then look down the column of employee names alphabetically and insert new row with new name. Is this possible?


Thanks
ciavala
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this:

Code:
Sub InsertName()
Dim sNewName As String
Dim lPosition As Long
Dim rEmpList As Range

Set rEmpList = Sheets("Sheet1").Range("A:A") 'adjust as required

sNewName = InputBox("Enter name of new employee")
On Error Resume Next 'if employee needs to go at start of list, Match will return #NA
lPosition = Application.WorksheetFunction.Match(sNewName, rEmpList, 1)
On Error GoTo 0 'revert to normal error handling (crashing)
Rows(lPosition + 1).Insert
Range("A" & lPosition + 1).Value = sNewName

End Sub
 
Upvote 0
Works like a charm cornflakegirl. I appriciate the help.

Thanks
ciavala
 
Upvote 0
Try this:

Rich (BB code):
Sub InsertName()
Dim sNewName As String
Dim lPosition As Long
Dim rEmpList As Range

Set rEmpList = Sheets("Sheet1").Range("A:A") 'adjust as required

sNewName = InputBox("Enter name of new employee")
On Error Resume Next 'if employee needs to go at start of list, Match will return #NA 
lPosition = Application.WorksheetFunction.Match(sNewName, rEmpList, 1)
On Error GoTo 0 'revert to normal error handling (crashing)
Rows(lPosition + 1).Insert
Range("A" & lPosition + 1).Value = sNewName

End Sub

Thanks for this, Cornflakegirl,

Can you modify it so that I can insert a cell that's not on column A and based on a named range? I tried to manipulate your code but with no luck. In my application, the named range is usually between C76:C115, but the column can change if an user insert a column between A&B or rows can be added, thus I like to use a named range.
 
Upvote 0
Code:
Set rEmpList = Sheets("Sheet1").Range("YourNamedRange") 
Sheets("Sheet1").Rows(lPosition + 1).Insert
Sheets("Sheet1").Range("YourNamedRange" & lPosition + 1).Value = sNewName
HTH. Dave
 
Last edited:
Upvote 0
Code:
Set rEmpList = Sheets("Sheet1").Range("YourNamedRange") 
Sheets("Sheet1").Rows(lPosition + 1).Insert
Sheets("Sheet1").Range("YourNamedRange" & lPosition + 1).Value = sNewName
HTH. Dave

Thank you for your reply, Dave. I had tried that before, but I get error of "400" when it tries to execute the very last line.
 
Upvote 0
Hi
Try
Code:
Sub InsertName()
Dim sNewName As String
Dim lPosition As Long
Dim rEmpList As Range

Set rEmpList = Range("names") 'adjust as required

sNewName = InputBox("Enter name of new employee")
On Error Resume Next 'if employee needs to go at start of list, Match will return [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=NA]#NA[/URL] 
lPosition = Application.WorksheetFunction.Match(sNewName, rEmpList, 1)
On Error GoTo 0 'revert to normal error handling (crashing)
rEmpList(lPosition + 1).Insert
rEmpList(lPosition + 1).Value = sNewName

End Sub
 
Upvote 0
Hi
Try
Code:
Sub InsertName()
Dim sNewName As String
Dim lPosition As Long
Dim rEmpList As Range

Set rEmpList = Range("names") 'adjust as required

sNewName = InputBox("Enter name of new employee")
On Error Resume Next 'if employee needs to go at start of list, Match will return [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=NA"]#NA[/URL] 
lPosition = Application.WorksheetFunction.Match(sNewName, rEmpList, 1)
On Error GoTo 0 'revert to normal error handling (crashing)
rEmpList(lPosition + 1).Insert
rEmpList(lPosition + 1).Value = sNewName

End Sub

Thanks, Fluff. That worked great. How to format that cell the new value is just now added with a different shade of color? It makes it easier to find once it is inserted to a new row.
 
Upvote 0
Add this
Code:
rEmpList(lPosition + 1).Interior.color=vbred
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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