visual basic 6+_excel
TRANSCRIPT
-
7/30/2019 Visual Basic 6+_Excel
1/5
Consider the following code below. This code stores the filename the user returns inthe public variable Fname. If the file is already open, then the sub will not open the
file again and if the user cancels the filename procedure then the sub exits
Code:
Public Fname
Sub OpenF()
Dim wb As Workbook
Fname = Application.GetOpenFilename("Excel-files,*.xls", , "Please
open your file")
'no file selected
If Fname = False Then Exit Sub
For Each wb In Application.Workbooks
If wb.Path & "\" & wb.Name = Fname Then
MsgBox "File " & wb.Name & " is already open"
Exit Sub
End If
Next
Workbooks.Open (Fname)
End Sub
The code is totally self explanatory, In the load event we're going to open the new instance of the excellibrary and our excel file book1.xls will be accessible from our code. Then we'll use Command1 to retrievedata from book1, please note that you must have some data in the excel file. Similarly Command2 is used toput/replace the data in the excel sheet cells.
'do declare these vari ables you need to add a refe rence
'to the microsoft excel 'xx' object l ib rary.
'you need two text boxes and two command buttons
'on the form, an excel fi le in c: \book1.xls
Dim xl As New Excel.Appli cat ion
Dim xlsheet As Excel .Worksheet
Dim xlwbook As Excel .Workbook
Pr ivate Sub Command1_Click ( )
'the benif i t of placing numbers in (row, col) is that you
'can loop through different directions if required. I could
'have used column names l ik e "A1" 'et c .
Text1.Text = xlsheet.Cel l s (2 , 1) ' row 2 col 1
Text2.Text = xlsheet.Cell s( 2, 2) ' row 2 col 2
-
7/30/2019 Visual Basic 6+_Excel
2/5
'don't forget to do this or you'l l not be able to open
'book1.xls again, unti l l you restart you pc.
xl.Act iveWorkbook.Close False, "c:\book1.xl s"
xl.Quit
End Sub
Pr ivate Sub Command2_Click ( )
xlsheet.Cel ls (2 , 1) = Text1.Text
xlsheet.Cel ls (2 , 2) = Text2.Text
xlwbook.Save
'don't forget to do this or you'l l not be able to open
'book1.xls again, unti l l you restart you pc.
xl.Act iveWorkbook.Close False, "c:\book1.xl s"
xl.Quit
End Sub
Pr iva te Sub Form_Load()
Set xlwbook = xl .Workbooks.Open("c : \book1.xls" )
Set xlsheet = xlwbook.Sheets. I tem(1)
End Sub
Pr iva te Sub Form_Unload(Cancel As Int eger)
Set xlwbook = Nothing
Set x l = Nothing
End Sub
Am new to this vb excel, I want a sample code to get me started in my vb program inwhich it:
1. Opens An excel file.2. Find the last row with blank values and append values on that blank row next to thelast row with values.
3. Save. or if it is possible autosave in every 1 minute.
Dim xlApp As Excel.ApplicationDim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Set xlApp = New Excel.ApplicationxlApp.Visible = True
Set xlWB = xlApp.Workbooks.Open("C:\Test.xls")
-
7/30/2019 Visual Basic 6+_Excel
3/5
Dim BlankRow As Long
BlankRow = Cells.SpecialCells(xlCellTypeLastCell).Row 'Finds first blank row in Column ACells(BlankRow, 1).Value = "SenderNo"
Cells(BlankRow, 2).Value = "DateTime"Cells(BlankRow, 3).Value = "SenderMg"
Call xlWB.SaveCall xlApp.QuitSet xlApp = NothingSet xlWB = NothingSet xlWS = Nothing
Declarations:
Dim ExcelApp As Excel.ApplicationDim ExcelWorkbook As Excel.WorkbookDim ExcelSheet As Excel.WorksheetDim MyMonth As StringDim MyYear As StringDim Mydirectory As StringDim MyExtension As StringDim MyFileName As StringDim FileCheck As String
Codes:
Private Sub Form_Initialize()'get monthMyMonth = Format(Now, "mm")'get yearMyYear = Format(Now, "yyyy")
'working directoryMydirectory = "c:\YourDirectory\"'Excels extensionMyExtension = ".xls"'complete path and file nameMyFileName = Mydirectory + MyMonth + "_" + MyYear + MyExtension
On Error Resume Next
-
7/30/2019 Visual Basic 6+_Excel
4/5
'create Excel objectSet ExcelApp = CreateObject("Excel.Application")'if file exists, place file name in FileCheckFileCheck = Dir$(MyFileName)
If FileCheck = MyMonth + "_" + MyYear + MyExtension Then
'Workbook exists, open itSet ExcelWorkbook = ExcelApp.Workbooks.Open(MyFileName)Set ExcelSheet = ExcelWorkbook.Worksheets(1)
Else'Workbook doesn't exist, create new workbookSet ExcelWorkbook = ExcelApp.Workbooks.AddSet ExcelSheet = ExcelWorkbook.Worksheets(1)ExcelApp.Columns("A:C").ColumnWidth = 20ExcelSheet.Cells(1, 1).Value = "Your"ExcelSheet.Cells(1, 2).Value = "Columb"ExcelSheet.Cells(1, 3).Value = "Headers"
ExcelApp.Range("A1:C1").SelectExcelApp.Selection.Font.Bold = True'write some dataExcelSheet.Cells(9, 2).Value = "123"
End IfEnd Sub
Private Sub Form_Unload(Cancel As Integer)If FileCheck = MyMonth + "_" + MyYear + MyExtension Then
'Save existing workbookExcelWorkbook.Save
Else'Save new workbookExcelWorkbook.SaveAs MyFileName
End If
'Close ExcelExcelWorkbook.Close savechanges:=FalseExcelApp.QuitSet ExcelApp = NothingSet ExcelWorkbook = NothingSet ExcelSheet = Nothing
End Sub
-
7/30/2019 Visual Basic 6+_Excel
5/5
Capture cell content from Excel into VB Textbox or Label
Dim xl As Excel.ApplicationDim strFileName As String
'under project_references_ check Microsoft Excel Objects.
Private Sub Command1_Click()Set xl = CreateObject("excel.Application")
xl.Workbooks.Open ("C:\my documents\test_bed.xls") ' substitute your f ile here
xl.Visible = FalseText1 = xl.Worksheets("Sheet1").Range("A1").ValueText2 = xl.Worksheets("Sheet1").Range("A2").Value
ActiveWorkbook.Save
xl.Application.Quit
Set xl = Nothing
End Sub