?
Solved

Excel 2013 VBA - Arrange data from two rows to one

Posted on 2016-08-10
6
Medium Priority
?
54 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 32

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 32

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
Industry Leaders: 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!

 
LVL 32

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 2000 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 32

Expert Comment

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

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

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 …
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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