+ Reply to Thread
Results 1 to 26 of 26

Have no idea but I have a problem, error 91

  1. #1
    Registered User
    Join Date
    08-28-2014
    Location
    São Paulo
    MS-Off Ver
    2007
    Posts
    30

    Angry Have no idea but I have a problem, error 91

    I'm getting error '91' after Cells.find command. Please help, thank you.

    Run-time error '91'
    Object variable or With block variable not set

    Please find a example below:

    PHP Code: 
    Sub teste() 

    Dim mes As Integer 
    Dim ano 
    As Integer 
    Dim pais 
    As String 
          
    mes 
    Application.InputBox(Prompt:="Digite o número do mês que esteja com todos os dias completos (ex:2 para Favereiro)"Title:="Mês"Type:=1
    ano Application.InputBox(Prompt:="Digite o ano (Menor anos possível: 2012)"Title:="Ano"Type:=1
    pais Application.InputBox(Prompt:="Digite o país (ex: Brazil)"Title:="País"Type:=2

    Cells.Find(What:=paisAfter:=ActiveCellLookIn:=xlFormulasLookAt _ 
            
    :=xlPartSearchOrder:=xlByRowsSearchDirection:=xlNextMatchCase:= 
            False
    SearchFormat:=False).Activate 


    EndSub 

  2. #2
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Canada Eh
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: Have no idea but I have a problem, error 91

    Hi PedroShulz,

    You are getting that error when the search criteria does not exist. I would suggest assigning the search to a range variable and then testing whether it exists:
    Please Login or Register  to view this content.
    Click the * to give Rep to a post you like.

  3. #3
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Have no idea but I have a problem, error 91

    Most likely the search string is not found and the ACTIVATE then fails... Not bothering with sample code - would have been the same as above

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Have no idea but I have a problem, error 91

    Pedro,

    The problem is that you are trying to activate a cell, but if no result is found, then you can't activate nothing. When the search finds a result there is no error, so I suggest something like:

    Please Login or Register  to view this content.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  5. #5
    Registered User
    Join Date
    08-28-2014
    Location
    São Paulo
    MS-Off Ver
    2007
    Posts
    30

    Unhappy Re: Have no idea but I have a problem, error 91

    It has happened again. Please find the whole Macro.

    Thank you

    PHP Code: 
    Sub teste() 

    Dim mes As Integer 
    'given month 
    Dim ano As Integer 
    '
    given year 
    Dim pais 
    As String 
    'given country 
    Dim find As Range 
    Dim find2 As Range 
    Dim find3 As Range 
    Dim valor As String 
    Dim col_pais As Integer 
    Dim lin_data As Integer 
    Dim lin_sum As Integer 
    Dim sum As Integer 
    Dim DataMin As String 
    Dim DataMax As String 
    Dim n As Integer 
          
    mes = Application.InputBox(Prompt:="Digite o número do mês que esteja com todos os dias completos (ex:2 para Favereiro)", Title:="Mês", Type:=1) 
    ano = Application.InputBox(Prompt:="Digite o ano (Menor anos possível: 2012)", Title:="Ano", Type:=1) 
    pais = Application.InputBox(Prompt:="Digite o país (ex: Brazil)", Title:="País", Type:=2) 
    sum = 0 

    Set find = Cells.find(What:=pais, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
            False, SearchFormat:=False) 
                 
    If Not find Is Nothing Then 
    find.Activate 
                 
    ActiveCell.Offset(1, 0).Select 
    ActiveCell.Offset(0, 1).Select 
    ActiveCell.Offset(7, 0).Select 
    col_pais = ActiveCell.Column 

    find = Nothing 
                 
        If mes > 1 And mes < 12 Then 
            If ano >= 2012 Then 
              
                valor = DateSerial(ano, mes, 1) 
                '
    valor is equal to the first day of the given month within the given year 
                Set find 
    Cells.find(What:=valorAfter:=ActiveCellLookIn:=xlFormulasLookAt _ 
                
    :=xlPartSearchOrder:=xlByRowsSearchDirection:=xlNextMatchCase:= 
                False
    SearchFormat:=False
                 
                If 
    Not find Is Nothing Then 
                    find
    .Activate 
                    lin_data 
    ActiveCell.Row 
                    DataMin 
    valor 
                    DataMax 
    DateSerial(ano, (mes 1), 0
                    
    find Nothing 
                    Set find 
    Cells.find(What:=DataMaxAfter:=ActiveCellLookIn:=xlFormulasLookAt _ 
                    
    :=xlPartSearchOrder:=xlByRowsSearchDirection:=xlNextMatchCase:= 
                    False
    SearchFormat:=False
                         
                    If 
    Not find Is Nothing Then 
                        find
    .Activate 
                        lin_sum 
    ActiveCell.Row 
                                 
                            
    While (lin_data 1) <> lin_sum 
                                Cells
    (lin_sumcol_pais).Select 
                                sum 
    sum Cells(lin_sumcol_pais
                                
    lin_sum lin_sum 
                            Wend 
                        MsgBox sum 
                        
    Else 
                            
    MsgBox "erro" 
                    
    End If 
                Else 
                    
    MsgBox "erro" 
                
    End If 
            Else 
                
    MsgBox "Menor ano possível: 2012" 
            
    End If 
        Else 
            
    MsgBox "Digite um mês de 1 a 12" 
        
    End If 

    Else 
        
    MsgBox "erro" 
    End If 

    End Sub 

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Have no idea but I have a problem, error 91

    Please Login or Register  to view this content.
    needs to say
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    08-28-2014
    Location
    São Paulo
    MS-Off Ver
    2007
    Posts
    30

    Re: Have no idea but I have a problem, error 91

    Quote Originally Posted by Arkadi View Post
    Please Login or Register  to view this content.
    needs to say
    Please Login or Register  to view this content.
    The second "find" doesn't work.

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Have no idea but I have a problem, error 91

    What do you mean when you say second find doesn't work? You have a line that says "find = nothing" but find is a range, and should say "SET". That is because find is a range. I'm not sure what else is wrong, maybe if you attach your workbook? When I tested your code, I got an error on your line of code "find = nothing" but the error stopped when I changed it to "set find = nothing"

  9. #9
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Have no idea but I have a problem, error 91

    PS don't forget you have 2 lines that say find = nothing, change both

  10. #10
    Registered User
    Join Date
    08-28-2014
    Location
    São Paulo
    MS-Off Ver
    2007
    Posts
    30

    Talking Re: Have no idea but I have a problem, error 91

    Quote Originally Posted by Arkadi View Post
    PS don't forget you have 2 lines that say find = nothing, change both
    I did it but the second cells.find didn't work.

    Please find a example sheet macro-enabled attached. I had to change data. Thank you very much for the help
    test.xlsm

  11. #11
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Canada Eh
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: Have no idea but I have a problem, error 91

    Don't use find as a range, find is a reserved word, use something like fnd.

  12. #12
    Registered User
    Join Date
    08-28-2014
    Location
    São Paulo
    MS-Off Ver
    2007
    Posts
    30

    Re: Have no idea but I have a problem, error 91

    Quote Originally Posted by Mad-Mizer View Post
    Don't use find as a range, find is a reserved word, use something like fnd.
    Changed and it still not working

  13. #13
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Canada Eh
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: Have no idea but I have a problem, error 91

    What line in your code is the error stopping on?

  14. #14
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Have no idea but I have a problem, error 91

    Pedro what is not working? When I go through the macro it does not give any errors. It does give the message "err" but the code works.

  15. #15
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Have no idea but I have a problem, error 91

    Mad-mizer, love your location, haha.

  16. #16
    Registered User
    Join Date
    08-28-2014
    Location
    São Paulo
    MS-Off Ver
    2007
    Posts
    30

    Re: Have no idea but I have a problem, error 91

    Quote Originally Posted by Arkadi View Post
    Pedro what is not working? When I go through the macro it does not give any errors. It does give the message "err" but the code works.
    Well, the Msg "err" is too find the error
    The problem is in line 47. There's no match for "valor" and it displays the msg "erro"

  17. #17
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Have no idea but I have a problem, error 91

    Ok so the code is working, it is just not doing what you want. The problem is you are looking for a date like 01/02/15 but your date column is formatted differently. Therefore there is no match.

  18. #18
    Registered User
    Join Date
    08-28-2014
    Location
    São Paulo
    MS-Off Ver
    2007
    Posts
    30

    Re: Have no idea but I have a problem, error 91

    Quote Originally Posted by Arkadi View Post
    Ok so the code is working, it is just not doing what you want. The problem is you are looking for a date like 01/02/15 but your date column is formatted differently. Therefore there is no match.
    If you're refering to "Digite o número do mês que esteja com todos os dias completos (ex:2 para Favereiro)" that's just a normal message suggesting to type 2 for February as example.

  19. #19
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Have no idea but I have a problem, error 91

    Pedro, instead of Dim valor as string, try dim valor as date. Change DataMax and any other date variable to a date instead of a string. That should help. Also, you have a line
    Please Login or Register  to view this content.
    I think that checks to make sure the month is 1-12? it needs to be > 0 and < 13

  20. #20
    Registered User
    Join Date
    08-28-2014
    Location
    São Paulo
    MS-Off Ver
    2007
    Posts
    30

    Re: Have no idea but I have a problem, error 91

    Quote Originally Posted by Arkadi View Post
    Pedro, instead of Dim valor as string, try dim valor as date. Change DataMax and any other date variable to a date instead of a string. That should help. Also, you have a line
    Please Login or Register  to view this content.
    I think that checks to make sure the month is 1-12? it needs to be > 0 and < 13
    I've change >= and <= already. My only problem is on the "If not fnd is Nothing Then" because for some reason fnd is equal to nothing :s

  21. #21
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Have no idea but I have a problem, error 91

    As I said, if you dim your date variables as date and not string, then it should find the date.
    There are more problems with the code too, since you have a while loop that will crash when the rest works. I don't know what it is supposed to do, so hard to help with that.

  22. #22
    Registered User
    Join Date
    08-28-2014
    Location
    São Paulo
    MS-Off Ver
    2007
    Posts
    30

    Re: Have no idea but I have a problem, error 91

    Quote Originally Posted by Arkadi View Post
    Pedro, instead of Dim valor as string, try dim valor as date. Change DataMax and any other date variable to a date instead of a string. That should help. Also, you have a line
    Please Login or Register  to view this content.
    I think that checks to make sure the month is 1-12? it needs to be > 0 and < 13
    But now I received a Overflow error from sum :S

  23. #23
    Registered User
    Join Date
    08-28-2014
    Location
    São Paulo
    MS-Off Ver
    2007
    Posts
    30

    Re: Have no idea but I have a problem, error 91

    Quote Originally Posted by Arkadi View Post
    As I said, if you dim your date variables as date and not string, then it should find the date.
    There are more problems with the code too, since you have a while loop that will crash when the rest works. I don't know what it is supposed to do, so hard to help with that.
    Sorry. Let me try to explain.
    Basically you type march (3) of 2014 (2014) for Brazil (Brazil) and you'll have a sum that will be the result from all revenues from the given month inside the given year. In other words, from the first until the 31st march, what's the rev?

  24. #24
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Have no idea but I have a problem, error 91

    What is the value of sum when it overflows? I see sum is an integer, so maybe the value is too big. What happens if you dim sum as long instead of integer?

  25. #25
    Registered User
    Join Date
    08-28-2014
    Location
    São Paulo
    MS-Off Ver
    2007
    Posts
    30

    Re: Have no idea but I have a problem, error 91

    Quote Originally Posted by Arkadi View Post
    What is the value of sum when it overflows? I see sum is an integer, so maybe the value is too big. What happens if you dim sum as long instead of integer?
    I've found it.
    The problem was that dates were organized from most recent to oldest. lin_sum was going further from the first day of the month and the value was too bizarre.
    The solution was:
    Change:
    lin_data - 1 --> lin_data +1
    lin_sum = lin_sum - 1 --> lin_sum = lin_sum +1


    Thank you for your patience man, seriously haha

  26. #26
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Have no idea but I have a problem, error 91

    You are welcome
    I hope things are working better now? The +1 does make more sense
    I would still dim sum as long and not integer, integers have a max value that COULD be a problem, depending on how big the revenues are

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Any idea why I would get a Loop without Do error???
    By Jubb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-25-2014, 02:39 PM
  2. error 13 type mismatch - any idea ??
    By Blue_kul in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-04-2012, 10:30 PM
  3. Getting run-time error......any idea why?
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-21-2010, 01:11 PM
  4. idea?:Run-time error '1004'
    By Jonathan78 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-04-2010, 08:00 AM
  5. A weird error, no idea why it occurs
    By cgi_pro in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-03-2006, 05:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1