You are on page 1of 5

Consider the following code below.

This code stores the filename the user returns in the 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 excel library and our excel file book1.xls will be accessible from our code. Then we'll use Command1 to retrieve data from book1, please note that you must have some data in the excel file. Similarly Command2 is used to put/replace the data in the excel sheet cells.
'do declare these var iab les you need to add a refe rence ' to the microsof t excel 'xx ' object l i b ra ry .

'you need two text boxes and two command buttons 'on the form, an excel f i l e in c: \book1.x l s

Dim xl As New Excel .Appl i ca t i on Dim xlsheet As Excel .Worksheet Dim xlwbook As Excel .Workbook

Pr ivate Sub Command1_Cl ick ( ) ' the beni f i t of plac ing numbers in ( row, col ) is that you requi red . I could

'can loop through di f f e ren t direc t i ons i f 'have used column names l i ke "A1" 'e tc .

Text1.Text = xlsheet .Ce l l s ( 2 , Text2.Text = xlsheet .Ce l l s ( 2 ,

1) ' row 2 col 1 2) ' row 2 col 2

'don ' t forget to do th i s or you' l l 'book1.x l s again , unt i l l

not be able to open

you restar t you pc.

xl .Ac t i veWorkbook.C lose False , "c : \book1.x l s " xl .Qu i t End Sub

Pr ivate Sub Command2_Cl ick ( ) xlsheet .Ce l l s ( 2 , xlsheet .Ce l l s ( 2 , xlwbook.Save 1) = Text1.Text 2) = Text2.Text

'don ' t forget to do th i s or you' l l 'book1.x l s again , unt i l l

not be able to open

you restar t you pc.

xl .Ac t i veWorkbook.C lose False , "c : \book1.x l s " xl .Qu i t End Sub

Pr ivate Sub Form_Load() Set xlwbook = xl .Workbooks.Open("c : \ book1.x l s " ) Set xlsheet = xlwbook.Sheets . I t em(1) End Sub

Pr ivate Sub Form_Unload(Cancel As In teger ) Set xlwbook = Nothing Set xl = Nothing End Sub

Am new to this vb excel, I want a sample code to get me started in my vb program in which it: 1. Opens An excel file. 2. Find the last row with blank values and append values on that blank row next to the last row with values. 3. Save. or if it is possible autosave in every 1 minute.

Dim xlApp As Excel.Application Dim xlWB As Excel.Workbook Dim xlWS As Excel.Worksheet Set xlApp = New Excel.Application xlApp.Visible = True Set xlWB = xlApp.Workbooks.Open("C:\Test.xls")

Dim BlankRow As Long BlankRow = Cells.SpecialCells(xlCellTypeLastCell).Row 'Finds first blank row in Column A Cells(BlankRow, 1).Value = "SenderNo" Cells(BlankRow, 2).Value = "DateTime" Cells(BlankRow, 3).Value = "SenderMg" Call xlWB.Save Call xlApp.Quit Set xlApp = Nothing Set xlWB = Nothing Set xlWS = Nothing

Declarations:

Dim ExcelApp As Excel.Application Dim ExcelWorkbook As Excel.Workbook Dim ExcelSheet As Excel.Worksheet Dim MyMonth As String Dim MyYear As String Dim Mydirectory As String Dim MyExtension As String Dim MyFileName As String Dim FileCheck As String

Codes:

Private Sub Form_Initialize() 'get month MyMonth = Format(Now, "mm") 'get year MyYear = Format(Now, "yyyy") 'working directory Mydirectory = "c:\YourDirectory\" 'Excels extension MyExtension = ".xls" 'complete path and file name MyFileName = Mydirectory + MyMonth + "_" + MyYear + MyExtension On Error Resume Next

'create Excel object Set ExcelApp = CreateObject("Excel.Application") 'if file exists, place file name in FileCheck FileCheck = Dir$(MyFileName) If FileCheck = MyMonth + "_" + MyYear + MyExtension Then 'Workbook exists, open it Set ExcelWorkbook = ExcelApp.Workbooks.Open(MyFileName) Set ExcelSheet = ExcelWorkbook.Worksheets(1) Else 'Workbook doesn't exist, create new workbook Set ExcelWorkbook = ExcelApp.Workbooks.Add Set ExcelSheet = ExcelWorkbook.Worksheets(1) ExcelApp.Columns("A:C").ColumnWidth = 20 ExcelSheet.Cells(1, 1).Value = "Your" ExcelSheet.Cells(1, 2).Value = "Columb" ExcelSheet.Cells(1, 3).Value = "Headers" ExcelApp.Range("A1:C1").Select ExcelApp.Selection.Font.Bold = True 'write some data ExcelSheet.Cells(9, 2).Value = "123" End If End Sub Private Sub Form_Unload(Cancel As Integer) If FileCheck = MyMonth + "_" + MyYear + MyExtension Then 'Save existing workbook ExcelWorkbook.Save Else 'Save new workbook ExcelWorkbook.SaveAs MyFileName End If 'Close Excel ExcelWorkbook.Close savechanges:=False ExcelApp.Quit Set ExcelApp = Nothing Set ExcelWorkbook = Nothing Set ExcelSheet = Nothing End Sub

Capture cell content from Excel into VB Textbox or Label


Dim xl As Excel.Application Dim 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 file here xl.Visible = False Text1 = xl.Worksheets("Sheet1").Range("A1").Value Text2 = xl.Worksheets("Sheet1").Range("A2").Value ActiveWorkbook.Save xl.Application.Quit Set xl = Nothing End Sub