JohnnyBconfused
New Member
- Joined
- Jun 7, 2015
- Messages
- 5
I'm a novice when it comes to VBA, and I have a list of names in Excel 2013 (Running Windows 8.1) and I want to write code that will generate and include all nicknames the person might have, so that if a user enters some variation - it will still be recognized. For example Richard would have the nicknames "Rich", "Rick", "D---". I want the code to add the additional names above the line of code that it is evaluating. I expected the code I currently have to copy and paste the row being evaluated into the newly created row(s) above. I planned to learn how to change the names later - and am currently doing it by hand. Unfortunately I'm getting a run-time error 1004 "application-defined or object-defined error", at "Rows("i:i").Select". I would like all the names to stay in the same spreadsheet. Can this be accomplished?
I've checked many examples here & on stack overflow, but none fit my use case & most send the rows to another sheet / workbook... Below is my code:
I've checked many examples here & on stack overflow, but none fit my use case & most send the rows to another sheet / workbook... Below is my code:
Code:
Sub insertnicknamerows()
Dim First, i, j,k,h
For i = 1 To Rows.Count
First = Split(Cells(i, 1).Value, " ")(0)
'this list grabs assigns the first name to the variable
If First = "Joe" Then
j = i + 1
Rows("i:i").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Rows("j:j").Select
Selection.Copy
Rows("i:i").Select
ActiveSheet.Paste
i = j
ElseIf First = "Richard" Then
j = i + 1
k = i + 2
h = i + 3
Rows("i:i").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Rows("h:h").Select
Selection.Copy
Rows("i:i").Select
ActiveSheet.Paste
Rows("j:j").Select
ActiveSheet.Paste
Rows("k:k").Select
ActiveSheet.Paste
i = h
End If
Next i
End Sub