VBScript: Find last column used LETTER value (ex. "F"), not numerical value

GJDouglas

New Member
Joined
Jun 22, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I am relatively new to Scripting in general, and need help with determining the LETTER of the last column used in a VBScript. The CVS file I am reading in the VBScript has data in columns A thru F. Therefore, want to figure out how to get the value of "F" returned (to be used later in the script). Most google searches return how to do it in VBA, but not VBScript. I have tried several of the googled "suggestions", but nothing has worked.

Here's my script's set up:

Set fso = CreateObject("Scripting.FileSystemObject")
file = PathAndFileName.csv
With CreateObject("Excel.Application")
Set WB = .Workbooks.Open(file)

Suggested, but failed, attempts to get the letter value of the last column used:
1. LastColumn = WB.ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
2. LastColumn = WB.ActiveSheet.UsedRange.Column + WB.ActiveSheet.UsedRange.Columns.Count - 1
3. LastColumn = WB.ActiveSheet.Columns.Cells(1, Columns.Count).End(xlToLeft).Column
4. LastColumn = WB.ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
5. LastColumn = Split(WB.ActiveSheet.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Cells.Address(1, 0), "$")(0)
6. LastColumn = WB.ActiveSheet.UsedRange.Column

Could someone please enlighten me on VBScript code that would actually work to return the LETTER value of the last column used?
Thanks....
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi and welcome to MrExcel

Try:

VBA Code:
dim LastColumn as string
LastColumn = Split(wb.ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Address(1, 0), "$")(0)

:cool:
 
Upvote 0
What exactly do you mean by "the last column used"? I am asking because you could have formulas displaying the empty text string "" located after the last visible value in a row... do you want the column letter of the last visible value or the column letter of the last cell with anything it even if that is a formula displaying "" (the empty text string)?
 
Upvote 0
Hi Rick,

I'm looking for the right-most column out of the whole table that has visible data in it. I would not want the column selected if it doesn't have any visible data in the entire column (i.e. a formula). Hope that makes sense. Thanks!
 
Upvote 0
Hi and welcome to MrExcel

Try:

VBA Code:
dim LastColumn as string
LastColumn = Split(wb.ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Address(1, 0), "$")(0)

:cool:
I'm sorry Dante.... I spoke to soon (I had quickly looked at your response from my phone, and had missed your comment that the code was for VBA). I am not looking for the code in VBA -- I'm need the code for finding the letter value of the last column that has data in VBSCRIPT. I do appreciate you taking the time to respond.
 
Upvote 0
I'm not sure, but try the following.
Replace my test file "c:\trabajo\testfile.csv" with your file.

VBA Code:
Sub LastCol()
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set objexcel = CreateObject("Excel.Application")
  objexcel.Visible = True
  Set objWB = objexcel.Workbooks.Open("c:\trabajo\testfile.csv")
  Set objSH = objWB.Worksheets(1)
  cell = objSH.Cells(1, objSH.Columns.Count).End("-4159").Address
  a = Split(cell, "$")
  MsgBox a(1)
  objexcel.Quit
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,503
Messages
6,131,020
Members
449,615
Latest member
Nic0la

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