最近秋招,搞了个脚本批量打开网申页面,看看进度
一、VBA脚本(20250907更新)
- 投递多了发现每更新一次就要导出网址,也麻烦。
- 那就直接在Excel里写vba打开吧,快一点。
- 其实一开始也是这么想的,但我办公室的电脑office抽风,开不了VBA。倒是在宿舍的电脑就可以正常操作了。
- 从自动化的角度上看,确实是vba便捷,只是处理语句过于繁琐的说,遍地IF ELSE。
- 记得另存为Excel启用宏的工作簿(*.xlsm)。
- 默认xlsx格式退出后不会保存VBA宏
在Excel启用开发工具选项卡
- 点击「文件」>「选项」>「自定义功能区」。
- 在右侧的「主选项卡」列表中,勾选「开发工具」。
插入按钮控件
- 进入「开发工具」选项卡。
- 「插入」,在「表单控件」中选择「按钮」。
- 右键「指定宏」,并「新建」。
VBA脚本(VBA宏代码)
需求
- 预检查非链接内容:首先扫描F列(从F2单元格开始处理,跳过F1单元格),识别所有非链接内容,并询问用户是否继续。
- 如果F列只有一行数据(即只有F1有内容),会提示用户没有需要处理的数据。
分类处理:
- 有效链接(以http://或https://开头):正常打开
- 非链接内容(如"国聘"、"智联"):记录并报告
- 无效链接(格式正确但无法打开):记录并报告
- 详细报告:操作完成后,会弹窗显示所有未能打开的内容,每项占一行,并标注原因(非链接或打开失败)。
- 错误处理:使用On Error Resume Next防止单个链接错误中断整个流程。
示例报告格式
以下内容未能打开:
国聘 (非链接)
智联 (非链接)
https://无效网址示例.com (打开失败)
脚本程序
Sub OpenAllUrls()
' 声明变量
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Dim lastRow As Long
Dim url As String
Dim nonUrlList As String
Dim nonUrlCount As Integer
Dim msgResult As VbMsgBoxResult
' 设置要操作的工作表,根据你的实际表名修改
Set ws = ThisWorkbook.Worksheets("Sheet1")
' 找到F列最后一行有数据的行
lastRow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
' 检查是否只有一行数据(F1)
If lastRow < 2 Then
MsgBox "F列没有需要处理的数据(从F2开始)", vbInformation
Exit Sub
End If
' 设置要遍历的单元格范围:F2 到 F最后一行
Set rng = ws.Range("F2:F" & lastRow)
' 初始化变量
nonUrlList = ""
nonUrlCount = 0
' 禁用屏幕更新和自动计算以提高宏运行速度
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' 首先检查有多少非链接内容
For Each cell In rng
url = cell.Value
If url <> "" And (Left(url, 7) = "http://" Or Left(url, 8) = "https://") Then
' 这是有效链接,跳过
ElseIf url <> "" Then
' 非链接内容
nonUrlCount = nonUrlCount + 1
If nonUrlList = "" Then
nonUrlList = cell.Value
Else
nonUrlList = nonUrlList & ", " & cell.Value
End If
End If
Next cell
' 如果有非链接内容,询问用户是否继续
If nonUrlCount > 0 Then
msgResult = MsgBox("发现 " & nonUrlCount & " 个非链接内容: " & nonUrlList & vbNewLine & vbNewLine & "是否继续打开有效链接?", vbYesNo + vbQuestion, "发现非链接内容")
If msgResult = vbNo Then
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
End If
End If
' 重置变量用于最终报告
nonUrlList = ""
nonUrlCount = 0
' 遍历范围内的每个单元格并打开有效链接
For Each cell In rng
url = cell.Value
If url <> "" And (Left(url, 7) = "http://" Or Left(url, 8) = "https://") Then
' 使用 FollowHyperlink 方法在默认浏览器中打开网址
On Error Resume Next ' 防止单个链接错误中断整个流程
ThisWorkbook.FollowHyperlink url, NewWindow:=True
If Err.Number <> 0 Then
' 记录无法打开的链接
If nonUrlList = "" Then
nonUrlList = cell.Value & " (打开失败)"
Else
nonUrlList = nonUrlList & vbNewLine & cell.Value & " (打开失败)"
End If
nonUrlCount = nonUrlCount + 1
Err.Clear
End If
On Error GoTo 0
' 短暂暂停,避免浏览器过载
Application.Wait (Now + TimeValue("0:00:01"))
ElseIf url <> "" Then
' 记录非链接内容
If nonUrlList = "" Then
nonUrlList = cell.Value & " (非链接)"
Else
nonUrlList = nonUrlList & vbNewLine & cell.Value & " (非链接)"
End If
nonUrlCount = nonUrlCount + 1
End If
Next cell
' 恢复屏幕更新和自动计算
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
' 显示未打开的内容报告
If nonUrlCount > 0 Then
MsgBox "以下内容未能打开:" & vbNewLine & vbNewLine & nonUrlList, vbInformation, "打开结果报告"
Else
MsgBox "所有有效链接已成功打开!", vbInformation, "操作完成"
End If
End Sub
二、BAT脚本(20250902)
在 Excel 里导出网址
- 存为
urls.txt
- 地址示例:
https://campus.xunlei.com/
https://careers.oppo.com/
https://careers.cvte.com/
BAT批处理文件
@echo off
setlocal enabledelayedexpansion
:: 设置存放网址的文件名(和 bat 放在同一个文件夹)
set "urlfile=urls.txt"
:: 检查文件是否存在
if not exist "%urlfile%" (
echo 找不到 %urlfile%
pause
exit /b
)
:: 循环逐行打开网址
for /f "usebackq delims=" %%a in ("%urlfile%") do (
start "" "%%a"
)
endlocal
Comments | NOTHING