Solved

Excel 2013 VBA - Arrange data from two rows to one

Posted on 2016-08-10
6
53 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 31

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 31

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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 31

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 31

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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