I'm having a really bad time trying to understand why I get an error which I thing should not appear. I'm using Office 2007. I have two sheets in Excel, "Ver" and "Encargado", in "Ver" sheet I have one cell in which to put a number and then I press one button. The code inside the button is:
Private Sub CommandButton1_Click()
Dim buscar As Range
dato = Range("b2").Value
If dato = "" Then Exit Sub
Sheets("Encargado").Select
Set buscar = Range("b2:b12").Find(What:=dato, LookIn:=xlFormulas, SearchOrder:=xlByRows)
If Not buscar Is Nothing Then
buscar.Activate
ActiveCell.Offset(, -1).Copy Destination:=Ver.Cells(4, 2)
Sheets("Ver").Select
Else
Sheets("Ver").Select
MsgBox "Encargado no encontrado"
Exit Sub
End If
End Sub
In "Encargado" sheet and inside the range b2:b12 there are only numbers beetwen 101 and 111, and when I put the number 117 as an input (for example) I think it should get into the else part as it shouldn't have found any coincidence, but it gets into the if part, why? and I get an error saying "Error en el método activate de la clase Range" (As you noticed I am using Office in spanish, and programming as well with spanish variables). When I run line by line the error appears in the line "buscar.Activate". Does anyone know where is the error? In the case you want to see it, here is the file: https://www.dropbox.com/s/j1b39fqy971n7lf/Todo.xlsm Thanks.
You have not defined the sheet in find function so its referring ver sheet. Use this code.
Private Sub CommandButton1_Click()
Dim buscar As Range
'Application.ScreenUpdating = False
dato = Range("b2").Value
If dato = "" Then Exit Sub
Set buscar = Sheets("Encargado").Range("b2:b12").Find(What:=dato, LookIn:=xlValues, SearchOrder:=xlByRows)
If Not buscar Is Nothing Then
'Encargado.Cells.Find(What:=Ver.Cells(2, 2), After:=Range("b2"), LookIn:=xlFormulas, SearchOrder:=xlByRows).Select
'desplazamiento en fila, desplazamiento en columna, positivo: hacia abajo, hacia la derecha
'buscar.Activate
c = buscar.Row
Sheets("Encargado").Range("a" & c).Copy
Sheets("Ver").Range("b4").PasteSpecial
Else
Sheets("Ver").Select
MsgBox "Encargado no encontrado"
Exit Sub
End If
'Application.ScreenUpdating = True
End Sub
EDIT: As pointed out in the comments below, the Range
object DOES have an .Activate
method: http://msdn.microsoft.com/en-us/library/office/ff837085(v=office.15).aspx
The following should get you where you're going, but out of curiosity -- have you looked into Excel's built-in buscar
is a
Range
object, which does not have an
Activate
method.
=VLOOKUP
functionality? It might save you from requiring VBA here...
Option Explicit
Private Sub CommandButton1_Click()
Dim TerritorioNum As Long, LastRow As Long
Dim TerritorioRange As Range, FoundRange As Range
Dim VerSheet As Worksheet, EncSheet As Worksheet
'set references up-front
Set VerSheet = ThisWorkbook.Worksheets("Ver")
Set EncSheet = ThisWorkbook.Worksheets("Encargado")
With EncSheet
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set TerritorioRange = .Range(.Cells(1, 1), .Cells(LastRow, 2))
End With
TerritorioNum = VerSheet.Cells(2, 2).Value
'find the territory number
Set FoundRange = TerritorioRange.Find(TerritorioNum)
If FoundRange Is Nothing Then
MsgBox ("Encargado no encontrado")
Exit Sub
Else
VerSheet.Cells(4, 2) = EncSheet.Cells(FoundRange.Row, FoundRange.Column - 1).Value
End If
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.