Print from Airliners.net discussion forum http://www.airliners.net/aviation-forums/non_aviation/read.main/2426136/ |
Topic: Excel VBA Question (yes HERE, Why Not...) Username: Airstud Posted 2012-05-20 02:48:59 and read 778 times.I have posted about this in a VBA forum but have not gotten a response and I am an impatient dude so why not go ahead and post it ici:
See, I do not have much experience with the VBA, but I want to write a program part of which will locate a certain row or rows in a spreadsheet that contain certain data in Column X (like "Henson" or "Nguyen" or "Hutchens"); then extract other data from a couple other columns in that row and spit it into a different spreadsheet (and then do the same thing until it's hit all the criterion-satisfying rows).
It seems that the Range.Find method is the way to do this. Am I wrong? VBA's documamentation says that the return value for this method is "A Range object that represents the first cell where that information is found."
When I run this code:
Resulticus = Range("X:X").Find("Henson")
MsgBox (Resulticus)
The MsgBox says "Henson Stewart". Which is indeed the content of the cell I expected to be found, but "Henson Stewart" is a string o' text. I want a Range object a)because consarn it, that's what the documamentation said I'd get; and 2)because I need to get the row number of this cell so I can copy the other four cells' worth of data out of that row, stick it into my other spreadsheet, and allow my for-next loop to go merrily along.
Does Range.Find actually return a object? When I commented out the MsgBox and used either "Resulticus.Select" or "Resulticus.Activate" I got a runtime error 424 "Object required." So I went into the General/Declarations section of the code editor and did a "Dim Resulticus as Object", and then I got runtime error 91 "Object variable or With block variable not set"
Wha goin' on is...how do I get this Range.Find method to work? |
Topic: RE: Excel VBA Question (yes HERE, Why Not...) Username: dfwrevolution Posted 2012-05-20 08:15:44 and read 738 times.If there is nothing else you are trying to automate with the VBA code, would a pivot table get the job done? I try to avoid VBA like the plague. |
Topic: RE: Excel VBA Question (yes HERE, Why Not...) Username: Goldenshield Posted 2012-05-20 11:44:56 and read 705 times.VBA is based on VB6, which is NOT object oriented in how we know it via .NET, although it does contain objects.
Anyhow, what you are doing is using the default action of the Range type, which is .text, and that's not what you want. You need to get its address.
This is what you need to do:
Quote:
'Define the range object
Dim c As Range
'get the first instance of the what we are searching for
c = Sheet1.Range("a1:a500").Find("test")
'test if anything was returned
If Not c Is Nothing Then
'Get the row and column where the search passed
Dim Row As Integer
Dim Col As Integer
Row = c.Range.Row
Col = c.Range.Column
MsgBox ("The row you want is: " & Row)
End If
|
|
The messages in this discussion express the views of the author of the message, not necessarily the views of Airliners.net or any entity associated with Airliners.net.Copyright © Lundgren Aerospace. All rights reserved. http://www.airliners.net/ |