I am trying to check if a value is in a certain range. If that value appears, the corresponding data to that enty is copied to another sheet. The trick is it has to be dynamically determined because the column size varies from input sheet to input sheet. In Java the hasNext()
function can be used. I think that VBA's most similar function would be Sheet1.Column.End(xlup)
. How is the best way to determine the end of a column for the test condition of a do while loop in vba?
Pseudo example:
'This is part of a nested loop, this segment traverses the column looking for 'specified data.
Do While (!Sheets(inputSheetName).SyntaxToDetermineEndOfColumn))
If(someCell = someValue)
Copy values from the corresponding row to fields in newSheet
End If
Next r 'This increments the row within the current column
Loop
Next c 'This increments to the next column of data
There are two parts to your question:
The first part about finding the last used row is easialy found with a quick Google: Error in finding last used cell in VBA
To loop from start to end of column, use this:
Say we have data like:
We want to find happiness in the first two columns and retrieve the column C value in that row:
You might find this useful: http://support.microsoft.com/kb/830287
But what I personally do in this situation involves a bit more code, but is flexible and fast. First create a class and call it "RangeInfo". Then past this:
Then for your code, use this: