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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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