Macros that move data

the_fitz

New Member
Joined
Mar 29, 2012
Messages
2
Hi All,

I am a noob to macros but have a need to get some data reformatting done that occurs often in my business.

I start with this...

-- removed inline image ---

Basically, column b's quantities need to get moved to the row below it's associated barcode in column a

I tried auto recording a macro, and it worked ONLY on the first line of the sheet. I will not know how many rows there are, so the macro should run until there is no data in column b.

I get that this requires cells "by reference" and some kind of loop (do:while , I think)

Could someone please help me with my excel 2007 spreadsheet?

Thanks,
the_fitz
 
Last edited:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi there,
Give this a try

Code:
Sub test()
 
Dim i As Long
Dim x As Long
 
Range("A1").Copy
Range("E1").PasteSpecial
Range("B1").Copy
Range("E2").PasteSpecial
 
i = ActiveSheet.UsedRange.Rows.Count
x = 3
 
For Each cell In Range("A2:B" & i)
    cell.Copy
    Range("E" & x).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, Skipblanks:=False, Transpose:=True
    x = x + 1
Next cell
 
Range("A1:B65536").Clear
Range("E1:E65536").Copy
Range("A1").PasteSpecial
Range("E1:E65536").Clear
Range("A1").Select
 
End Sub
 
Upvote 0
Welcome to the forum!

This should do the trick.
Code:
Sub ReformatColumnToNextRow()
Dim i As Long, b As Variant
For i = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
    b = Cells(i, "B").Value
    Rows(i + 1).Insert (xlShiftDown)
    Cells(i + 1, "A").Value = b
Next
Columns(2).Delete (xlshiftleft)
End Sub

NOTE: Since this deletes data I recommend you test this in a copy of your worksheet first. Although it doesn't actually 'DELETE' it, it moves it in such a way that you can't just copy/paste it back.
 
Upvote 0
the_fitz,


Welcome to the MrExcel forum.


Sample raw data:


Excel Workbook
AB
1Barcode:Quantity
2077178466
38105980113843
4077178784
58105980117452
6
7
8
9
10
11
Sheet1





After the macro:


Excel Workbook
AB
1Barcode:
2Quantity
307717846
46
5810598011384
63
707717878
84
9810598011745
102
11
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub ReorgData()
' hiker95, 03/29/2012
' http://www.mrexcel.com/forum/showthread.php?t=625084
Dim r As Long, lr As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = lr To 1 Step -1
  Rows(r + 1).Insert
  Cells(r + 1, 1).Value = Cells(r, 2).Value
  Cells(r, 2) = ""
Next r
Application.ScreenUpdating = True
End Sub


Then run the ReorgData macro.
 
Upvote 0
Yet Another Way...

Code:
Sub RformatBCdata()
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 3 Step -1
    Cells(i, 1).EntireRow.Insert
Next i
LR = Range("B" & Rows.Count).End(xlUp).Row
    Range("B2:B" & LR).Cut Destination:= _
    Range("B3")
End Sub
 
Upvote 0
And still one more (I think this one will execute quite quickly)...

Code:
Sub MergeColumns()
  Dim X As Long, LastRow As Long, TwoCols As Variant, OneCol As Variant
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  TwoCols = Range("A1:B" & LastRow)
  ReDim OneCol(1 To 2 * LastRow, 1 To 1)
  Application.ScreenUpdating = False
  For X = 1 To LastRow
    OneCol(2 * X - 1, 1) = TwoCols(X, 1)
    OneCol(2 * X, 1) = TwoCols(X, 2)
  Next
  Range("A1").Resize(2 * LastRow) = OneCol
  Columns("B").Clear
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
LOL, he even got 3 solutions AFTER he had what he needed.
This forum is awesome!
I definitely appreciate seeing all of the variant solutions as it prompts me to think about writing code in new and 'hopefully' more efficient ways.
 
Upvote 0

Forum statistics

Threads
1,215,350
Messages
6,124,430
Members
449,158
Latest member
burk0007

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top