1

I have this Excel spreadsheet/workbook that parses through some data and creates another condensed spreadsheet. The problem is when the sheet has more than 255 columns. When the line of VBA code at the bottom is run, it returns 1, rather than 258. It seems as though the VBA script that I am working on is made for the older version of Excel, right? What do I have to do to get this to work properly?

Excel.Worksheet.Cells(1, MySheet.Columns.Count).End(xlToLeft).Column returns 1 when there are more than 255 columns

3
  • The problem might be that Excel.Worksheet.Cells and MySheet.Columns.Count actually refer to different worksheets. It should probably read: MySheet.Cells(1, MySheet.Columns.Count)..... Commented Feb 7, 2014 at 19:46
  • That should always return column number for the last no empty cell in that row... Commented Feb 7, 2014 at 19:49
  • You got is Stewbob, thanks man!!! Commented Feb 7, 2014 at 20:03

1 Answer 1

2

In your code Excel.Worksheet.Cells refers to the current, or active, worksheet, while MySheet.Columns.Count refers to a specific worksheet, defined in the MySheet variable.

If MySheet is not the current sheet, this code will return results that may differ from what you expect.

Change the line of code to:

MySheet.Cells(1, MySheet.Columns.Count).End(xlToLeft).Column
Sign up to request clarification or add additional context in comments.

1 Comment

As I mentioned above, you are absolutely correct. I appreciate the quick and accurate answer!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.