Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel 2010 - split one row into 2 rows and offset

Posted on 2013-05-22
3
Medium Priority
?
741 Views
Last Modified: 2013-05-23
I would like either vba code or a macro to split the attached spreadsheet as follows:

A1:V1 - Remain as titles
A2:V2 - Remain in row 2
Insert new row and move W2:AK2 to this row as follows
W2 would become F3, X2 would become G3 and so on

Carry on for the remaining 244 rows
Book1.xlsx
0
Comment
Question by:andymacf
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
redmondb earned 2000 total points
ID: 39188289
Hi, andymacf.

Please see attached. Couple of points...
(1) I changed the contents of columns A and W for testing purposes.
(2) It's not the fastest code, but it's OK for a few hundred rows. Let me know if you are planning to run this against tens of thousands.

The code is...
Option Explicit

Sub Split_Rows()
Dim i As Long
Dim xLast_Row As Long

ThisWorkbook.Sheets("Sheet1").Activate

xLast_Row = ActiveSheet.UsedRange.Cells(1, 1).Row + ActiveSheet.UsedRange.Rows.Count - 1
If xLast_Row < 2 Then
    MsgBox ("No data found - run cancelled.")
    Exit Sub
End If

Application.ScreenUpdating = False
    
    For i = xLast_Row To 2 Step -1
    
        Rows(i + 1).Insert Shift:=xlShiftDown
    
        Range("W" & i & ":AK" & i).Cut Destination:=Range("F" & i + 1 & ":T" & i + 1)
    
    Next
    
Columns("W:AK").Delete Shift:=xlToLeft

Application.ScreenUpdating = True

MsgBox "Run complete - " & xLast_Row - 1 & " rows processed."

End Sub

Open in new window

Thanks,
Brian.Book1b-V2.xlsm
0
 
LVL 7

Author Closing Comment

by:andymacf
ID: 39190140
Fantastic job, this file will only ever be run against the 245 rows so it is a perfect fit.
Thanks Brian, much appreciated.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 39190485
Thanks, andymacf, glad to help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question