在excel 2010中将输入框添加到URL检查器宏

时间:2016-05-12 03:21:36

标签: excel vba excel-vba inputbox

我在网上找到了以下宏。 我想知道如何添加一个输入框,允许我告诉宏检查除A之外的其他列,并从A1以外的其他单元开始。

Sub Check_URLs()

    Dim cell As Range
    Dim IE As Object

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True

    For Each cell In Range("A1", Range("A" & Rows.Count).End(xlUp))

        If Left(cell.Value, 4) = "http" Then

            ' Goto web page
            IE.Navigate cell.Text

            Application.StatusBar = cell.Text & "  is loading. Please wait..."

            ' Wait until web page is loaded
            Do Until IE.ReadyState = 4 'READYSTATE_COMPLETE
            Loop

            Application.StatusBar = ""

            ' Look for text on web page
            If InStr(IE.Document.body.innerText, _
               "HTTP 404") > 0 Then
               cell.Offset(, 1).Value = "zzz"
               Else
               cell.Offset(, 1).Value = "Y"

            End If
        End If
    Next cell

    IE.Quit
    Set IE = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

转到here to learn about the InputBox。然后将该资源标记为包含指向完整Excel对象模型(以及其他办公应用程序)的链接,以供将来参考:)

  

Application.InputBox Method (Excel)

然后您可以从中创建InputBox并将其结果分配给Range对象:

Dim startRange as Range

Set startRange = Application.InputBox(Prompt:="Select starting cell...", _
                                      Default:="$A$1", _
                                      Type:=8)

然后,转到here了解如何查找"最后一行"在给定的范围/列/等中

  

Error in finding last used cell in VBA

最后,相应地调整For each cell...循环。

或者使用一个简单的输入框(这很可能需要额外的错误处理,例如,如果有人输入 Steve ?,或者是数字而不是字母,等)

Dim myColumnLetter as String
myColumnLetter = Application.InputBox("Input a column letter.", Default:="A")

' ### Here you may want to validate the input value, 
'     since it could cause errors if it's not a valid input
' ###

For Each cell In Range(myColumnLetter & "1", Range(myColumnLetter & Rows.Count).End(xlUp))