views:

265

answers:

5

alt text

Given the Image... If I know that there is some data starting at Range("B3").
How can I find the cells with contiguous data that is till cell E3? Since F3 is blank G3 onwards should not be considered. The result could either be a range object (B3:E3) or count of cells( 4 in this case).

By setting B3 as the Active cell and doing..

Range(ActiveCell, ActiveCell.End(xlToRight).Count

I do get the count, however this method is not reliable, in case only B3 has data it counts the cells till the end of the sheet.
Of course this could also be achieved by looping through the cells but I'd rather use a Worksheet Function or some other efficient method.

+1  A: 

You could use the CurrentRegion property. This returns the range that is contiguous to the specified range. So...

Range("B3").CurrentRegion returns the range B3:E3
Range("B3").CurrentRegion.Columns.Count returns 4
Range("B3").CurrentRegion.Cells.Count also returns 4

However, if you had data in rows 4 and below (let's say you had data in B4:E6), then you would get these results

Range("B3").CurrentRegion returns the range B3:E6
Range("B3").CurrentRegion.Columns.Count returns 4
Range("B3").CurrentRegion.Cells.Count returns 16

Is this what you were after?

Nice idea but I might have data on the rows above and below the said row so CurrentRegion may not return the right values.
Kevin Boyd
Would CurrentRegion.Columns.Count not work for you?
I think CurrentRegion always returns the bounding area which is >= the used area. Therefore, given he has column headings, CurrentRegion.Columns would always return 4.
Ryan Shannon
In his original question, he says the result could be a range or a count. I thought he wanted a count of 4 in this instance, which would give him the number of columns in the currentregion around B3. Perhaps Kevin could clarify?
@dendarii: Well to your 1st question, I really don't know I thought that current region requires a cell blank area on all sides of the data to work well. But in my case they might be data in the row starting at B2 and even at B1 and also at B4 and B5 which I have not shown in the image. so really don't know it CurrentRegion will work out, will check out you suggesion though
Kevin Boyd
@Ryan Shannon: Well I have answered to dendarii and hope that clarifies your question, I just checked out regarding Columns.Count and if there is some data above the row B3 or below it and it extends more than the column headers then CurrentRegion even selects them.
Kevin Boyd
@dendarii: to your 2nd question, well I could have data from cell B2 onwards it could extend to 2 cells,3 cells on more which dont know at compile time but I know for sure that the data starts at B2. now given that info the program should be able to decipher the num of columns the data extends to before it encounters a blank cell.
Kevin Boyd
+3  A: 

It seems that you are trying to determine the number of continuous columns used by in a row, starting from cell B3.

The code below will return the values of $B$3:$E$3 and 4 based on your data. If only cell B3 has data, it will return $B$3 and 1.

Sub GetDataArea()

Dim strCellToTest As String
Dim rngMyRange As Range
Dim lngColumns As Long

strCellToTest = "B3"

lngColumns = ActiveWorkbook.ActiveSheet.Range("" & strCellToTest).End(xlToRight).Column - 1

If lngColumns >= 256 Then
 Set rngMyRange = ActiveWorkbook.ActiveSheet.Range("" & strCellToTest)
 lngColumns = 1
Else
 Set rngMyRange = ActiveWorkbook.ActiveSheet.Range _
 (strCellToTest & ":" & Range("" & strCellToTest).Offset(0, lngColumns - 1).Address)
End If

MsgBox "Columns: " & lngColumns & vbCr & vbLf & "Range: " & rngMyRange.Address

End Sub
Robert Mearns
Seems to be good! Let me check that out.
Kevin Boyd
+1  A: 

Intersect(Activecell.CurrentRegion, ActiveCell.EntireRow)

Will return B3:E3. Alternatively

If IsEmpty(ActiveCell.Offset(0,1).Value) Then
   Set rMyRange = ActiveCell
Else
   Set rMyRange = ActiveCell.Parent.Range(ActiveCell, ActiveCell.End(xlToRight))
End If

rMyRange will also return B3:E3

Dick Kusleika
The second solution seems to be good! Again let me check up on that!
Kevin Boyd
A: 

Depending on how general you need to get, it could be as simple as

Application.WorksheetFunction.Count([b4:e4])

If you want to tie in the ActiveCell, try

Application.WorksheetFunction.Count(intersect(activecell.CurrentRegion, activecell.EntireRow))
Ryan Shannon
WorksheetFunction.count will not work as I don't know the parameter e4 at compile time, check my replies to dendarii.Let me have a look at the other one.
Kevin Boyd
+1  A: 

I like to use a function that counts columns that contain values until it encounters an empty cell. The return value can be used to set up a FOR NEXT loop to churn through a table. Here is how I would do it:

Sub tester()
    Dim Answer
    Answer = CountColumns(3, 2)
    MsgBox "There are " & Answer & " columns."
 End Sub
Public Function CountColumns(ByVal startRow As Integer, ByVal startColumn As Integer)
    'Pass starting location in spreadsheet for function to loop through until
    'empty cell is found. Return count of columns function loops through

     Do While ActiveSheet.Cells(startRow, startColumn).Value <> ""
        startColumn = startColumn + 1
     Loop
     startColumn = startColumn - 1
     CountColumns = startColumn
 End Function
Krashman5k