VBA and selecting non-zero values..

segran

Active Member
Joined
Aug 20, 2004
Messages
335
Hi,

I am performing regression analysis.

I created a button, and am running the following macro -


Sub Macro5()
'
' Macro5 Macro
'

'
ActiveSheet.Range("$o$16:$w$34").Clear
Application.Run "ATPVBAEN.XLAM!Regress", ActiveSheet.Range("$h$15:$h$32"), _
ActiveSheet.Range("$i$15:$l$32"), False, True, , ActiveSheet.Range("$o$16") _
, False, False, False, False, , False
End Sub


Currently, I have defined Y-range as $h$15:$h$32, however, I want to automatically choose y- and x- range.

Currently $h$15:$h$32 is based on historical data. However, this range then has zero data, when another set of y-variable is chose (based on forecasted data). I would like to automatically choose all-non zero data in column h, and automatically chose the corresponding data in range i:l. For example, if the data range with non-zeros start in range ("$h$33:$h$40), then x-range will be ("$i$33:$l$40").

Thanking you in advance for your help.

 
Hi,

I am still only obtaining 15 and 32 instead of 22 and 27.

I am not sure why. Please help?
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
In you original post you had zeroes for no data. Now you have "". Try:

Rich (BB code):
Sub Test()
    Dim r As Integer
    Dim First As Long
    Dim Last As Long
    r = 15
    With ActiveSheet
        Do While .Range("H" & r) = ""
            r = r + 1
        Loop
        First = r
        Do Until .Range("H" & r) = ""
            r = r + 1
        Loop
        Last = r - 1
    End With
    MsgBox First & " " & Last
End Sub
 
Upvote 0
That did the trick :) Thank you Andrew.

But how do I include the labels of columns.
If I change r = 15, will that work?
 
Upvote 0
Hi,

Hope you all are well.

I have a follow-up question to my last post regarding this example.

The code give me the first and last row line, example row 32, when it outputs.

I have the following excel funstion in column E.

IF(D16<VLOOKUP($F$5,beta,2,FALSE),SUMPRODUCT((consumer=$F$5)*(sega=$F$7)*(date=$D16)*data),F16))

The vlookup function, essentially chooses between years 2008 and 2030.Thereafter, if the reference cell (D16), which refers to column D (years), is less than 2008 for example, then it prints sumproduct in colum E as a result, else F16.

I would like to use the Last result as in the code, such that if D16<VLOOKUP($F$5,beta,2,FALSE)<Last (but converted in terms of years, like column D), then it prints sumproduct, else F16.

Also, how do I place the last result converted to a year, for example Last = 29, refers to year 2004 (in column D), in a cell in the sheet.

I hope I made sense.

Thank you in advance for your help.
 
Upvote 0
In your macro you can place the year in a cell, eg A1, like this:

Range("A1").Value = Range("D" & Last).Value
 
Upvote 0

Forum statistics

Threads
1,216,085
Messages
6,128,732
Members
449,465
Latest member
TAKLAM

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