VBA Loop Evaluates Cell, Copy & Paste it above Current Row, If Criteria is met

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:




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
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi,

Welcome to the forum...

Would it be possible to post a representation of your data? A table representing the data you want to process, or a picture or even better, upload a sample workbook to Dropbox or OneDrive and provide a link to the shared file...

It would be much easier to assist you with something more visual or graphical to work with.

TX
 
Upvote 0
TX...

I have scanned your code and I can see you are inserting blank rows above the names if they meet a condition. Where will I get this condition and how many blank rows must be inserted if the condition is true?

Please clarify. TX
 
Upvote 0
Hi RudiS,

I actually don't want to insert blank rows. If the name is "Richard Branson", I want to insert rows above that, that contain: "Rich Branson", "Rick Branson", & "D--- Branson"

I was going to code them into either a long if statement, or a select cases statement. Different names will need different numbers of rows (ex: Joe only has the alternative of Joseph, but Richard has alternatives of Rich & Rick & D--- and therefore it needs 3 rows to be added). If you could show me one example of how to code it so that "Richard" in a row, will generate 3 rows above with "Rich" & "Rick" & "D---k" as the first name - then I can write out the if / case statements. I don't understand how to instruct excel to do the first piece.






Thanks!
 
Upvote 0
Hi,

I would approach it differently.
The nickname variations I'd put into a sheet in the workbook and hide the sheet.
The code would then reference the sheet to find the actual name and extract the nicknames to the right of it (see example table below)
This way would be FAR easier to maintain the names and nicknames, adding, editing, adding more variations and referencing than to have all that in code. It will keep the code shorter and running faster too.

If you agree with this I can assist with the code...


Excel 2012
ABCDEFGH
1Actual NameNickN 1NickN 2NickN 3NickN 4
2JohnJoeJJ
3JaneJJoeySweetheart :)
4BobBoBB
5TomTommyMr. T
6MichaelMickeyMike
7GeorgeGorgePorgyPuddingPie
8BillBoBilly
9
10VBA will reference col A and find the actual name vertically
11Once the name is found, it can offset to the right and collect each name as listed in the row
12
13
Nicknames
 
Upvote 0
Ignore the link above...

Here is an updated and improved version that will remove the nicknames and rebuild the list if you make changes (editions, deletions and added nicknames. It clears the nickname build in the master list and rebuilds it to incorporate the modifications to the nickname list...

Updated workbook and code.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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