Retrieve column of last non-zero value in VBA

louiserace

New Member
Joined
Jun 24, 2015
Messages
15
I have a spreadsheet which is created using VBA and I want to retrieve the column of the last non zero value on row 1 (range(K1:T1)). This is so I can use the column later in the code.

I am using Excel 2016.

Any help would be appreciated.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Heres one way:

Code:
myCol = Application.Match(10 ^ 10, Sheets("Sheet1").Range("K1:T1"))
If Not IsError(myCol) Then myCol = myCol + 10

The sheet will need changing as appropriate. The myCol variable will be an integer representing the last column with a number in the range.
 
Upvote 0
Sorry just seen none zero. Maybe then:

Code:
myCol = Evaluate("=LOOKUP(2,1/((Sheet1!K1:T1<>0)*(ISNUMBER(Sheet1!K1:T1))),COLUMN(Sheet1!K1:T1))")
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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