Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help - How do I import data from Excel into VB....

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I have a spreadsheet with various sheets and several cells of data on each sheet. I want to extract the data from these cells into various text boxes on my VB form. The spreadsheet name changes on a daily basis (as it has the date in its name).

Many thanks in advance.
 
Try this:
Option Explicit
Dim objExcel As Excel.Application
Dim objWorkbook As Excel.Workbook
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal HWnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal lpParameters _
As String, ByVal lpDirectory As String, ByVal nShowCmd _
As Long) As Long

Private Sub Command1_Click()
Dim i As Long
Dim n As Long
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
If Err.Number Then
Err.Clear
Set objExcel = CreateObject("Excel.Application")
If Err.Number Then
MsgBox "Can't open Excel."
End If
End If
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add
AppActivate "FlexGrid To Excel Demo"
For i = 0 To 3
MSFlexGrid1.Row = i
For n = 0 To 3
MSFlexGrid1.Col = n
objWorkbook.ActiveSheet.Cells(i + 1, n + 1).Value = MSFlexGrid1.Text
Next
Next
End Sub

Private Sub Form_Load()
Dim i As Integer
Dim n As Integer
Me.Caption = "FlexGrid To Excel Demo"
'Populate the FlexGrid with sample data
With MSFlexGrid1
.Rows = 1
.Cols = 4
'Add field headers
For i = 1 To 3
.Col = i
.Text = "Heading " & i
Next
'Add data
For i = 1 To 3
.Rows = .Rows + 1
.Row = .Rows - 1
.Col = 0
.Text = "Record " & i
For n = 1 To 3
.Col = n
.Text = "Row " & i & ",Col " & n
Next
Next
End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set objWorkbook = Nothing
Set objExcel = Nothing
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top