Ver Mensaje Individual
  #6 (permalink)  
Antiguo 19/12/2010, 22:47
Avatar de lokoman
lokoman
 
Fecha de Ingreso: septiembre-2009
Mensajes: 502
Antigüedad: 14 años, 7 meses
Puntos: 47
Respuesta: tomar rango en excel

Puedes usar una sentencia SQL en un recordset:
"SELECT * FROM [HOJA$] WHERE [NOMBRE_COLUMNA]=" & 1

eso te cargaria todos los registros que sean "1", a partir de ahi lo copias en otro archivo excel, este seria el proceso:

1-Conectarse al archivo de excel
2-Leer los registros que sean "1"
3-Copiar los registros que sean "1" en un archivo excel nuevo

ejem.:
Código vb:
Ver original
  1. 'PARA OBTENER EL NOMBRE DE LA  HOJA ACTIVA
  2.                Dim ObjExcel As Object
  3.                 Set ObjExcel = CreateObject("Excel.Application")
  4.                
  5.                 'Abre los libros
  6.                ObjExcel.Workbooks.Open "RUTA DEL ARCHIVO DE EXCEL"
  7.                
  8.                 With ObjExcel
  9.                     .ActiveSheet.Select ' La selecciona
  10.                    NombreHoja = UCase(.ActiveSheet.Name)
  11.                 End With
  12.                
  13.                 'Descarga la referencia y cierra el Excel
  14.                ObjExcel.Quit
  15.                 Set ObjExcel = Nothing
  16.                
  17.                 DoEvents
  18.                        
  19. 'CONEXION A LOS ARCHIVOS EXCEL
  20.               Dim Conn As ADODB.Connection
  21.                Dim Rec As New ADODB.Recordset
  22.  
  23.                 Set Conn = New ADODB.Connection
  24.                
  25.                 With Conn
  26.                     .Provider = "MSDASQL"
  27.                     .ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};" & _
  28.                     "DBQ=" & Trim(RUTA DEL ARCHIVO DE EXCEL) & "; ReadOnly=False;"
  29.                     Conn.Open
  30.                 End With
  31.  
  32.                 Rec.Source = "SELECT * FROM [" & Trim(NombreHoja) & "$] WHERE [COLUMNA]=" & 1
  33.                 Rec.Open , Conn, adOpenForwardOnly, adLockReadOnly
  34.        
  35.                 If Not Rec.EOF Then
  36.                         Dim xlApp As Excel.Application
  37.                         Dim xlBook As Excel.Workbook
  38.                         Dim xlSheet As Excel.Worksheet
  39.                         Dim CellCnt As Double
  40.                         Dim I, J As Double
  41.  
  42.    
  43.                         Set xlApp = New Excel.Application 'asignar las referencias a las variables
  44.                        Set xlBook = xlApp.Workbooks.Add
  45.                         Set xlSheet = xlBook.Worksheets.Add
  46.    
  47.                         I = 2      
  48.                         CellCnt = 1
  49.  
  50.                         DO WHILE REC.EOF=FALSE
  51.                              xlSheet.Cells(I, CellCnt) = REC!COLUMNA
  52.                              I = I + 1
  53.                             DOEVENTS
  54.                         LOOP
  55.  
  56. 'Ajustar todas las columnas
  57.                        For J = 1 To lsvData.ColumnHeaders.Count
  58.                             xlSheet.Columns(J).AutoFit
  59.                         Next J
  60.  
  61. 'Salvar la hoja de excel
  62.                        xlSheet.Name = "EXPORT"
  63.                         xlSheet.SaveAs "EXPORT.XLS"
  64.                         MsgBox "Consulta exportada a Excel!!", vbInformation
  65.  
  66.                         xlBook.Close
  67.                         if rec.state=1 then rec.close
  68.                         xlApp.Quit
  69.                         Set xlApp = Nothing
  70.                         Set xlBook = Nothing
  71.                         Set xlSheet = Nothing
  72.                         Set rec=nothing
  73.                     End if

Ajusta el code