You are on page 1of 3

Excel Get Used Range Count and Insert Picture using VB Scripting

May 31, 2012ShivaPVLeave a commentGo to comments

Set objXL = CreateObject(Excel.Application) objXL.visible = True objXL.DisplayAlerts = false Set wkb = objXL.Workbooks.Open(ExcelFilePath) Set ws = wkb.Sheets(Sample) Used_RowCount = ws.UsedRange.Rows.Count Used_ColumnCount = ws.UsedRange.Rows.Count Next_Row_Range = Used_RowCount +3 Add_Column_Range = Used_ColumnCount + 10 Cell_Range = A & Next_Row_Range &:G & Add_Column_Range Set objRng = ws.Range(Cell_Range) Insert Picture.. objRng.Merge Set Picture = ws.Pictures.Insert(ImageFilePath) With Picture .Top = objRng.Top .Left = objRng.Left .Width = objRng.Width .Height = objRng.Height End With Insert text ws.Cells(Cell_Range).Value = Test value wkb.SaveAs ExcelFilePath wkb.Close objXL.Quit ******************************************************** Function FindLastCell() Dim lRow As Long, lCol As Integer, mRow As Long, mCol As Integer lCol = ActiveSheet.UsedRange.Columns.Count mRow = 0 For i = 1 To lCol lRow = Range(Cells(rows.count, i), Cells(rows.count, i)).End(xlUp).Row If lRow > mRow Then mRow = lRow mCol = i Else End If Next i FindLastCell = Range(Cells(mRow, mCol), Cells(mRow, mCol)).Address End Function

Methods of Reading Data from EXCEL Sheet -Using VBScript


May 31, 2012ShivaPVLeave a commentGo to comments

Method #1

Datatable.Importsheet path of the excel file.xls, source sheet ID/Name, desination sheet ID/Name Datatable.SetCurrentRow(1) Read Value from First Row value = datatable.Value(parameterName,Sheet ID/Name) To Read Value from All the rows Datatable.Importsheet path of the excel file.xls, source sheet ID/Name, desination sheet ID/Name For vRow = 1 To Datatable.GetRowCount Datatable.SetCurrentRow(vRow) value = datatable.Value(parameterName,Sheet ID/Name) Next Method #2 Dim objexcel, objWorkbook, objDriverSheet, columncount, rowcount set objexcel = Createobject(Excel.Application) Set objWorkbook = objExcel.WorkBooks.Open(path of the file.xls) Set objDriverSheet = objWorkbook.Worksheets(name of the sheet / Id of the sheet) columncount = objDriverSheet.usedrange.columns.count rowcount = objDriverSheet.usedrange.rows.count for i = 1 to colunmcount columnname = objDriversheet.cells(i,1) if columnname = knowncolumnname then for j = 1 to rowcount fieldvalue = objdriversheet.cells(j,i) next end if next Method #3 Function GetDataFromDB (strFileName, strSQLStatement) Dim objAdCon, objAdRs Set objAdCon = CreateObject(ADODB.Connection) objAdCon.Open DRIVER={Microsoft Excel Driver (*.xls)};DBQ=&strFileName & ;Readonly=True If Err <> 0 Then Reporter.ReportEvent micFail,Create Connection, [Connection] Error has occured. Error : & Err Set obj_UDF_getRecordset = Nothing Exit Function End If Set objAdRs = CreateObject(ADODB.Recordset) objAdRs.CursorLocation=3 set the cursor to use adUseClient disconnected recordset objAdRs.Open strSQLStatement, objAdCon, 1, 3 MsgBox objAdRs.fields(4).name While objAdRs.EOF=false For i=0 to objAdRs.Fields.count Msgbox objAdRs.fields(i) Next objAdRs.moveNext Wend If Err<>0 Then Reporter.ReportEvent micFail,Open Recordset, Error has occured.Error Code : & Err Set obj_UDF_getRecordset = Nothing

Exit Function End If Set objAdRs.ActiveConnection = Nothing objAdCon.Close Set objAdCon = Nothing End Function Example Set rsAddin = GetDatasFromDB(C:\Documents and Settings\gramesh\Desktop\Login.xls, Select * from [Login$])

You might also like