Search code examples
vbaexcelimacros

Looping with Excel VBA and iMacros


Alright, the scenario is this. I am working to move a client list from one backend manager to another, but unfortunately the one I am moving the list from (which contains addresses, phone numbers, notes about the account and general information about what each client likes) cannot export to excel. Hence where this begins to get tedious. My next step was trying something called iMacros for IE. The way I have it setup now is very rudimentary. I have 15 imacro macros (one for each field needing to be copied). The first one changes the page so that a loop is made and extracts the first field. Each of the other 14 only extract one field at a time. I am trying to put all the information for a single client on a single row, just separate columns for the piece of info gathered. Then go to the next row to prepare for the next client. It starts on row 3 currently, but If that needs to be changed, fine.

   dim iim1, iret, row


Sub Button1_Click()

    Set iim1 = CreateObject("imacros")
     iret = iim1.iimInit
    iret = iim1.iimDisplay("Submitting Data from Excel")

    row = ActiveCell.row

    Range("A3").Select

    iret = iim1.iimPlay("Login")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If




    clientloop1

End Sub

Sub clientloop1()


    Do Until ActiveSheet.UsedRange.Rows.Count = 4


    iret = iim1.iimPlay("Field1")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 1).Value = iim1.iimgetlastextract(0)


    iret = iim1.iimPlay("Field2")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 2).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field3")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 3).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field4")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 4).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field5")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 5).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field6")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 6).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field7")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 7).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field8")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 8).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field9")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 9).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field10")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 10).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field11")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 11).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field12")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 12).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field13")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 13).Value = iim1.iimgetlastextract(0)

     iret = iim1.iimPlay("Field14")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If
    Cells(row, 14).Value = iim1.iimgetlastextract(0)


     iret = iim1.iimPlay("Field15")
    If iret < 0 Then
        MsgBox iim1.iimgetlasterror()
    End If

    ActiveCell.Offset(1, 0).EntireRow.Range("A1").Select




    Loop

End Sub

"

When I execute the code as is, it does the first row correctly, but keeps overwriting the information with the next client instead of going to the next row and adding it there. Any help that can be provided, I appreciate. I'm stumped sadly. It's great to join this community finally, I can't tell you guys how many times a question on here was relevant to another issue and helped me out lol.

It's either this or copy and paste 1 at a time lol for a very large client list.


Solution

  • You haven't incremented the 'row' variable. You need to include "row = row + 1" right before "Loop", or if you really want to use the Activecell idea, you can put "row = ActiveCell.row" right before 'Loop' (although the other way makes more sense and will be faster)

    Even if you set row = ActiveCell.row, this won't change the row variable when you select a new cell. It simply sets the row variable to whatever cell is active at the time and then this value of row is set until it is changed.