Solved

Excel 2013 VBA - Arrange data from two rows to one

Posted on 2016-08-10
6
46 Views
Last Modified: 2016-08-10
Dear Experts,

Could you please have a look to the attached file on 'Sheet1', basically I have data in the following format so being in separate lines:
Data in two lines
Could you please advise which VBA code could arrange it as having on the 'Target' sheet, so placing each second line to column B?
Data arranged in one line
Thanks in advance,
TwoLinesExample.xlsm
0
Comment
Question by:csehz
  • 4
  • 2
6 Comments
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41750333
You may try something like this......

Sub ReArrangeData()
Dim sws As Worksheet, dws As Worksheet
Set sws = Sheets("Sheet1")
Set dws = Sheets("Target")
dws.Cells.Clear
On Error Resume Next
sws.Columns("A:A").SpecialCells(xlCellTypeConstants, 1).Copy dws.Range("A1")
sws.Columns("A:A").SpecialCells(xlCellTypeConstants, 2).Copy dws.Range("B1")
dws.Activate
End Sub

Open in new window

0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41750342
Try the suggested code on your sample workbook as the code assumes that there are two sheets called Target and Sheet1 in the workbook where Sheet1 contains the raw data. If the sheet names are different, please change them in the code as per the actual sheet names.
0
 
LVL 1

Author Comment

by:csehz
ID: 41750357
Thank you it works of course,

could you please advise how to modify the code if the A1,A3,A5 etc cells are not number but also text?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41750382
In that case, try this.....
Sub ReArrangeDataV2()
Dim sws As Worksheet, dws As Worksheet
Dim lr As Long
Application.ScreenUpdating = False
Set sws = Sheets("Sheet1")
Set dws = Sheets("Target")
lr = sws.Cells(Rows.Count, 1).End(xlUp).Row
dws.Cells.Clear
sws.Columns(1).Insert
sws.Range("A1:A" & lr).Formula = "=IF(MOD(ROW(),2)=1,NA(),2)"
sws.Range("A:A").SpecialCells(xlCellTypeFormulas, 16).Offset(0, 1).Copy dws.Range("A1")
sws.Range("A:A").SpecialCells(xlCellTypeFormulas, 1).Offset(0, 1).Copy dws.Range("B1")
sws.Columns(1).Delete
dws.Activate
Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:csehz
ID: 41750400
Thank you that is amazing, it works great
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41750431
You're welcome. Glad to help.
Thanks for the feedback.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now