Calculation in excel VBA

mehidy1437

Active Member
Joined
Nov 15, 2019
Messages
348
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
Hi Experts,

I need help with the below issue.
I have data in A2:D5, I will divide each cell by a value let say 40 or anything.
What I need is a bit different, kindly see the below example.

2233.xlsm
ABCDEFGHIJK
1SMLXLRESULT DIVIDED BY 40REQUREMENTULTIMATE RESULT
2180400.025021IF RESULT IS <1 OR PRIME NUMBER, THEN IT WILL COUNT 1 FOR FULL ROW RANGE1
370112001.750.0253IF RESULT IS >1 AND FRACTION NUMBER, THEN IT WILL COUNT 2 FOR EACH CELL3
4100391231442.50.9753.0753.67
511201001000.02532.52.55
616
Sheet3 (2)
Cell Formulas
RangeFormula
F2:I5F2=A2/40
K3K3=1+2
K4K4=2+1+2+2
K5K5=1+2+2
K6K6=SUM(K2:K5)


*** Range is not fixed
***Range will select through an input box & divided value will also get from another input box
My ultimate result is to get 16 from this example in VB
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
See if this does what you need,
VBA Code:
Sub test()
Dim d As Long, r As Long, i As Long, n As Double, p As Long, t As Long
Dim rng As Range, rw As Range, c As Range
d = InputBox("Divide by what?")
Set rng = Application.InputBox(prompt:="Please select range", Type:=8)

For Each rw In rng.Rows
    p = 0: r = 0
    For Each c In rw.Cells
        n = c.Value / d
           
        If n <> Int(n) Or n <= 1 Then
            p = 1
        Else
            For i = 1 To n
                If n Mod i = 0 Then
                    r = r + 2
                End If
            Next i
        End If
    Next c
        t = t + r + p
Next rw
    MsgBox t
End Sub
 
Upvote 0
First of all, many thanks for your kind response.
It worked for the first case, but when I increase the range,
it's giving me a result 22, but it should be 18.
Kindly see below.

2233.xlsm
ABCDEFGHIJK
1SMLXLRESULT DIVIDED BY 40REQUREMENTULTIMATE RESULT
2180400.025021IF RESULT IS <1 OR PRIME NUMBER, THEN IT WILL COUNT 1 FOR FULL ROW RANGE1
370112001.750.0253IF RESULT IS >1 AND FRACTION NUMBER, THEN IT WILL COUNT 2 FOR EACH CELL3
4100391231442.50.9753.0753.67
511201001000.02532.52.55
611110.0250.0250.0250.0251
7112000.0253001
8
918
Sheet3 (2)
Cell Formulas
RangeFormula
F2:I7F2=A2/40
K3K3=1+2
K4K4=2+1+2+2
K5K5=1+2+2
K9K9=SUM(K2:K8)
 
Upvote 0
Some additional information,
only first row result is showing 5, but it s/b 1.
Ony 2nd row result is showing 5, but it s/b 3.
Only 3rd row result is showing 1, but it s/b 7.
 
Upvote 0
I have gained the correct result with the below changes.

VBA Code:
Sub test()
Dim d As Long, r As Long, i As Long, n As Double, p As Long, t As Long
Dim rng As Range, rw As Range, c As Range
d = InputBox("Divide by what?")
Set rng = Application.InputBox(prompt:="Please select range", Type:=8)

For Each rw In rng.Rows
    p = 0: r = 0
    For Each c In rw.Cells
        n = c.Value / d
        'If n <> Int(n) Or n <= 1 Then
        If n = Int(n) Or n <= 1 Then
            p = 1
        Else
            'For i = 1 To n
            For i = 1 To 1
                If n Mod i = 0 Then
                    r = r + 2
                End If
            Next i
        End If
    Next c
        t = t + r + p
Next rw
    MsgBox t
End Sub
 
Upvote 0
I did have a quick look earlier to try and find where it was going wrong, I wasn't seeing it but think I might have misunderstood part of the question.

Glad you got it working :)
 
Upvote 0
I have added one more condition in it & its working fine.
This is just to let you know @jasonb75

VBA Code:
Sub test()
Dim d As Long, r As Long, i As Long, n As Double, p As Long, t As Long, j As Long, k As Long
Dim rng As Range, rw As Range, c As Range
d = InputBox("Divide by what?")
Set rng = Application.InputBox(prompt:="Please select range", Type:=8)

For Each rw In rng.Rows
    p = 0: r = 0: k = 0
    For Each c In rw.Cells
        n = c.Value / d
        'If n <> Int(n) Or n <= 1 Then
        'If n = Int(n) Or n <= 1 Then
        If n <= 1 Then
            p = 1
            
         ElseIf n = Int(n) And n > 1 Then
            'For j = 1 To n
            For j = 1 To 1
                'If n Mod j = 0 Then
                    k = k + 1
                'End If
            Next j
    
        Else
            'For i = 1 To n
            For i = 1 To 1
                If n Mod i = 0 Then
                    r = r + 2
                End If
            Next i
        End If
    Next c
        t = t + r + k + p
Next rw
    MsgBox t
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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