Checking sheet for values and capturing column name
JE:
Thanks for offering that VBA. From the looks of it, this would actually
address the issue with multiple identical column header entries (like if a
value from Sheet1 happens to be in A5, A10, A122, etc.). However, when I
try it, it just goes to the msgbox saying no unique values found, which I
know can't be the case. I made sure the sheet titles are correct; do you
know why the check returns the msgbox?
Thanks again, your help is very much appreciated.
"JE McGimpsey" wrote in message
...
One way:
Public Sub ListUniqueHeaders()
Dim rCell As Range
Dim rUnique As Range
Dim rFound As Range
Dim sColumnHeaders As String
Dim sFirstAddress As String
With Worksheets("Sheet1")
Set rUnique = .Cells.Find( _
What:="Unique", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
Lookat:=xlWhole, _
MatchCase:=False)
If rUnique Is Nothing Then
MsgBox "'Unique' column not found)"
Exit Sub
Else
Set rUnique = .Range(rUnique.Offset(1, 0), _
.Cells(.Rows.Count, rUnique.Column).End(xlUp))
If rUnique.Cells.Count = 1 Then
MsgBox "No values in 'Unique' column"
Exit Sub
End If
End If
End With
With Sheets("Sheet2")
For Each rCell In rUnique
Set rFound = .Cells.Find( _
What:=rCell.Text, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
Lookat:=xlPart, _
MatchCase:=False)
If Not rFound Is Nothing Then
sFirstAddress = rFound.Address
Do
sColumnHeaders = sColumnHeaders & "," & _
rFound.EntireColumn.Cells(1).Text
Set rFound = .Cells.FindNext(After:=rFound)
Loop Until rFound.Address = sFirstAddress
rCell.Offset(0, 1).Value = Mid(sColumnHeaders, 2)
sColumnHeaders = vbNullString
Set rFound = Nothing
End If
Next rCell
End With
End Sub
In article ,
"Complete Newb" wrote:
It's weird that every time I think I'm going to know how to do something,
it
winds up I get 1/4 or 1/2 way there and then stuck.
On Sheet1 I have one column of text values (about 500) in Column A (w/
header of "Unique"). On Sheet2 there are many columns and many rows,
with
all kinds of values everywhere. What I need to do is:
Run through Sheet2, and for every occurrence of a value in Sheet1's
"Unique"
column, enter the column heading(s) of the column(s) that value is in in
Column 2 of Sheet1 next to that value.
For instance, Sheet1's "Unique" column has:
Gear
Tranny
Door
On Sheet2:
- The value "Gear" is located in A5 (Column heading of "Parts"), E114
(Column heading of "Labor"), and G55 (Column heading of "Misc").
- The value "Tranny" is in B45 (Column heading of "Traps")
- The value "Door" is in A88 (Column heading of "Parts") and E6 (Column
heading of "Labor").
So, after I run a sub procedure, Sheet1 should show the following:
Gear Parts,Labor,Misc
Tranny Traps
Door Parts, Labor
I don't care if it's easier to put each column header instance in a
separate
column on Sheet1 (instead of values separated by commas), because I can
combine them as a separate step. Also, if getting the actual column
header
value is a big complication, I don't mind just returning the column
letters
and then I can run a sub or use a formula to change column letters to
their
respective header names as a separate step also.
Can anyone help me figure out how to do this?
Any help is greatly appreciated, and thanks for reading.
|