Replace Range by Cells

jocker_boy

Board Regular
Joined
Feb 5, 2015
Messages
83
Hello,

I'm trying to create a column group based on my cells that are empty in row 4.

I have this code working perfect:

VBA Code:
Sub MakeGroups()
    Dim rA As Range

    For Each rA In Range("ED4", Range("ED4").End(xlToRight)).SpecialCells(xlCellTypeBlanks)
        rA.EntireColumn.Group
    Next rA
End Sub

But my column name is variable, so i would like to replace with this:

VBA Code:
Sub MakeGroups()
    Dim rA As Range
    Dim lc As Long
      
'Find last column in row 1 with data
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
 
    For Each rA In Range(Cells(4, lc + 1), Range(Cells(4, lc + 1)).End(xlToRight)).SpecialCells(xlCellTypeBlanks)
        rA.EntireColumn.Group
    Next rA
End Sub

But it doesn't work.

Thanks for all the help.
Gonçalo
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I don't understand.
If you are starting from the last populated column in a row, moving to the right will not find anything, it will simply go to the last possible column on the worksheet .

Maybe you could show us an example (with data) of what you are hoping to accomplish here.
 
Upvote 0
I don't understand.
If you are starting from the last populated column in a row, moving to the right will not find anything, it will simply go to the last possible column on the worksheet .

Maybe you could show us an example (with data) of what you are hoping to accomplish here.

Header1Header2
xxxx
xxxx
xxxx
xxxxxx
xxxxxxxxxxxx

In this example i need to group columns 3 to 5. because in row 4 they are empty.
The variable is my number of columns with header. In this example i have 2 columns but i can have 20 or more.
And also the number of columns to group are variable.

My goal is to check what is the last column with data in row1 - variable "lc"
Go to row 4 and start after "lc" and finish until have data in row 4.

Thanks
 
Last edited:
Upvote 0
In this example i need to group columns 3 to 5. because in row 4 they are empty.
I don't see how you arrive there from your example.

The last column with data in row 1 is column 2.
The last column with data in row 4 is still column 2.
The last column with data in row 5 is column 6.

So how do you get that columns 3-5 need to be grouped for row 4?
 
Upvote 0
I don't see how you arrive there from your example.

The last column with data in row 1 is column 2.
The last column with data in row 4 is still column 2.
The last column with data in row 5 is column 6.

So how do you get that columns 3-5 need to be grouped for row 4?

Header1Header2
xxxx
xxxx
xxxx
xxxxDATA
xxxxxxxxxxxx

The last column with data in row 4 is column 6.
Sorry for this confusion.

In this example, i can use this code and works perfect.

VBA Code:
Sub MakeGroups()
    Dim rA As Range

    For Each rA In Range("C4", Range("C4").End(xlToRight)).SpecialCells(xlCellTypeBlanks)
        rA.EntireColumn.Group
    Next rA
End Sub

But column "C" is variable. It is always the last column with data in row 1 plus 1.
 
Last edited:
Upvote 0
OK, see if this gives you what you are looking for:
VBA Code:
Sub MakeGroups()

    Dim rA As Range
    Dim lc1 As Long
    Dim lc4 As Long
    
'   Find last column with data in row 1
    lc1 = Cells(1, Columns.Count).End(xlToLeft).Column
    
'   Find last column with data in row 4
    lc4 = Cells(4, Columns.Count).End(xlToLeft).Column

    For Each rA In Range(Cells(4, lc1 + 1), Cells(4, lc4 - 1)).SpecialCells(xlCellTypeBlanks)
        rA.EntireColumn.Group
    Next rA
    
End Sub
 
Upvote 0
Solution
OK, see if this gives you what you are looking for:
VBA Code:
Sub MakeGroups()

    Dim rA As Range
    Dim lc1 As Long
    Dim lc4 As Long
   
'   Find last column with data in row 1
    lc1 = Cells(1, Columns.Count).End(xlToLeft).Column
   
'   Find last column with data in row 4
    lc4 = Cells(4, Columns.Count).End(xlToLeft).Column

    For Each rA In Range(Cells(4, lc1 + 1), Cells(4, lc4 - 1)).SpecialCells(xlCellTypeBlanks)
        rA.EntireColumn.Group
    Next rA
   
End Sub

Thanks you very much.
It's perfect.
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top