I am reletively new to VBA and a lot of web trawling was not yielding the answer to this simple question so hopefully this will help someone else.
I was faced with having to reorder rows within subsets in a worksheet which could not be defined in a normal sort and had to be done through loops and If Then statements. I turns out this was not as difficult as I first thought and the code below shows it in simple fashion.
Hope this turns out to be useful to some other newby.Sub copy_insert() ' use loops and If Then logic to decide the original row ' position and the row where it should appear in the ' worksheet use variables for source and destination ' in this example src and dest. src = 5 dest = 2 ' copy original "source" row Rows(src).Select Selection.Copy ' insert at new position in worksheet "destination" Rows(dest).Select Selection.Insert Shift:=xlDown ' delete original "source" row which is now a duplicate Rows(src + 1).Select Application.CutCopyMode = False Selection.Delete Shift:=xlUp End Sub
Since you are relately new to VBA you may not be familiar to avoid select and activate in VBA.
The code you posted can also be written as:
Sub copy_insert() src = 5 dest = 2 rows(dest).insert rows(scr).cut rows(dest+1) End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks