• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

select range in columns

hello,

can someone tell me how i can search in a column range.
I can make it work for the rows, but not for the colums

thanks!
WIm

Code:
Public Sub Group()
 
Dim nColumn As Long
Dim nStart As Long, nEnd As Long
Dim i As Integer
 
i = 1
 
Do
 
For nColumn = A To AAA
If Range(nColumn & "4").Value = i Then
nStart = nColumn
Exit For
End If
Next nColumn
 
For nColumn = nStart To AAA
If Range(nColumn & "4").Value <> i Then
nEnd = nRow
Exit For
End If
Next nColumn
nEnd = nEnd - 1
 
Columns(nStart & ":" & nEnd).Select
Selection.Columns.Group
 
i = i + 1
 
Loop Until i = 52
  
End Sub
 
Hi Wim,

If you want to use a Long variable to loop through the columns then you need to use column numbers rather than column letters:

Code:
    For nColumn = 1 To 703
        If Cells(4, nColumn).Value = i Then

If you want to use a Range object to loop through the columns then you can use a For Each...Next loop:

Code:
    Dim rngCell As Range
   
    For Each rngCell In Range("A4:AAA4").Cells
        If rngCell.Value = i Then

        'etc...
        End If
    Next rngCell
 
Hi Colin,

Thanks for the reply.

But can you tell me how I can select the colums of those cells.
I need to select them in order to group them

Code:
Columns(nStart & ":" & nEnd).Select
Selection.Columns.Group
 
Hi,

There are various syntaxes/properties you could use; here's one of them:
Code:
Range(Cells(4, nStart), Cells(4, nEnd)).Group
 
Back
Top