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.

 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
This should give you the first and last rows:

Code:
Sub Test()
    Dim r As Integer
    Dim First As Long
    Dim Last As Long
    r = 16
    With ActiveSheet
        Do While .Range("H" & r) = 0
            r = r + 1
        Loop
        First = r
        Do Until .Range("H" & r) = 0
            r = r + 1
        Loop
        Last = r - 1
    End With
    MsgBox First & " " & Last
End Sub

You can use the variables in your code like this:

ActiveSheet.Range("H" & First & ":H" & Last)

You may need to change the starting value of r.
 
Upvote 0
Hi,

How do i enter this code into the macro?
Also, how does it chose the x-range?

Thank you
 
Upvote 0
Just paste it at the beginning of your code (excluding the first and last lines). Then use the variables First and Last in your code like I showed you.
 
Upvote 0
Hi,

This is not working. Please help.


Sub Macro5()
'
' Macro5 Macro
'

'

Dim r As Integer
Dim First As Long
Dim Last As Long
r = 16
With ActiveSheet
Do While .Range("H" & r) = 0
r = r + 1
Loop
First = r
Do Until .Range("H" & r) = 0
r = r + 1
Loop
Last = r - 1
End With
MsgBox First & " " & Last


ActiveSheet.Range("$o$16:$w$34").Clear
Application.Run "ATPVBAEN.XLAM!Regress", ActiveSheet.Range("H" & First & ":H" & Last), _
ActiveSheet.Range("H" & First & ":H" & Last), False, True, , ActiveSheet.Range("$o$16") _
, False, False, False, False, , False
End Sub
 
Upvote 0
Your second:

ActiveSheet.Range("H" & First & ":H" & Last)

should be:

ActiveSheet.Range("I" & First & ":L" & Last)
 
Upvote 0
Hi,

I get 15 and 32.

Here what I have :

Sub Macro5()
'
' Macro5 Macro
'

'

Dim r As Integer
Dim First As Long
Dim Last As Long
r = 15
With ActiveSheet
Do While .Range("H" & r) = 0
r = r + 1
Loop
First = r
Do Until .Range("H" & r) = 0
r = r + 1
Loop
Last = r - 1
End With
MsgBox First & " " & Last


ActiveSheet.Range("$o$16:$w$34").Clear
Application.Run "ATPVBAEN.XLAM!Regress", ActiveSheet.Range("H" & First & ":H" & Last), _
ActiveSheet.Range("i" & First & ":l" & Last), False, True, , ActiveSheet.Range("$o$16") _
, False, False, False, False, , False
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,709
Members
449,464
Latest member
againofsoul

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