VBA Code to Get the Column Letter for the First non-Zero

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for any suggestions for which I will provide feedback.

Why am I getting the error: "Run-time error '438': Object doesn't support this property or method" on the following line:

Code:
Cells(4, i).Formula "= MATCH(TRUE,INDEX(E" & i & ":" & LastColumnLtr & i & "<>0,),0)"

The following is the entire code where I am trying to obtain the column letter of the first non-zero or first non-blank value (cannot be zero).

Code:
Sub Non()


'Dimensioning
    Dim i As Long
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim LastColumnLtr As String
    

Worksheets("Sheet1").Activate 


'Find the last row      
        LastRow = Cells.Find(What:="*", After:=Range("A1"), _
        LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, MatchCase:=False).Row
      
'Find the last column & turn into letter
    'Find the last column
        LastColumn = Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, _
        LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, _
        MatchCase:=False).Column
        
    'Turn into letter
        LastColumnLtr = Split(Cells(1, LastColumn).Address, "$")(1)




'Find the first non-zero or non-blank cells


    For i = 4 To LastRow
        
        
        Cells(4, i).Formula "= MATCH(TRUE,INDEX(E" & i & ":" & LastColumnLtr & i & "<>0,),0)"




    Next i


End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try:
Code:
Cells(4, i).Formula  = "= MATCH(TRUE,INDEX(E" & i & ":" & LastColumnLtr & i & "<>0,),0)"
 
Upvote 0
Thanks Fluff as that worked perfect. Now another question, where the LastRow is row 6, it only executes the first row. I did add a line after the formula for a message box to appear indicating the formula and it works, but nothing appears in cells (4, 5) or (4, 6). Just (4, 4).
 
Upvote 0
Thanks mumps as that worked perfect. Now another question, where the LastRow is row 6, it only executes the first row. I did add a line after the formula for a message box to appear indicating the formula and it works, but nothing appears in cells (4, 5) or (4, 6). Just (4, 4)
 
Upvote 0
Do you want the formula in rows4,5,6 of column D, or columns D,E,F on row 4?
 
Upvote 0
Thanks so much Fluff!

I wanted it column D and realized it should be Cells(i, 4) not Cells(4, i).
 
Upvote 0
Glad you figured it out & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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