Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Value in Spreadsheet

Status
Not open for further replies.

jennuhw

MIS
Apr 18, 2001
426
US
I am trying to find a value in column A of a spreadsheet. This is what I have, but it seems to be going through the values of row 1 instead. What am I doing wrong?


Set rng = oExcel.Range("A1:A1000") 'I have tried ("A:A")
For Each range1 in rng

oExcel.Cells(range1).Activate
x = oExcel.ActiveCell.Value
msgbox (x)
Do while oExcel.ActiveCell.Value = var1
'Do Stuff
Loop
Next
 
Here's another bit from the same code I used to give you those other examples:

Code:
varRow = 1
Do While oExcel.Worksheets("Sheet 1").Cells(varRow, 1) > ""
    If oExcel.Worksheets("Sheet 1").Cells(varRow, 1) = var1 Then
        'do your stuff here
        Exit Do
    End If
    varRow = varRow + 1
Loop

If it doesn't find anything, it will set varRow to a blank row and you can happily add new data...(only works if the file doesn't have blank cells between rows or at the beginning of the file) Notorious P.I.G.
 
How do I find out what the value is of what is in oExcel.Worksheets("Sheet 1").Cells(varRow, 1) cell? Everytime I run this it is entering the new data in the first row no matter what the value is in that cell.
 
jennuhw,

I use this in my code:

rows = Actv.UsedRange.rows.Count
cols = Actv.UsedRange.columns.Count
For n = 1 to rows
hdr = Ucase( trim( Actv.Cells(n,1).Value ) )
If val = "xxxxx" then
..........
..........
End If
Next

fengshui_1998
 
jennuhw,

Forgot to add this at the top..

Set Actv = objWkBook.Worksheets(1)

rows = Actv.UsedRange.rows.Count
cols = Actv.UsedRange.columns.Count
For n = 1 to rows
hdr = Ucase( trim( Actv.Cells(n,1).Value ) )
If val = "xxxxx" then
..........
..........
End If
Next


fengshui_1998
 
jennuhw,

Sorry again.

Change "hdr" to "val" (without the quotes)


fengshui_1998
 
Thanks for clearing that up. I was really confused there for a minute. I will try that. Thanks.
 
I had to change a couple of things to suite my code, but it works like a charm! Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top