Está en la página 1de 1

C�digo Visual Basic para importar datos Excel

Private Sub LeerExcel()

'dimensiones
Dim xlApp As Excel.Application
Dim xlLibro As Excel.Workbook
Dim xlHoja As Excel.Worksheet
Dim varMatriz As Variant
Dim lngUltimaFila As Long

'abrir programa Excel


Set xlApp = New Excel.Application
'xl.Visible = True

'abrir el archivo Excel


(archivo en otra carpeta)
Set xlLibro = xlApp.Workbooks.Open _
("c:\Fax2.xls", True, True, , "")

'abrir el archivo Excel


(archivo en la misma carpeta)
Set xlLibro = xlApp.Workbooks.Open(App.Path & _
"\Fax2.xls", True, True, , "")
Set xlHoja = xlApp.Worksheets("Hoja1")

'1. Si conoces el rango a leer


'varMatriz = xlHoja.Range("A1:C10").Value

'2. Si no conoces el rango


lngUltimaFila = _
Columns("A:A").Range("A65536").End(xlUp).Row

varMatriz = xlHoja.Range(Cells(1, 1), _


Cells(lngUltimaFila, 1))

'utilizamos los datos...


Text1.Text = varMatriz(27, 1)

'cerramos el archivo Excel


xlLibro.Close SaveChanges:=False
xlApp.Quit

'reset variables de los objetos


Set xlHoja = Nothing
Set xlLibro = Nothing
Set xlApp = Nothing

End Sub

También podría gustarte