question on macro

H

Harry

Hi,

I have a spreadsheet at hand that is an output from a lab computer. Until
now, I had to do some simple calculations with excel on this sheet. But in
future I want to do more. For this I need a simple (I think) solution for a
problem. But it seems I cannot find how to do that :)

In essence it comes down to this:

-I have a workbook with two sheets.
-In sheet one is the output file (xls) from a laboratory machine. This
consists mainly of multiple colums with each a unique text header in the
top row. Each column has a variable number of cells depending on what
analysis is done by the lab machine. It can be 10 cells or 100 cells under
each header. In practice this means that sheet one can consist of anything
from 2 to 400 rows with all numbers possible in between.
-In sheet two I have a list in one column of all possible column headers
that could be found in sheet 1 (this is made up by an total output list from
the lab machine).
Note that not from every analysis, all header names are put in sheet one.
That depends on the analysis again.
So the total list on sheet two can be 200 names long while the output from
the
labmachine to sheet one could consist of e.g. only of these 20 headers with
a variable
number of cells under each header.
-I want a macro that takes every name from sheet two and checks if it is in
the current header row of sheet one. IF SO, I want the macro to select the
cells under that column header for as far the column is filled with values.
-With this selection made by the macro, I than want to perform another
action (maybe copying it to another section of the workbook or maybe give
the selected range a name - but that is something I have to look at later).

Anybody able to help?

Regards,
Harry
 
G

Guest

One way to find the range(s). It just gives a message box with the range
address -change when you decide what you want to do with it. Note I'm using
the codenames for the worksheets (check VBA help for more info). I'm
assuming your list is on Sheet2 Column A and the headers of the imported data
is on Shee1, Row1. Hope it helps.

Sub test()
Dim x As Range
Dim FoundCell As Range

With Sheet2
For Each x In .Range("A1", _
.Cells(.Rows.Count, 1).End(xlUp))
With Sheet1.Rows("1:1")
Set FoundCell = .Find(what:=x.Value, _
after:=.Cells(1, 1), LookIn:=xlValues, _
lookat:=xlWhole, searchorder:=xlByRows, _
searchdirection:=xlNext, MatchCase:=False, _
matchbyte:=False)
If Not FoundCell Is Nothing Then
With .Parent
MsgBox .Range(FoundCell, .Cells(.Rows.Count, _
FoundCell.Column).End(xlUp)).Address
End With
Else: MsgBox "Not Found"
End If
End With
Next x
End With

End Sub
 
H

Harry

JMB,

Thx alot for the thinking! Great!
I have to study this macro of yours. And I will try to adopt in in my
workbook. And I will try to let you know if it worked.

Regards
Harry
 
H

Harry

JMB,

I managed to get this macro working ones. It was giving me all ranges in a
message box.
But after that first time I keep getting an error message when it reaches
"For Each x In .Range".
The error says something like "Error 424 during execution, object needed"

The complete test of the macro in my actual workbook reads :

Sub test()
Dim x As Range
Dim FoundCell As Range

With Blad2
For Each x In .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
With Blad1.Rows("1:1")
Set FoundCell = .Find(what:=x.Value, after:=.Cells(1, 1),
LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows,
searchdirection:=xlNext, MatchCase:=False, matchbyte:=False)
If Not FoundCell Is Nothing Then
With .Parent
MsgBox .Range(FoundCell, .Cells(.Rows.Count,
FoundCell.Column).End(xlUp)).Address
End With
Else: MsgBox "Not Found"
End If
End With
Next x
End With

End Sub

The only thing altered was that in my Dutch excel, a sheet is called "Blad".
Any idea why it worked once and not again????

Harry
 
H

Harry

btw,
in my email below, the underscores miss, but in excel i have the linebreaks
correct....
 
H

Harry

JMB,

Thx again.

I made a mistake with the "Sheet1" / "Blad1" naming. That is why it did not
work after the first use. That is fixed now.

Now I am looking at the part with the output to the message box. That I want
to change in something that pastes every single selected range in a new
"Sheet3" and preparing new columns from left to right with those ranges..
Will have to figure that out. Maybe you can help again ;-)

Next, I want a good tutorial on the web for VBA/excel. Most that I did find
with google seem to be very superficial. And I need more like the macro you
did give me.
Any tips for that?

Harry
 
G

Guest

Using the same code I posted earlier (so you'll need to change references as
needed). I'm assuming you want Sheet3 cleared before copying everthing over.
If that is not the case, delete the statement that clears Sheet3. The
changes are and IF/THEN/ELSE statement after the With .Parent line, and I
deleted the Else:Msgbox "Not Found" line (figured you probably don't need,
but if you want to do something different, you can leave the else in and do
something different than a msgbox).

This forum is a good source for learning new things, you can also try

mvps.org
xldynamic.com
mcgimpsey.com

Many include their website links w/their posts.

Sub test()
Dim x As Range
Dim FoundCell As Range

Sheet3.Cells.Clear

With Sheet2
For Each x In .Range("A1", _
.Cells(.Rows.Count, 1).End(xlUp))
With Sheet1.Rows("1:1")
Set FoundCell = .Find(what:=x.Value, _
after:=.Cells(1, 1), LookIn:=xlValues, _
lookat:=xlWhole, searchorder:=xlByRows, _
searchdirection:=xlNext, MatchCase:=False, _
matchbyte:=False)
If Not FoundCell Is Nothing Then
With .Parent
If IsEmpty(Sheet3.Cells(1, 1)) Then
.Range(FoundCell, .Cells(.Rows.Count, _
FoundCell.Column).End(xlUp)).Copy _
Sheet3.Cells(1, 1)
Else: .Range(FoundCell, .Cells(.Rows.Count, _
FoundCell.Column).End(xlUp)).Copy _
Sheet3.Cells(1, Sheet3.Columns.Count).End(xlToLeft)(1, 2)
End If
End With
End If
End With
Next x
End With

End Sub
 
H

Harry

Thanks again from Holland JMB!!!!

I will study your code and let you know if it worked! Looks good however!

Harry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top