finding the last used column when it is merged?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi everyone
I need a macro that can message box me the last used column in the active sheet but many of the rows are merged and go past any data, I want to put data down the first column that's not being used but I'm hitting problems as we download the data and they merge so many cells so excel thinks the last column is g for example, because that is the last column with data in it but let's say row 10 is a merge of C10:K10, so I need it to find me K so I know the column I need is L
please help if you can
Tony
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try using this snippet:
Code:
On Error Resume Next
LastC = 1
LastC = Cells.Find(What:="*", After:=Range("A1"), _
    Lookat:=xlPart, LookIn:=xlValues, SearchOrder:=xlByColumns, _
    SearchDirection:=xlPrevious, MatchCase:=False).Column
On Error GoTo 0
LastC should tell you which is the last used column, or 1 if the sheet is empty
 
Upvote 0
@Anthony47 the OP is after the last cell in the merged range when it extends past the last column with data so with the data below the OP is looking for 11 (Column K) as C10:K10 is merged, whereas your code returns 7
so excel thinks the last column is g for example, because that is the last column with data in it but let's say row 10 is a merge of C10:K10, so I need it to find me K so I know the column I need is L
please help if you can



Book1
ABCDEFGHIJKL
1Order IDOrder DateOrder DateSiteSP NameCategory NameShip Via
2105-29-N2129/04/201329/04/2013On SiteLaura CallahanBeveragesRoad
3104-14-F4614/02/201314/02/2013Off SiteMichael SuyamaGrains/CerealsRoad
4109-31-F8731/03/201431/03/2014Off SiteLaura CallahanDairy ProductsAir
5105-30-F2230/04/201330/04/2013Off SiteMargaret PeacockBeveragesRoad
6104-1-F0101/01/201301/01/2013Off SiteNancy DavolioDairy ProductsRoad
7110-1-F6401/05/201401/05/2014Off SiteNancy DavolioConfectionsRail
8106-8-F2508/08/201308/08/2013Off SiteJanet LeverlingProduceAir
9104-1-F0101/01/201301/01/2013Off SiteNancy DavolioGrains/CerealsSea
10104-1-F0201/01/201301/01/2013
11102-19-N6119/07/201219/07/2012On SiteMargaret PeacockBeveragesAir
12108-13-F2913/01/201413/01/2014Off SiteAnne DodsworthBeveragesSea
13104-4-N6404/03/201304/03/2013On SiteMargaret PeacockGrains/CerealsAir
14105-23-N4723/05/201323/05/2013On SiteJanet LeverlingSeafoodAir
Sheet1
 
Upvote 0
@tonywatsonhelp try the code below adapted from some code Chris Neilsen posted at SO.

VBA Code:
Sub testit()
MsgBox MyLastCol(ActiveSheet) 'replace ActiveSheet with Sheets("YourSheetName") if applying to another sheet
End Sub

Function MyLastCol(ws As Worksheet) As Long
    Dim ur As Range
    Dim lastcell As Range
    Dim col As Long
    Dim urCol As Range
    Dim urCell As Range
    
    Set ur = ws.UsedRange
  
    Set lastcell = ws.Cells.Find("*", ws.Cells(1, 1), xlFormulas, , xlByColumns, xlPrevious)
    If Not lastcell Is Nothing Then
    For col = ur.Columns.Count To lastcell.Column - ur.Column + 2 Step -1
        Set urCol = ur.Columns(col)
        If Application.CountA(urCol) > 0 Then
            MyLastCol = urCol.Column
            Exit Function
        End If
        If IsNull(urCol.MergeCells) Then
            For Each urCell In urCol.Cells
                If urCell.MergeCells Then
                    If Not IsEmpty(urCell.MergeArea.Cells(1, 1)) Then
                        MyLastCol = urCol.Column
                        Exit Function
                    End If
                End If
            Next
        End If
    Next
    MyLastCol = lastcell.Column
    Else
    MyLastCol = 1
    End If
End Function
 
Upvote 0
Solution
:mad:
I realize now what the OP meant, thank you
And I realized that my snippet IGNORE merged cells :unsure:
I mean that, using the data in the picture (H7:K7 are merged cells), that code returns 7; or 1 (the default value) if G7 is empty

I am sure your code does the right job!

Have a nice evening
 

Attachments

  • Screenshot 2024-03-12 210910.png
    Screenshot 2024-03-12 210910.png
    6.5 KB · Views: 4
Upvote 0
Thank you for your help,
I hate meged cells as well but these are really bad, but marks vba seams to be working thank you
 
Upvote 0
Would it not be better to replace all merged ranges with center across selection with a piece of code?
Code:
Sub Change_Merged_To_Center_Across()
Dim c As Range
For Each c In ActiveSheet.UsedRange
With c
 If .MergeCells Then
    With .MergeArea
        .UnMerge
        .HorizontalAlignment = xlCenterAcrossSelection
    End With
 End If
End With
Next c
End Sub
I know it has the dreaded Loop but with today's machines that should not be a problem if the UsedRange is within limits.
I have not tested it for LastColumn results but I will after I get back later tonight if someone else has not done it
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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