Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改VBA代码以应用于列中的所有行,而不仅仅是特定行_Vba_Excel - Fatal编程技术网

更改VBA代码以应用于列中的所有行,而不仅仅是特定行

更改VBA代码以应用于列中的所有行,而不仅仅是特定行,vba,excel,Vba,Excel,我有一个VBA脚本,用于检查单元格是否命名为“URL”,并在第2行第4列和第5列中输出结果 我想修改它,以便它检查C列中的所有单元格,并将其导出到D&E列,相对于刚刚检查的单元格 如果可以在每个单元格执行之间添加一个定时延迟,那就太好了。即 在C列第2行执行,打印结果 等一秒钟 在C列第3行执行,打印结果 等一秒钟 等等 Private更改为布尔值 专用子重定向检查器(ByVal url作为字符串) 将sh设置为工作表 设置sh=ActiveSheet 将http设置为新的WinHttp.Win

我有一个VBA脚本,用于检查单元格是否命名为“URL”,并在第2行第4列和第5列中输出结果

我想修改它,以便它检查C列中的所有单元格,并将其导出到D&E列,相对于刚刚检查的单元格

如果可以在每个单元格执行之间添加一个定时延迟,那就太好了。即

在C列第2行执行,打印结果 等一秒钟 在C列第3行执行,打印结果 等一秒钟

等等

Private更改为布尔值
专用子重定向检查器(ByVal url作为字符串)
将sh设置为工作表
设置sh=ActiveSheet
将http设置为新的WinHttp.WinHttpRequest
http.Option(WinHttpRequestOption_UserAgentString)=“Mozilla/4.0(兼容;MSIE 7.0;Windows NT 6.0)”
Option(WinHttpRequestOption_EnableRedirects)=False
“”清除现有信息
sh.Cells(2,4).Formula=“”
sh.Cells(2,5).Formula=“”
多芬特
“”如果缺少,请添加协议
如果(InStr(url,“:/”)=0),则
url=“http://”&url
如果结束
“”启动HTTP请求
http.Open“GET”,url
如果错误号为0,则
“”处理URL格式错误
sh.Cells(2,4).公式=修剪(错误描述)
出口接头
如果结束
http.Send
如果错误号为0,则
“”处理HTTP错误
sh.Cells(2,4).公式=修剪(错误描述)
出口接头
如果结束
“”显示HTTP响应信息
sh.Cells(2,4).Formula=http.Status&&http.StatusText
sh.Cells(2,5).Formula=http.GetResponseHeader(“位置”)
端接头
私有子工作表_更改(ByVal目标作为范围)
如果更改,则退出Sub
改变=真
将名称设置为字符串
出错时继续下一步
Name=Target.Name.Name
如果Name=“URL”,则
重定向检查器目标值
如果结束
错误转到0
更改=错误
端接头

您可以让代码像这样等待一秒钟:

Application.Wait(现在+时间值(“00:00:01”))

你只需要把它整合到你的循环中,这能回答你的问题吗?ps尝试谷歌:excel vba等待1秒

代码取自Achaibou Karim post:

你能举个例子说明你在找什么吗。你的问题似乎有点让人困惑。像这样工作:不是这样:
Private Changing As Boolean

Private Sub RedirectChecker(ByVal url As String)
Dim sh As Worksheet
Set sh = ActiveSheet

Dim http As New WinHttp.WinHttpRequest
http.Option(WinHttpRequestOption_UserAgentString) = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
http.Option(WinHttpRequestOption_EnableRedirects) = False

'' Clear existing info
sh.Cells(2, 4).Formula = ""
sh.Cells(2, 5).Formula = ""
DoEvents

'' Add protocol if missing
If (InStr(url, "://") = 0) Then
    url = "http://" & url
End If

'' Launch the HTTP request
http.Open "GET", url
If Err.Number <> 0 Then
    '' Handle URL formatting errors
    sh.Cells(2, 4).Formula = Trim(Err.Description)
    Exit Sub
End If
http.Send
If Err.Number <> 0 Then
    '' Handle HTTP errors
    sh.Cells(2, 4).Formula = Trim(Err.Description)
    Exit Sub
End If
'' Show HTTP response info
sh.Cells(2, 4).Formula = http.Status & " " & http.StatusText
sh.Cells(2, 5).Formula = http.GetResponseHeader("Location")
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
If Changing Then Exit Sub
Changing = True
Dim Name As String
On Error Resume Next
Name = Target.Name.Name
If Name = "URL" Then
    RedirectChecker Target.Value
End If
On Error GoTo 0
Changing = False
End Sub