Looping/VBA Questions

Mikeymike15

New Member
Joined
Jul 20, 2006
Messages
33
Hi All,

Getting back into VBA and I'm definitely rusty.

Here is my situation and I hope someone can get me started

I have 2 Rows of Data. For each instance of an occurance in row B, I need the corresponding value in A to be placed in Row D.

6801 ATL
6802 BOS
6803 BOM
6804 CVG
6805 MNL
6806 NRT
6807 PDX
6808 ATL
6809 PDX
6810 BOS
6811 ATL

So for if I was selecting ATL,

Row D would look like this

D
6801
6808
6811

Anyone can help get me started. My mind is numb on trying to figure out the best way to handle the looping, dims, etc

Thanks,
Mike the rookie
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I have 2 Rows of Data. For each instance of an occurance in row B, ...
You're also a little rusty on the terminology.
From what you have shown you appear to have two Columns of data, ;)


So for if I was selecting ATL,
Exactly what do you mean by "if I was selecting ATL"?
Are you trying to list all the column B values and all their corresponding column A values?
Or are you wanting to list only the values for the active cell if a cell in column B is the active cell?
Or something else?
 
Upvote 0
Hi All,

Getting back into VBA and I'm definitely rusty.

Here is my situation and I hope someone can get me started

I have 2 Rows of Data. For each instance of an occurance in row B, I need the corresponding value in A to be placed in Row D.

6801 ATL
6802 BOS
6803 BOM
6804 CVG
6805 MNL
6806 NRT
6807 PDX
6808 ATL
6809 PDX
6810 BOS
6811 ATL

So for if I was selecting ATL,

Row D would look like this

D
6801
6808
6811

Anyone can help get me started. My mind is numb on trying to figure out the best way to handle the looping, dims, etc

Thanks,
Mike the rookie
Mike,

If your data are in columns A and B, does running this macro give you anything like what you want?
Code:
Sub tryout()

Dim a(), b()
Dim c As Object, d, e
Dim i&, n&

Set c = CreateObject("scripting.dictionary")
e = Range("A1").CurrentRegion.Resize(, 2)
n = UBound(e, 1)

For i = 1 To n
    d = e(i, 2)
    If Not c.Exists(d) Then
        c(d) = c.Count + 1
        ReDim Preserve b(1 To c(d))
        ReDim Preserve a(1 To n, 1 To c(d))
        b(c(d)) = 2
        a(1, c(d)) = e(i, 2)
        a(2, c(d)) = e(i, 1)
    Else
        b(c(d)) = b(c(d)) + 1
        a(b(c(d)), c(d)) = e(i, 1)
    End If
Next i

Range("D1").Resize(n, c.Count) = a

End Sub
 
Upvote 0
Thank you very much mirabeau!

Yes this did the trick.

Ultimately, I will have a fixed header and will need to add a second search criteria, but I think I can follow your example and go from there.

Mike,

If your data are in columns A and B, does running this macro give you anything like what you want?
Code:
Sub tryout()

Dim a(), b()
Dim c As Object, d, e
Dim i&, n&

Set c = CreateObject("scripting.dictionary")
e = Range("A1").CurrentRegion.Resize(, 2)
n = UBound(e, 1)

For i = 1 To n
    d = e(i, 2)
    If Not c.Exists(d) Then
        c(d) = c.Count + 1
        ReDim Preserve b(1 To c(d))
        ReDim Preserve a(1 To n, 1 To c(d))
        b(c(d)) = 2
        a(1, c(d)) = e(i, 2)
        a(2, c(d)) = e(i, 1)
    Else
        b(c(d)) = b(c(d)) + 1
        a(b(c(d)), c(d)) = e(i, 1)
    End If
Next i

Range("D1").Resize(n, c.Count) = a

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,550
Messages
6,114,265
Members
448,558
Latest member
aivin

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