For the millionth time... Trying everthing I can think of the get the last occupied column in a row using a cell in the row.

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
855
Office Version
  1. 365
Platform
  1. Windows
Oy! I just cannot get the syntax correct to find the last occupied column in a row where the row is the row that a specified cell is located in. What simple thing am I doing incorrectly?

VBA Code:
    Set wsChart = ThisWorkbook.Worksheets("Sheet2") '<= worksheet containing the chart

    Set rAnchorCellChartData = wsChart.Range("C2")  '<= this is the cell where chart is located (in wsChart)
   
    With rAnchorCellChartData
   
'This yields Sheet2 which is correct
Debug.Print "rAnchorCellChartData.Parent = " & .Parent.Name

'This yields $C$2 which is correct.
Debug.Print "rAnchorCellChartData address = " & .Address

'This yields $B$2:$F$3 which is correct. Column B holds data labels.
Debug.Print "Data range = " & .CurrentRegion.Address
   
'       Yields zero
        'iColumns = .Cells(1, wsChart.Columns.Count).End(xlToLeft).Column - .Column
       
'       Yields zero
        'iColumns = .Cells(1, Columns.Count).End(xlToLeft).Column - .Column
       
'       Yields zero
        'iColumns = .Offset(0, 10000).End(xlToLeft).Column - .Column
       
'       Yields 16383
'       iColumns = .End(xlToRight).Column - .Column
   
    End With
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try:
VBA Code:
Sub test_last()
  Dim wsChart As Worksheet
  Dim rAnchorCellChartData As Range
  Dim iColumns As Long
  
  Set wsChart = ThisWorkbook.Worksheets("Sheet2") '<= worksheet containing the chart
  Set rAnchorCellChartData = wsChart.Range("C2")
  With rAnchorCellChartData
    iColumns = wsChart.Cells(.Row, wsChart.Columns.Count).End(xlToLeft).Column
  End With
End Sub

Or simply:
VBA Code:
Sub lastcolumn()
  Dim lc As Long
  lc = Sheets("Sheet2").Cells(2, Columns.Count).End(xlToLeft).Column
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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