Buscar si hay más valores en la misma fila

08/03/2005 - 01:34 por sanchez.javi | Informe spam
Hola

Estoy intentando buscar si hay más valores en la misma fila (hacia la
derecha) de una hoja de cálculo y las siguientes, pero se me está
complicando mucho el código y soy novato en esto.

Estaba haciendo algo como esto:

For hoja = 1 To 5 Step 1
Columna = 4
For Contador = 1 To 40 Step 1
' Comprobar si hay más valores en la base de datos aunque haya espacios
por medio
nohaymas = 0
Dim rangoitems As Range

Set r1 = Workbooks("BdD.xls").Worksheets("hoja " &
hoja).Range(Cells(nfila, Columna), Cells(nfila, 256))
Set r2 = Workbooks("BdD.xls").Worksheets("hoja " & hoja +
1).Range(Cells(nfila, 4), Cells(nfila, 256))
Set r3 = Workbooks("BdD.xls").Worksheets("hoja " & hoja +
2).Range(Cells(nfila, 4), Cells(nfila, 256))
Set r4 = Workbooks("BdD.xls").Worksheets("hoja " & hoja +
3).Range(Cells(nfila, 4), Cells(nfila, 256))
Set r5 = Workbooks("BdD.xls").Worksheets("hoja " & hoja +
4).Range(Cells(nfila, 4), Cells(nfila, 256))
Set rangoitems = Union(r1, r2, r3, r4, r5)

For Each v In rangoitems
If v.Value <> "" Then
nohaymas = nohaymas + 1
End If
Next
If nohaymas=0 Then
End
End If

' Sigue el código para el caso de que nohaymas sea mayor que 0


La idea es que si la variable nohaymas se queda en 0 estará conseguido,
pero, aparte de que no funciona, se me plantea una duda, supongo que si
estoy en la hoja 3 al no existir "hoja 6" ni "hoja 7" el rango devolverá
un error ¿es así?

Muchas gracias

Javi

Preguntas similare

Leer las respuestas

#1 KL
08/03/2005 - 06:23 | Informe spam
Hola Javi,

Mira a ver si el siguiente macro hace lo q buscas.

Saludos,
KL

Sub test1()
Dim Hoja As Worksheet
Dim NoHayMas As Boolean
Dim Contador As Long
For Each Hoja In ThisWorkbook.Worksheets
With Hoja
If Not Intersect(.UsedRange, .Columns("D:AR")) Is Nothing Then
If WorksheetFunction.CountA(Rng) > 0 Then _
Contador = Contador + 1
End If
End With
Next Hoja
NoHayMas = (Contador = 0)
MsgBox NoHayMas
End Sub



"Javi" wrote in message
news:%
Hola

Estoy intentando buscar si hay más valores en la misma fila (hacia la
derecha) de una hoja de cálculo y las siguientes, pero se me está
complicando mucho el código y soy novato en esto.

Estaba haciendo algo como esto:

For hoja = 1 To 5 Step 1
Columna = 4
For Contador = 1 To 40 Step 1
' Comprobar si hay más valores en la base de datos aunque haya espacios
por medio
nohaymas = 0
Dim rangoitems As Range

Set r1 = Workbooks("BdD.xls").Worksheets("hoja " &
hoja).Range(Cells(nfila, Columna), Cells(nfila, 256))
Set r2 = Workbooks("BdD.xls").Worksheets("hoja " & hoja +
1).Range(Cells(nfila, 4), Cells(nfila, 256))
Set r3 = Workbooks("BdD.xls").Worksheets("hoja " & hoja +
2).Range(Cells(nfila, 4), Cells(nfila, 256))
Set r4 = Workbooks("BdD.xls").Worksheets("hoja " & hoja +
3).Range(Cells(nfila, 4), Cells(nfila, 256))
Set r5 = Workbooks("BdD.xls").Worksheets("hoja " & hoja +
4).Range(Cells(nfila, 4), Cells(nfila, 256))
Set rangoitems = Union(r1, r2, r3, r4, r5)

For Each v In rangoitems
If v.Value <> "" Then
nohaymas = nohaymas + 1
End If
Next
If nohaymas=0 Then
End
End If

' Sigue el código para el caso de que nohaymas sea mayor que 0


La idea es que si la variable nohaymas se queda en 0 estará conseguido,
pero, aparte de que no funciona, se me plantea una duda, supongo que si
estoy en la hoja 3 al no existir "hoja 6" ni "hoja 7" el rango devolverá
un error ¿es así?

Muchas gracias

Javi

email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida