Run time error 91

victorel21

New Member
Joined
Jul 8, 2021
Messages
25
Office Version
  1. 365
Platform
  1. Windows
I have this code and i am getting an error 91 on the find line.

Every variable has been correctly defined in another module, this worked just find two days ago, now all of them are giving me problems,

Any ideas?

Appreciate the help.

Sub todatabase2()

Sheets("Form").Activate
ID = Range("F1")
GR = Range("A3")
Departamento = Range("C3")
Semana = Range("D3")
Comentarios = Range("B25")

Sheets("Datalist").Activate
Month = Cells(Range("E:E").Find(Semana, Lookat:=xlWhole).Row, 6)


With Sheets("Employee_List")
Set RngCol = .Range("A2", .Cells(Rows.Count, 1).End(xlUp))
End With

Sheets("Employee_List").Activate
LastRow = RngCol.Rows.Count
Range("A2:G" & LastRow + 1).Copy

Sheets("Historico").Activate
last_line = Range("F" & Rows.Count).End(xlUp).Row + 1

Sheets("Historico").Range("F" & last_line).PasteSpecial Paste:=xlPasteValues


Do While Cells(last_line, 6) <> ""

If Sheets("Historico").Range("F" & last_line) <> "" Then
Sheets("Historico").Range("A" & last_line) = ID
Sheets("Historico").Range("B" & last_line) = GR
Sheets("Historico").Range("C" & last_line) = Departamento
Sheets("Historico").Range("D" & last_line) = Semana
Sheets("Historico").Range("M" & last_line) = Comentarios
Sheets("Historico").Range("E" & last_line) = Month

End If

last_line = last_line + 1

Loop

ThisWorkbook.Worksheets("Historico").Cells.EntireColumn.AutoFit

End Sub
 
Slight flaw in that idea, if you have already used something like
VBA Code:
Dim month
in any module, it will automatically be lower case in all modules.
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Slight flaw in that idea, if you have already used something like
VBA Code:
Dim month
in any module, it will automatically be lower case in all modules.
Right. You would need to use that technique before declaring those variables (and/or be aware what you may have done in other modules).
If you are starting a new project and beginning from scratch, and do that as you are declaring the variables, you should be good.
 
Upvote 0
It was great help, I changed from Month to Mes, and i Got the same error as before.

I dont get why the code is so volatile.

Whatever help will be appreciated, thanks.

Sub todatabase2()

Dim ID, GR, Departamento, Semana, Comentarios, Mes As Range

With Sheets("Form")
ID = Range("F1")
GR = Range("A3")
Departamento = Range("C3")
Semana = Range("D3")
Comentarios = Range("B25")
End With

Set Mes = Sheets("Datalist").Cells(Range("E:E").Find(Semana, Lookat:=xlWhole).Row, 6)

With Sheets("Employee_List")
Set RngCol = .Range("A2", .Cells(Rows.Count, 1).End(xlUp))
LastRow = RngCol.Rows.Count
.Range("A2:G" & LastRow + 1).Copy
End With

With Sheets("Historico")
last_line = .Range("F" & Rows.Count).End(xlUp).Row + 1
.Range("F" & last_line).PasteSpecial Paste:=xlPasteValues
Do While Cells(last_line, 6) <> ""
If .Range("F" & last_line) <> "" Then
.Range("A" & last_line) = ID
.Range("B" & last_line) = GR
.Range("C" & last_line) = Departamento
.Range("D" & last_line) = Semana
.Range("M" & last_line) = Comentarios
.Range("E" & last_line) = Mes
End If
last_line = last_line + 1
Loop
End With

ThisWorkbook.Worksheets("Historico").Cells.EntireColumn.AutoFit

End Sub
 
Upvote 0
It was great help, I changed from Month to Mes, and i Got the same error as before.
That often means it cannot find the value you are looking for in the range you have defined.

Try running this variation, which will tell you which value it cannot find:
VBA Code:
Sub todatabase2()

Dim ID, GR, Departamento, Semana, Comentarios, Mes As Range

With Sheets("Form")
    ID = Range("F1")
    GR = Range("A3")
    Departamento = Range("C3")
    Semana = Range("D3")
    Comentarios = Range("B25")
End With

On Error GoTo err_chk:
Set Mes = Sheets("Datalist").Cells(Range("E:E").Find(Semana, Lookat:=xlWhole).Row, 6)
On Error GoTo 0

With Sheets("Employee_List")
    Set RngCol = .Range("A2", .Cells(Rows.Count, 1).End(xlUp))
    LastRow = RngCol.Rows.Count
    .Range("A2:G" & LastRow + 1).Copy
End With

With Sheets("Historico")
    last_line = .Range("F" & Rows.Count).End(xlUp).Row + 1
    .Range("F" & last_line).PasteSpecial Paste:=xlPasteValues
    Do While Cells(last_line, 6) <> ""
        If .Range("F" & last_line) <> "" Then
            .Range("A" & last_line) = ID
            .Range("B" & last_line) = GR
            .Range("C" & last_line) = Departamento
            .Range("D" & last_line) = Semana
            .Range("M" & last_line) = Comentarios
            .Range("E" & last_line) = Mes
        End If
        last_line = last_line + 1
    Loop
End With

ThisWorkbook.Worksheets("Historico").Cells.EntireColumn.AutoFit

Exit Sub


err_chk:
    If Err.Number = 91 Then
        MsgBox "Cannot find value of " & Semana, vbOKOnly, "ERROR!!!"
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If
        
End Sub

Also, when posting your VBA code, please use Code Tags! Trying to read unformatted code can be cumbersome, especially when it gets a little lengthy.
See: How to Post Your VBA Code
 
Upvote 0
N

Also, you should NEVER use reserved words like "Month" as a variable!!! This can cause errors and unexpected results.
Reserved words are words that are already used as functions, properties, methods, etc.
Since there is already a function named "Month", you should not use it as a variable name (or a procedure or function name).

whilst it is generally considered not a good idea to use any names that are the same as the function, statement, method used in Visual Basic as you end up shadowing the same keywords in the language, it is my understanding this does not mean you cannot do it.

To use a language function that conflicts with an assigned name, you need explicitly identify it by preceding with the name of the associated type library an example would be, if you have a variable called Left, you can only call the VBA the Left function by using VBA.Left or Month would be VBA.Month
Using truly reserved keywords like Date for instance, normally the compiler complains turning the declared variable red & should take that as a no.

But I do agree, all best avoided. much easier to prefix with the variable with the declared data type example Dim strMonth As String or whatever.

just my thoughts

Dave
 
Upvote 0
That often means it cannot find the value you are looking for in the range you have defined.

Try running this variation, which will tell you which value it cannot find:
VBA Code:
Sub todatabase2()

Dim ID, GR, Departamento, Semana, Comentarios, Mes As Range

With Sheets("Form")
    ID = Range("F1")
    GR = Range("A3")
    Departamento = Range("C3")
    Semana = Range("D3")
    Comentarios = Range("B25")
End With

On Error GoTo err_chk:
Set Mes = Sheets("Datalist").Cells(Range("E:E").Find(Semana, Lookat:=xlWhole).Row, 6)
On Error GoTo 0

With Sheets("Employee_List")
    Set RngCol = .Range("A2", .Cells(Rows.Count, 1).End(xlUp))
    LastRow = RngCol.Rows.Count
    .Range("A2:G" & LastRow + 1).Copy
End With

With Sheets("Historico")
    last_line = .Range("F" & Rows.Count).End(xlUp).Row + 1
    .Range("F" & last_line).PasteSpecial Paste:=xlPasteValues
    Do While Cells(last_line, 6) <> ""
        If .Range("F" & last_line) <> "" Then
            .Range("A" & last_line) = ID
            .Range("B" & last_line) = GR
            .Range("C" & last_line) = Departamento
            .Range("D" & last_line) = Semana
            .Range("M" & last_line) = Comentarios
            .Range("E" & last_line) = Mes
        End If
        last_line = last_line + 1
    Loop
End With

ThisWorkbook.Worksheets("Historico").Cells.EntireColumn.AutoFit

Exit Sub


err_chk:
    If Err.Number = 91 Then
        MsgBox "Cannot find value of " & Semana, vbOKOnly, "ERROR!!!"
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If
       
End Sub

Also, when posting your VBA code, please use Code Tags! Trying to read unformatted code can be cumbersome, especially when it gets a little lengthy.
See: How to Post Your VBA Code
This is great help as always, its says it cannot find the value, but it should.

please see

Overtime Request Form.V6 historic by employee ID.xlsm
EF
1WeekMonth
21January
32January
43January
54January
65February
76February
87February
98February
109March
1110March
1211March
1312March
1413March
1514April
1615April
1716April
1817April
1918April
2019May
2120May
2221May
2322May
2423May
2524June
2625June
2726June
2827June
2928July
3029July
3130July
3231July
3332August
3433August
3534August
3635August
3736September
3837September
3938September
4039September
4140September
4241October
4342October
4443October
4544October
4645November
4746November
4847November
4948November
5049December
5150December
5251December
5352December
Datalist
Cell Formulas
RangeFormula
E2:E53E2=SEQUENCE(52)
Dynamic array formulas.
 
Upvote 0
What is the saying the value it cannot find is?

Is it really in column E on sheet "Datalist"?

Are you sure it is EXACTLY the same? Things like extra spaces can cause things not to match.

What cell is it in?
 
Upvote 0
What is the saying the value it cannot find is?

Is it really in column E on sheet "Datalist"?

Are you sure it is EXACTLY the same? Things like extra spaces can cause things not to match.

What cell is it in?
The value is 29, I dont know if its too early to claim victory but i removed the array formula, sequence and it did work this time.

May its not exactly the same because of that. Ill let you know after a few trials, thanks again.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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