0

I am writing script/code/something to loop through each worksheet in the workbook and it is supposed to get the values from the worksheets that match the criteria. But when I run the function I do not get any value and when I have added MsgBox to see how far the code has gone it doesn't trigger.

I want the code to cycle through each worksheet and then gather the data as coded.

Anyone know what I am doing wrong? (Also I am new to Stackoverflow so if I need to improve my post in anyway please let me now!)

Function FindBelopp(Month As String, Typ As String) As Double

Dim rng As Range
Dim beloppColumn As Range
Dim lRow As Range
Dim firstAddress As Range
Dim ws As Worksheet
Dim rngAmount As Double
Dim ws_Count As Integer
Dim I As Integer

I = 1
ws_Count = Worksheets.Count
Do While I < ws_Count
Set ws = Worksheets(I)
If InStr(1, ws.Name, Month, 0) > 0 Then
With ws.Cells
    Set beloppColumn = .Find("SEK", LookIn:=xlValues)
    Set rng = .Find(Typ, LookIn:=xlValues, lookAt:=xlWhole)
    If Not rng Is Nothing Then
    Set firstAddress = rng
    Do
        If IsEmpty(rng.Offset(1, 0)) = True Then
            Set lRow = Range(Cells(rng.Row, beloppColumn.Column), Cells(rng.End(xlDown)(0).Row, beloppColumn.Column))
        Else
            Set lRow = Range(Cells(rng.Row, beloppColumn.Column), Cells(rng.Row, beloppColumn.Column))
        End If
    If Application.Sum(lRow) > 0 Then
        rngAmount = Application.Sum(lRow) + rngAmount
    Else: End If
    Set rng = .FindNext(rng)
    Loop While rng.Address <> firstAddress.Address
    Else: End If
    End With
    Else: End If
    I = I + 1
Loop
FindBelopp = rngAmount
End Function

First of all. Thank you for the quick response and for teaching me something new. The issue was with .FindNext()

I made it work in a similar fashion and it might not be the most efficient use but incase of someone stumbling here with the same issue I thought I could add my now working code.

Function LoopThroughEachSheet(iWantMonths As String, iWantValues As String) As Double

Dim rng As Range
Dim firstAddress As Range
Dim moneyColumn As Range
Dim sumRows As Range
Dim lRow As Range
Dim Money As Double
Dim ws As Worksheet


For Each ws In Worksheets
    If InStr(1, _
            UCase(ws.Name), _
            UCase(iWantMonths)) > 0 Then
        With ws.Cells
            Set rng = .Find(iWantValues, _
                            lookAt:=xlWhole, _
                            LookIn:=xlValues)
            If Not rng Is Nothing Then
                Set firstAddress = rng
                Set moneyColumn = .Find("SEK", _
                                        LookIn:=xlValues, _
                                        lookAt:=xlWhole)
                    Do
                        With ws
                            Set lRow = rng.End(xlDown)(0)
                            If IsEmpty(rng.Offset(1, 0)) = True Then
                                Set sumRows = .Range(Cells(rng.Row, moneyColumn.Column).Address, _
                                                    Cells(lRow.Row, moneyColumn.Column).Address)
                            Else
                                Set sumRows = .Cells(rng.Row, moneyColumn.Column)
                            End If
                        End With
                        Money = Application.Sum(sumRows) + Money
                        Set rng = .Find(iWantValues, _
                                        After:=rng, _
                                        lookAt:=xlWhole, _
                                        LookIn:=xlValues)
                    Loop While firstAddress.Address <> rng.Address
            Else: End If
        End With
    Else: End If
Next

LoopThroughEachSheet = Money
End Function
  • How are you calling this? If you're calling it from a cell, note that `FindNext` doesn't work in UDFs. – BigBen Aug 20 '21 at 12:27
  • How `Month` does look when you call the function? I suppose this is not a UDF function... How do you call the function? – FaneDuru Aug 20 '21 at 12:29
  • Is the routine called at all? How? Have you tried to use the debugger to step thru your code? – FunThomas Aug 20 '21 at 12:29
  • @BigBen The function is entered into a cell. I had it earlier as calling a function but it would only do 2 loops and then quit. Even though I have 51 Worksheets. – Marcus Pantzar Aug 20 '21 at 12:33
  • @FaneDuru Hi, sorry but I don't understand your question. The function is entered into a cell currently. Month is just a string containing the first letters of that Month ex:"Jan". – Marcus Pantzar Aug 20 '21 at 12:35
  • @FunThomas Not sure what you mean with routine but the function is put in a cell with the values. The debugger won't let me step through it since it is not a sub to my understanding. – Marcus Pantzar Aug 20 '21 at 12:36
  • With routine I mean your function. And of course you can debug also a function. – FunThomas Aug 20 '21 at 12:39
  • 1
    Being entered into a cell, it is a UDF function, and you cannot use this code. UDF function returns only a value, not allow to use FindNext... Try calling the function from VBE. Create a simple sub `Sub TestCall` having a single code line: `MsgBox "Jan", Typ` `End Sub`. Take care to correctly use the `Typ` parameter, which I do not know what it should be... This is only to test the function if it wonks not called from a cell (like a UDF function). – FaneDuru Aug 20 '21 at 12:47
  • @FaneDuru It works when I run it from a Sub. I will try to call the Sub through the function and see if that fixes it. – Marcus Pantzar Aug 20 '21 at 12:57
  • @FunThomas How would I go about debugging a function. I can't figure out how to step into it. – Marcus Pantzar Aug 20 '21 at 13:01
  • 1
    No, it will not fix it, I am afraid... A UDF function cannot call function not allowed as UDF... Or, it can, but it will not work, exactly as calling it directly. You have to change the way of doing it. Try using only VBA, creating a button able to iterate between the range you want processing and giving values trough the existing function. Even a sheet Event (maybe `Change`) to process according to a changed cell/range value. – FaneDuru Aug 20 '21 at 13:03
  • 1
    @FaneDuru Thanks for all the help. I will try to find a pretty way to do it with a button. I really appreciate all the help! – Marcus Pantzar Aug 20 '21 at 13:10
  • Simply set a Breakpoint. If it is a UDF. After that, either modify something in the sheet so that it is triggered, or call if from the immediate window, or write a 1-line Sub that calls it. – FunThomas Aug 20 '21 at 14:31

0 Answers0