Please I Need Help In Modifying Some Code

woodpecker2

New Member
Joined
Aug 2, 2007
Messages
33
I currently have data in the format of a table with row one as Origin and the first column as Destination, the data inbetween are the volumes.

Using some code I previously found I can take the data from the table and create a list. Basically it takes the Destinatin and Origin and where they intersect the value.

This is the code I'm currently using:-

Code:
Private Sub CommandButton1_Click()
Dim a, b(), i As Long
a = Sheets("sheet1").Range("a1").CurrentRegion.Value
ReDim b(1 To (UBound(a, 1) - 1) * (UBound(a, 2) - 1), 1 To 3)
For i = 2 To UBound(a, 2)
    For ii = 2 To UBound(a, 1)
        n = n + 1
        b(n, 1) = a(ii, 1): b(n, 2) = a(1, i)
        b(n, 3) = a(ii, i)
        If IsEmpty(a(ii, i)) Then b(n, 3) = 0
    Next
Next
With Sheets("sheet2").Range("a1")
    .Cells.Clear
    .Resize(, 3) = Array("Destination", "Origin", "Amount")
    .Offset(1).Resize(UBound(b, 1), 3) = b
End With
Erase a, b
End Sub

This where I now need some help. I need to add an additional column so that not only does it pick up row one and column A, I also need column B to be shown as well.

Current Data Table:
TestThree.xls
ABCDEFGHI
1CostingsABCDEFGH
2AA9.280.030.000.280.000.130.040.07
3BB0.163.620.000.360.000.611.090.05
4CC0.040.040.000.270.000.850.030.04
5DD0.230.120.000.350.000.240.240.09
Sheet1


Current Result
TestThree.xls
ABCD
1DestinationOriginAmount
2AAA9.28
3BBA0.16
4CCA0.04
5DDA0.23
6AAB0.03
7BBB3.62
8CCB0.04
9DDB0.12
10AAC0.00
11BBC0.00
12CCC0.00
13DDC0.00
14AAD0.28
15BBD0.36
16CCD0.27
17DDD0.35
Sheet2


I'll show what I'm after in a minute.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
This is what the new table will be like:-
TestThree.xls
ABCDEFGHIJ
1CostingsABCDEFGH
2AAMon9.280.030.000.280.000.130.040.07
3BBTue0.163.620.000.360.000.611.090.05
4CCWed0.040.040.000.270.000.850.030.04
5DDThu0.230.120.000.350.000.240.240.09
Sheet1


This is the result I'm after:-
TestThree.xls
ABCD
1DestinationOriginAmountFilter
2AAA9.28Mon
3BBA0.16Tue
4CCA0.04Wed
5DDA0.23Thu
6AAB0.03Mon
7BBB3.62Tue
8CCB0.04Wed
9DDB0.12Thu
10AAC0.00Mon
11BBC0.00Tue
12CCC0.00Wed
13DDC0.00Thu
14AAD0.28Mon
15BBD0.36Tue
16CCD0.27Wed
17DDD0.35Thu
Sheet2


Please note the actual table I will be using is much greater and this is only a small sample.
 
Upvote 0
Try this:
Code:
Private Sub CommandButton1_Clickx()
Dim a, b(), i As Long
a = Sheets("sheet1").Range("a1").CurrentRegion.Value
ReDim b(1 To (UBound(a, 1) - 1) * (UBound(a, 2) - 1), 1 To 4)
For i = 3 To UBound(a, 2)
    For ii = 2 To UBound(a, 1)
        n = n + 1
        b(n, 1) = a(ii, 1): b(n, 2) = a(1, i)
        b(n, 3) = a(ii, i): b(n, 4) = a(ii, 2)
        If IsEmpty(a(ii, i)) Then b(n, 3) = 0
    Next
Next
With Sheets("sheet2").Range("a1")
    .Cells.Clear
    .Resize(, 4) = Array("Destination", "Origin", "Amount", "Filter")
    .Offset(1).Resize(UBound(b, 1), 4) = b
End With
Erase a, b
End Sub
 
Upvote 0
Thanks that works fine.

I'm trying to understand the code but am having trouble fathoming it out.

If I wanted to swap column A and B around, which part of the code would need amending as I wish to see the same results?
 
Upvote 0
Thanks that works fine.

I'm trying to understand the code but am having trouble fathoming it out.

If I wanted to swap column A and B around, which part of the code would need amending as I wish to see the same results?

It wasn't my code. Looks like one of Jindon's.

However, try this:
Code:
Private Sub CommandButton1_Clickxx()
Dim a, b(), i As Long
a = Sheets("sheet1").Range("a1").CurrentRegion.Value
ReDim b(1 To (UBound(a, 1) - 1) * (UBound(a, 2) - 1), 1 To 4)
For i = 3 To UBound(a, 2)
    For ii = 2 To UBound(a, 1)
        n = n + 1
        b(n, 1) = a(ii, 2): b(n, 2) = a(1, i)
        b(n, 3) = a(ii, i): b(n, 4) = a(ii, 1)
        If IsEmpty(a(ii, i)) Then b(n, 3) = 0
    Next
Next
With Sheets("sheet2").Range("a1")
    .Cells.Clear
    .Resize(, 4) = Array("Destination", "Origin", "Amount", "Filter")
    .Offset(1).Resize(UBound(b, 1), 4) = b
End With
Erase a, b
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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