excel - Type mismatch error in vba while search and update
问题描述
The code below throws a Runtime Error: 13 Type mismatch
. I have a list of thousands of URLs in the Sheet3, column A
where I run my vba script. It finds each of the URLs in Sites, column B
of 300 workbooks which is in a folder. If found, the column E
of the same row is updated with Yes
.
Sub ReplaceInFolder()
Dim strPath As String
Dim strFile As String
Dim wbk As Workbook
Dim wsh As Worksheet
Dim strReplace As String
Dim i As Long
Dim FoundCell As Variant
Dim FoundNo As String
strReplace = "Yes"
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then
strPath = .SelectedItems(1)
Else
MsgBox "No folder selected!", vbExclamation
Exit Sub
End If
End With
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
Application.ScreenUpdating = False
strFile = Dir(strPath & "*.xls*")
Do While strFile <> ""
Set wbk = Workbooks.Open(Filename:=strPath & strFile, AddToMRU:=False)
For Each wsh In wbk.Worksheets
If wsh.Name = "Sites" Then
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
Set FoundCell = wsh.Range("B:B").Find(What:=Cells(i, "A").Value, LookAt:=xlWhole, MatchCase:=False)
If Not FoundCell Is Nothing Then
FoundNo = FoundCell.Address
Debug.Print FoundCell.Address
Do Until FoundCell Is Nothing
wsh.Cells(FoundCell.Row, 5).Value = strReplace
Set FoundCell = wsh.Cells.FindNext(after:=FoundCell)
If FoundCell.Address = FoundNo Then Exit Do
Loop
End If
Next i
End If
Next wsh
wbk.Close SaveChanges:=True
strFile = Dir
Loop
Application.ScreenUpdating = True
End Sub
解决方案
推荐阅读
- reactjs - React Native - 如何为文本对象的单个元素设置样式
- android - 当我在活动之间切换时,我的应用程序正在闪烁并在后台显示以前的活动
- python - 当收到所有回复时,非阻塞 zmq 发送/接收和处理
- python - 如何创建导入其他导入的python脚本
- php - Session 类构造函数中的 session_start() (是否在每个 url 上启动多个会话)
- javascript - 上一个和下一个按钮触发两次
- mysql - 如何在 mysql 中将逗号分隔的字符串与逗号分隔的值匹配?
- rest - 上游提供程序失败时在 RESTful api 中返回 HTTP 502 错误代码
- .net - 正则表达式选择前缀后的所有数字,最小和最大位数并跳过非数字字符(最多 4 个)
- android - 最新的 firebase sdk 将继续导致应用程序崩溃