|
Visual Basic-14
Excel y Visual Basic
Private Sub Command1_Click()
Dim xls As Object
Set xls = CreateObject("Excel.Application")
xls.Visible = False
xls.Workbooks.Open "c:\excelvb\source.xls"
xls.Range("a1").Value = Text1.Text
Text2.Text = xls.Range("a3").Value
End Sub
Private Sub Command2_Click()
Archivo = App.Path & "\source.xls"
Set xls = CreateObject("Excel.Application")
xls.Workbooks.Open Archivo
xls.Visible = False
xls.Worksheets("Hoja1").Activate
xls.Worksheets("Hoja1").Range("A1").Value = 666
xls.Worksheets("Hoja1").Range("A2").Value = 777
xls.Worksheets("Hoja1").Range("A3").Value = 888
'Para leer
' ValorCelda=pxls.Worksheets("Hoja1").Range("A3").Value
xls.ActiveWorkbook.Close SaveChanges:=True
End Sub
|
Introducir y leer
datos de una celdilla de Excel
Tenemos una Hoja de Cálculo Excel llamada
Ejemplo.xls y ubicada en la misma carpeta que la aplicación
de Visual Basic.
En caso que esté en otra carpeta pondríamos:
Archivo = "C:\Carpeta\Ejemplo.xls"
Elegimos una Hoja (o Sheet), un rango, por ejemplo:
A3 y un valor.
Al pulsar el botón el valor se insertará en la celda
elegida o bien se leerá de ella, según el botón
pulsado.

Private Sub Command1_Click()
h = Text1.Text
l = Text2.Text
n = Text3.Text
Valor = Text4.Text
hoja = "Hoja" & CStr(h)
rango = Text2.Text & Text3.Text
Archivo = App.Path & "\Ejemplo.xls"
Set xls = CreateObject("Excel.Application")
xls.Workbooks.Open Archivo
xls.Visible = False
xls.Worksheets(hoja).Activate
xls.Worksheets(hoja).Range(rango).Value = Valor
xls.ActiveWorkbook.Close SaveChanges:=True
MsgBox ("Dato introducido.")
End Sub
Private Sub Command2_Click()
h = Text5.Text
l = Text6.Text
n = Text7.Text
hoja = "Hoja" & CStr(h)
rango = Text6.Text & Text7.Text
Archivo = App.Path & "\Ejemplo.xls"
Set xls = CreateObject("Excel.Application")
xls.Workbooks.Open Archivo
xls.Visible = False
xls.Worksheets(hoja).Activate
Text8.Text = xls.Worksheets(hoja).Range(rango).Value
xls.ActiveWorkbook.Close
MsgBox ("Dato leido.")
End Sub
|
Otro: ExcelExport.zip (14 K)
|