简体   繁体   English

使用VBA在Excel中查找和激活功能时出错

[英]Error with find and activate functions in excel with vba

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. 我正在使用Office2007。在Excel中有两张纸,分别是“ Ver”和“ Encargado”。在“ Ver”纸中,我有一个单元格,其中要输入数字,然后按一个按钮。 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? 在“ Encargado”表中,在b2:b12范围内,只有数字beetwen 101和111,当我将数字117作为输入时(例如),我认为它应该进入else部分,因为它不应该具有发现了任何巧合,但它进入了if部分,为什么呢? 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). 我收到一条错误消息:“错误激活了范围范围错误”(您注意到我在西班牙语中使用Office,并使用西班牙语变量进行编程)。 When I run line by line the error appears in the line "buscar.Activate". 当我逐行运行时,错误出现在“ 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. 如果您要查看它,请参见以下文件: https : //www.dropbox.com/s/j1b39fqy971n7lf/Todo.xlsm谢谢。

You have not defined the sheet in find function so its referring ver sheet. 您尚未在find函数中定义工作表,因此未定义工作表的参考工作表。 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 编辑:如在下面的注释中指出, Range对象确实有一个.Activate方法: http : //msdn.microsoft.com/zh-cn/library/office/ff837085( .Activate .aspx

buscar is a Range object, which does not have an Activate method. buscar是一个 Range对象,它没有 Activate方法。 The following should get you where you're going, but out of curiosity -- have you looked into Excel's built-in =VLOOKUP functionality? 以下内容应该带您前往目的地,但出于好奇–您是否研究了Excel的内置=VLOOKUP功能? It might save you from requiring VBA here... 这可能使您不必在这里要求VBA ...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM