如何在VBA中处理列表?

时间:2016-01-25 00:51:03

标签: excel excel-vba vba

所以我有VBA代码,我有一个单元格的引用。每次宏循环时,有没有可能的方法来处理数据列表?例如,第一个循环我需要它来引用单元格A1,然后是第二个循环单元格A2,依此类推

Dim IE As Object
Dim strURL As String
Dim objelement As Object
Sub login()
' Add reference to Microsoft Internet Controls
' Add reference to Microsoft HTML Object Library


Set IE = New SHDocVw.InternetExplorer
IE.Visible = True
IE.navigate "https://minecraft.net/profile"

While IE.Busy
DoEvents
Wend
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
 Loop
IE.document.getElementById("username").Value = "DYNAMIC CELL COLUMN A"
 IE.document.getElementById("password").Value = "DYNAMIC CELL COLUMN B"

Dim htmlForm As HTMLFormElement
Set htmlForm = IE.document.getElementById("loginForm")
 htmlForm.submit



Application.Wait (Now + #12:00:03 AM#)

IE.navigate "https://minecraft.net/profile/password"
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
IE.document.getElementById("oldPassword").Value = "DYNAMIC CELL COLUMN B"
IE.document.getElementById("password").Value = "DYNAMIC CELL COLUMN C"
IE.document.getElementById("passwordConfirmation").Value = "DYNAMIC CELL COLUMN C"
With IE.document

Set elems = .getElementsByTagName("input")
For Each e In elems

    If (e.getAttribute("value") = "Submit Query") Then
        e.Click
        Exit For
    End If

Next e


Set elems = .getElementsByTagName("a")
For Each e In elems

    If (e.getAttribute("value") = "Log out") Then
        e.Click
        Exit For
    End If

Next e

End With
End Sub

2 个答案:

答案 0 :(得分:1)

可以使用For ... Each循环:

Sub LoopEachCellInRange()

    Dim MyRange As Range
    Dim MyCell As Range
    Set MyRange = ThisWorkbook.Worksheets("Sheet1").Range("A1:A20")

    For Each MyCell In MyRange

        MyCell.Offset(, 1) = MyCell.Address

        If IsEmpty(MyCell) Then MyCell.Offset(, 2) = "It's empty"

        If MyCell.Row > 1 Then

            MyCell.Offset(, 3) = MyCell.Row + MyCell.Offset(-1).Row

            'Formula using R1C1 format.
            MyCell.Offset(, 4).FormulaR1C1 = "=SUM(R1C4:RC4)"

            'Formula using A1 format.
            MyCell.Offset(, 5).Formula = "=SUM(E1:" & MyCell.Offset(, 4).Address & ")"

        End If

    Next MyCell

End Sub

答案 1 :(得分:0)

刚刚掀起了这个,试一试

Sub loopDo()
    Do Until ActiveCell.Value = ""
        'whatever you want the code to do in each cell replaces this message box
        MsgBox (ActiveCell.Value)
        ActiveCell.Offset(1).Select
    Loop
End Sub