Adecuación de macro para limitar el rango de extracción de datos

Tengo una macro la cual tengo en evento de hoja y me es funcional

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$5" Then
If UCase(Target.Value) <> "" Then
MsgBox "aqui busca"

Set h = ThisWorkbook.Sheets(Hoja31.Name) 'base de datos
Set h2 = ThisWorkbook.Sheets(Hoja32.Name) 'reporte produccion

Dim ultfiladatos As Long
Dim ultfilareporte As Long
ultfiladatos = h.Range("a" & Rows.Count).End(xlUp).Row
For cont = 2 To ultfiladatos
clave = h.Cells(cont, 6)
ref = h.Cells(cont, 1)

If ref = h2.[d4] & h2.[d5] & h2.[d6] Then
ultfilareporte = h2.Range("c" & Rows.Count).End(xlUp).Row
h2.Cells(ultfilareporte + 1, 3) = clave
End If
Next cont

ElseIf UCase(Target.Value) = "" Then
MsgBox "aqui limpia"
End If
End If
End Sub

el unico detalle que necesito y no he podido lograr.. Es esta parte

If ref = h2.[d4] & h2.[d5] & h2.[d6] Then
ultfilareporte = h2.Range("c" & Rows.Count).End(xlUp).Row
h2.Cells(ultfilareporte + 1, 3) = clave
End If

Aquí trae los datos a partir de la ultima celda con datos de la columna C

Yo quisiera que cuando me traiga los datos

Este sea en el rango disponible de

C12:C81 (este rango que sea para extraer los datos)

Así si vuelvo a aplicar el código, no se me salga de este rango

Respuesta
2

Utiliza esto:

  For i = 12 To 81
    If h2.Range("C" & i).Value = "" Then
      ultfilareporte = i
      Exit For
    End If
  Next
  h2.Cells(ultfilareporte, 3) = clave

Una duda, dan... Ese código que me compartes. Quito lo que tengo hecho y coloco el que me comparte? 

Así:

  If ref = h2.[d4] & h2.[d5] & h2.[d6] Then
    For i = 12 To 81
      If h2.Range("C" & i).Value = "" Then
        ultfilareporte = i
        Exit For
      End If
    Next
    h2.Cells(ultfilareporte, 3) = clave
  End If

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas