Macro Help,

mazher

Active Member
Joined
Nov 26, 2003
Messages
363
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi All,

Please can some one help me with the macro.

I need to do the following tasks

Data starts from Column A till Column ADX
Row 1 is date
Row 2 below are the invoice numbers, In some columns there is only a row of data nd in some columns there are 150+ rows of data

I need to insert Column while at A1,
copy the value of B1 down ( which was previosuly A1, before adding the column) to A2 and copy tthat value down till the end of values in Coilumn B

Need to perform this task till the end of all columns

After that I need to stack all the data from all the columns into column A & B

Column A will be date and Column B willbe the Invoice Numbers.

I will be extremely thankful
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
try this code which uses a couple of arrays to copy the data rather than inserting columns which will be rather slow, Using arrays is very fast:
VBA Code:
Sub test()
Dim outarr()
lr = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
inarr = Range("A1:ADX" & lr)

indi = 1  ' initalise output array index
ReDim outarr(1 To UBound(inarr, 1) * UBound(inarr, 2), 1 To 2) ' redimn output array to accomodate maximum size
For j = 1 To UBound(inarr, 2) ' loop through all columns
  dat = inarr(1, j) ' save the date
  For i = 2 To lr
   If inarr(i, j) <> "" Then
    outarr(indi, 1) = dat
    outarr(indi, 2) = inarr(i, j)
    indi = indi + 1
   Else
    Exit For
   End If
  Next i
Next j
With Worksheets("Sheet2")
 .Range(.Cells(1, 1), .Cells(indi, 2)) = outarr
End With
End Sub
Note I output the resuslt to sheet 2 just for testing , change with statement to the correct sheet
 
Upvote 0
Thanks it works but when consolidating all the columns in column A & B , it is not clearing the contents of the other columns after consolidation.
 
Upvote 0
Just add a line to clear the whole sheet i.e.:

VBA Code:
inarr = Range("A1:ADX" & lr)  ' AFTER this line add:
Range("A1:ADX" & lr)=""   ' add this line
 
Upvote 0
Solution

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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