Question : insert rows and data in Excel from VB6

I need to search for value in a specific column on a spreadsheet to locate a match to variable in vb6.  If found, I need to move to the last row containg that value and insert a row of data.

Example, I need to locate loom no. 21 as matched to the value in my VB6 variable, When I locate the value in the second row, row 42, I will insert a row of data below that.

I need specific example please.  Thank you.

Spreadsheet: Destination
====================
    A                B                  C                         D
::::::::::::::::
1: loomNo
2: ______
40: 7
41: 21
42: 21
43: 6
44: 6

Answer : insert rows and data in Excel from VB6

I found an old copy of VB6 so i've installed it and got it working with your code from above

What Library are you referencing for Excel?  I used "Microsoft Excel 11.0 Object Library" which should be the one you are using too.

I got the following output from the code below:

Looking in column 3 for the value: 10
 8
 6
 7
 3
 3
 1
Found Value: 10
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
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
Random Solutions  
 
programming4us programming4us