Public Sub Main()
Dim oXLApp As Excel.Application
Dim oXLBook As Excel.Workbook
Set oXLApp = New Excel.Application
oXLApp.Visible = True
Set oXLBook = oXLApp.Workbooks.Open("c:\Book1.xls") 'Open an existing workbook
FindValue oXLBook
oXLBook.Close False
oXLApp.Quit
End Sub
Public Sub FindValue(ByRef xlWorkbook As Excel.Workbook)
'Dim v As Object
Dim v As Variant
Dim x As Integer
Dim Col As Integer
Dim xlsheet As Excel.Worksheet
Set xlsheet = xlWorkbook.Sheets.Item(1)
Col = 3
v = 10
Debug.Print "Looking in column " & CStr(Col) & " for the value: " & CStr(v)
With xlsheet
.Activate
.Cells(1, 1).End(Excel.XlDirection.xlDown).Select
x = xlsheet.Application.ActiveCell.Row
While (x > 1)
If (.Cells(x, Col).Value = v) Then
Debug.Print "Found Value: " + CStr(v)
'.Rows(x + 1).Insert(Shift:=Excel.XlDirection.xlDown, CopyOrigin:=Excel.XlInsertFormatOrigin.xlFormatFromLeftOrAbove)
.Rows(x + 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
x = 1
Else
' put here to see how it is working
Debug.Print .Cells(x, Col).Value
End If
x = x - 1
Wend
End With
End Sub
|