Saltar al contenido
View in the app

A better way to browse. Learn more.

Ayuda Excel

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Variable con Rango de Celdas (Cells)

publicado

Hola buenas días,

Mi consulta trata sobre introducir una variable para un rango de celdas.

El codigo es el siguiente

Sub Macro1()
Dim tablaRef As Range
Dim UltimaFila As Long
Dim UltimaColumna As Long

'Recogemos valores para las variables de UltimaFila y UltimaColumna
    UltimaFila = Sheets(Hoja1).Range("C3").End(xlDown).Row
    UltimaColumna = Sheets(Hoja1).Range("C3").End(xlToRight).Column
    
    Set tablaRef = Sheets("Hoja1").Range(Cells(2, 1), Cells(UltimaFila, UltimaColumna))
    
	tablaTarifasRef.Select

End Sub

 

Los resultados de las variables UltimaFila = 205 y UltimaColumna = 24

El problema viene al intentar dar a la variable tablaRef su rango, me salta el error 1004 en tiempo de ejecución. Error definido por la aplicación y el objeto.

En cambio si utilizo el siguiente ejemplo si me lo permite, pero no me sirve porque quiero definir tanto la ultima fila como la última columna. 

Set tablaRef = Sheets("Hoja1").Range("A2:X" & UltimaFila)

Es por el objeto Cells? Me podéis aclarar el porque de ese error?

Muchas gracias por adelantado!!

Editado el por sergiovieri

Featured Replies

publicado
Sub Macro1()
    Dim tablaRef As Range
    Dim UltimaFila As Long
    Dim UltimaColumna As Long

    ' Recogemos valores para las variables de UltimaFila y UltimaColumna
    With Sheets("Hoja1")
        UltimaFila = .Cells(.Rows.Count, "C").End(xlUp).Row
        UltimaColumna = .Cells(3, .Columns.Count).End(xlToLeft).Column
        Set tablaRef = .Range(.Cells(3, 3), .Cells(UltimaFila, UltimaColumna))
    End With
    
    ' Seleccionamos la tabla de referencias
    tablaRef.Select
End Sub

No lo he probado, prueba y comenta

publicado
  • Autor
hace 3 horas, JSDJSD dijo:
Sub Macro1()
    Dim tablaRef As Range
    Dim UltimaFila As Long
    Dim UltimaColumna As Long

    ' Recogemos valores para las variables de UltimaFila y UltimaColumna
    With Sheets("Hoja1")
        UltimaFila = .Cells(.Rows.Count, "C").End(xlUp).Row
        UltimaColumna = .Cells(3, .Columns.Count).End(xlToLeft).Column
        Set tablaRef = .Range(.Cells(3, 3), .Cells(UltimaFila, UltimaColumna))
    End With
    
    ' Seleccionamos la tabla de referencias
    tablaRef.Select
End Sub

No lo he probado, prueba y comenta

Genial, me funcionó, mil gracias!!

Archivado

Este tema está ahora archivado y está cerrado a más respuestas.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.