Vba formula help

Giggzz

Well-known Member
Joined
Jul 4, 2002
Messages
990
Code:
Sub AllGood6()
ActiveSheet.Unprotect ("as")
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim r As Range
Dim s As Range
If UserForm6.TextBox1 > 0 Then
    For Each r In Range("H26:H35")
    [B]For Each s In Range("A26:A35")
[/B]        If r.Value > 0 And s.Value > 0 Then
        r.Value = r.Value - ((r.Value * UserForm6.TextBox1.Value) / 100) / [B]s.Value
[/B]        
        Next r
        
  Unload UserForm6
End If

Application.ScreenUpdating = True
Application.EnableEvents = True
ActiveSheet.Protect ("as")
End Sub

Im trying to count the number of cells in A26:A35 > 0, taking that number and dividing it into the r.value.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You did not say what the problem is, but you need both an "End If" and a "Next s" before "Next r".
 
Upvote 0
Code:
Sub AllGood6()
ActiveSheet.Unprotect ("as")
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim r As Range
[B][U]Dim s As Range
[/U][/B]If UserForm6.TextBox1 > 0 Then
    For Each r In Range("H26:H35")
    [B][U]For Each s In Range("A26:A35")[/U]
[/B]        If r.Value > 0 [B][U]And s.Value > 0[/U][/B] Then
        r.Value = r.Value - ((r.Value * UserForm6.TextBox1.Value) / 100) / [B][U]s.Value
[/U][/B]        [B][U]Next s
[/U][/B]        Next r
        
  Unload UserForm6
End If

Application.ScreenUpdating = True
Application.EnableEvents = True
ActiveSheet.Protect ("as")
End Sub

Everything in bold I added thinking this was a easy change, wrong. ;)

1. with s.value - Im trying to count all the cells in A25:A36 > 0

2. than take that number and divide it into the r.value

3. than have applied to each cell in H26:H35 > 0.
 
Upvote 0
Worksheetfunction CountIf seems to work as I understand what you want.

Code:
Sub test()
  MsgBox CountGTZ(Range("A26:A35"))
End Sub

Function CountGTZ(r As Range) As Long
  CountGTZ = WorksheetFunction.CountIf(r, ">0")
End Function
 
Upvote 0
Hi Giggzz,

Hard to test without creating the form, but let me know if incorporating the following into your existing code does the trick:

Code:
Dim rngCell As Range

For Each rngCell In Range("A26:A35")
    If rngCell.Value > 0 And rngCell.Offset(, 7).Value > 0 Then
        With rngCell.Offset(, 7)
            .Value = .Value - ((.Value * UserForm6.TextBox1.Value) / 100) / rngCell.Value
        End With
    End If
Next rngCell

HTH

Robert
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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