View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
CompleteNewb CompleteNewb is offline
external usenet poster
 
Posts: 12
Default One for JMB, DE-concatenating, but keeping relationship

I started a new post because the one JMB replied to worked, but now there's
a twist, and that post was long enough ago that I figured some newsreaders
wouldn't show this as new. Sorry if that is a bad way to approach this.

So, initially (problem solved) I was trying to re-populate some rows that
turned comma-separated lists in Column B into separate values, one per row,
while repeating (and possibly separating comma-separated lists) value(s) in
Column A associated with the original list in B. Thus, this:

Column A Column B
Dodge,Plymouth Trucks,Cars,Scooters
Buick Cars
GM,Ford Trucks,Cars

was turned into this:

Column A Column B
Dodge Trucks
Dodge Cars
Dodge Scooters
Plymouth Trucks
Plymouth Cars
Plymouth Scooters
Buick Cars
GM Trucks
GM Cars
Ford Trucks
Ford Cars

JMB's VBA (copied below) worked PERFECTLY. However, now there's a twist
(simpler in one way, but more complicated in another). After performing the
above, I now have added a third column (Column C) which has yet another
comma-separated list related to Column A and Column B. So now I have this
in Sheet1:

Column A Column B Column C
Dodge Trucks Recall,Discontinued
Dodge Cars Recall
Dodge Scooters Discontinued,New
*Ford Recall, New

*I occasionally have blanks in Column B or Column C, which the initial VBA
didn't take into account, but I figured out how to handle it, but now I
can't figure out how to handle it with the new situation.

And I basically need to do the same thing, except maintaining the
relationship in TWO columns, not just one. So in Sheet2 I need:

Column A Column B Column C
Dodge Trucks Recall
Dodge Trucks Discontinued
Dodge Cars Recall
Dodge Scooters Discontinued
Dodge Scooters New
Ford Recall
Ford New

I tried several different permutations of what seemed to me to be intuitive
modifications to JMB's code (ie. changing the range references, adding a 3rd
var (var3 as Variant) and a 3rd lngCount (lngCount3 as Long), and
extrapolating out through the rest of the code, taking the new variables
into account, adding the appropriate lines to the "For Each" blocks and the
"trim" functions, but the logic in my head is just not matching what the VBA
wants or does. Maybe it's because I don't have comma-separated lists in
Column A or Column B anymore? It's very frustrating to me, because I like
to think that I can usually analyze some code and modify it to accomplish
new tasks, but it's just not working for me lately. I don't know if that
means the code is getting more complicated or I'm losing brain cells.

JMB's initial VBA (which, again, worked absolutely perfectly) accomplished
what I was trying to do with 2 columns, with the possible comma-separated
lists in Column A and/or Column B. But now I need to do the same thing with
a THIRD column, repeating the related values in Column A and Column B (which
are now single values per cell, only Column C has the comma-separated list)
for every value separated by commas in Column C (but also handling blanks in
Column B or Column C). JMB's code for the 2 columns was:

Sub test()
Dim rngData As Range
Dim rngRow As Range
Dim rngDest As Range
Dim var1 As Variant
Dim var2 As Variant
Dim lngCount1 As Long
Dim lngCount2 As Long
Dim TotalCount As Long


Set rngData = Worksheets("Sheet1").Range("A1:B3") '<<CHANGE
Set rngDest = Worksheets("Sheet2").Range("A1") '<<CHANGE

For Each rngRow In rngData.Rows
var1 = Split(rngRow.Cells(1).Value, _
",", -1, vbTextCompare)
var2 = Split(rngRow.Cells(2).Value, _
",", -1, vbTextCompare)
For lngCount1 = LBound(var1) To UBound(var1)
For lngCount2 = LBound(var2) To UBound(var2)
rngDest.Offset(TotalCount, 0).Value = _
Trim(var1(lngCount1))
rngDest.Offset(TotalCount, 1).Value = _
Trim(var2(lngCount2))
TotalCount = TotalCount + 1
Next lngCount2
Next lngCount1
Next rngRow


End Sub

If anyone can help me with this, I'd appreciate it. And if you happen to
know why my code modifications don't work anymore, feel free to suggest some
type of brain food or a place that covers how to add (what I assume to be)
minor complications to an existing solution. I mean, seriously, what's
going on with me? And why are my posts so long?

As an aside, if this would be easier to do in Access, any suggestions there
are welcome also. I've always thought myself to be better with Access than
with Excel, and yet I couldn't wrap my brain around this using the query
builder either. It seems like every time I DON'T want Field1 (the Access
equivalent of Excel's Column A) to repeat values, it happens, and then when
I want to do a Make Table query that DOES repeat Field1 for every related
value in Field2, then I can't make it work. I mean, sheesh.

Thanks for reading, and thanks to those (like JMB and Mr. Ogilvy and all of
you out there) who have already helped me (and those like me) save hours and
hours of repetetive tasks and failed experimentation.

The Complete Newb