Need solution regarding Mod combined with Lcm in macro

johnny5837

New Member
Joined
Jan 20, 2018
Messages
2
Hello guys,


I’m new in VBA and can’t figure out how should I move forward. I’m using the below mentioned macro to calculate different requirements. It works fine if the frequency values in column B are divisible with lowest frequency, for example 100, 200, 400 & 500. However if there is an non-common number in my range, the result is incorrect.


Code:
 Sub MaxFreq()
'Speed up
Application.ScreenUpdating = False


'Clear column D & E
Dim LastRow As Long
With ActiveSheet
    LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
End With
    
Range("D2:F" & LastRow).ClearContents


'Numbers needed as Integer
Dim i, x, NumRows, MyRows, LowestFreq, HighestFreq As Integer


'Find the highest frequency
HighestFreq = Application.WorksheetFunction.max(Range("B:B"))


'Find lowest frequency
LowestFreq = Application.WorksheetFunction.min(Range("B:B"))


'Use frequencies as string
Dim myResult As String


'Find the number of rows to use it in loop in column B
NumRows = Range("B1", Range("B1").End(xlDown)).Rows.Count


'Use LCM function
MyRows = WorksheetFunction.Lcm(Range("B2:B" & NumRows)) / LowestFreq


'Loop both x & i to get the results
For x = 2 To NumRows


    For i = 1 To MyRows
    myResult = Range("B" & x)
    
    If LowestFreq * i = LowestFreq Then
    Range("E" & i + 1) = LowestFreq
    End If


    If LowestFreq * i Mod myResult = 0 Then
        If Range("E" & i + 1) = "" Then
        Range("E" & i + 1) = Range("B" & x)
        Range("F" & i + 1) = LowestFreq
        Else
        Range("E" & i + 1) = Range("E" & i + 1) & ", " & Range("B" & x)
        Dim text_string As String
        text_string = Range("E" & i + 1)
        Range("F" & i + 1) = WorksheetFunction.Lcm(Split(text_string, ", "), LowestFreq)
        End If
    End If
    Range("D" & i + 1) = "Service # " & i


    Next i
Next x


'Show results
Application.ScreenUpdating = True


End Sub


My sheet has the followings with correct result:



Excel 2010
BCDEF
1FreqNumber of ServicesType of ServicesJob plan
2100Service # 1100100
3200Service # 2100, 200200
4400Service # 3100100
5500Service # 4100, 200, 400400
6Service # 5100, 500500
7Service # 6100, 200200
8Service # 7100100
9Service # 8100, 200, 400400
10Service # 9100100
11Service # 10100, 200, 5001,000
12Service # 11100100
13Service # 12100, 200, 400400
14Service # 13100100
15Service # 14100, 200200
16Service # 15100, 500500
17Service # 16100, 200, 400400
18Service # 17100100
19Service # 18100, 200200
20Service # 19100100
21Service # 20100, 200, 400, 5002,000
CALCULATION





And here is the incorrect result with other frequencies:



Excel 2010
BCDEF
1FreqNumber of ServicesType of ServicesJob plan
2300Service # 1300300
3750Service # 2300300
41,500Service # 3300300
5Service # 4300300
6Service # 5300, 750, 15001,500
CALCULATION



And this how the result should look like:



Excel 2010
BCDEF
1FreqNumber of ServicesType of ServicesJob plan
2300Service # 1300300
3750Service # 2300300
41,500Service # 3750750
5Service # 4300300
6Service # 5300300
7Service # 6300, 750, 15001,500
CALCULATION



I would really appreciate if someone would take a look on it.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
This may or (probably) may not have an affect -

Your Dim statement here actually sets only the last named variable as an integer. The rest are "Variant" type.

'Numbers needed as Integer
Dim i, x, NumRows, MyRows, LowestFreq, HighestFreq As Integer

To get the integer types:

Dim i as integer
dim x as integer
etc.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
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