excel - 在尝试使代码更快时键入不匹配
问题描述
我试图切换到 For 循环 (For i = 1 To UBound(arrSOPID,1) 以使代码更快;根据我收到的一些建议,在循环许多单元格之前。
然后我在这部分得到类型不匹配...Trim(arrSOPID(i, 1)
'Store cells in COL A that have values as a range
Dim arrSOPID As Variant: Set arrSOPID = .Range("A1", .Range("A1").End(xlDown))
Dim cel As Range
'Loop through each SOP audit file
For Each oFile In oFiles
'Strip audit date out of filename and trim off the file extension
Dim auditDate As Date: auditDate = CDate(DateSerial(Right(Left(Split(oFile.Name, "-")(3), 8), 4), _
Left(Left(Split(oFile.Name, "-")(3), 8), 2), _
Mid(Left(Split(oFile.Name, "-")(3), 8), 3, 2)))
Dim i As Long: i = 0
'Loop through all SOP IDs stored in COL A
For i = 1 To UBound(arrSOPID, 1)
'MsgBox (Trim(arrSOPID(i, 1)))
'See if SOP ID in COL A matches SOP ID in Audit file name
If Trim(RemoveLeadingZeroes(Split(oFile.Name, "-")(2))) = Trim(arrSOPID(i, 1)) Then
'Insert link to audit, change background color, etc of selected cell
With cel.Offset(0, 3 + Month(auditDate))
.Hyperlinks.Add Anchor:=cel.Offset(0, 3 + Month(auditDate)), Address:=oFile.Path, TextToDisplay:="X"
.Interior.Color = RGB(34, 139, 34)
.Font.Color = vbBlack
.Font.Bold = True
End With
End If
Next i
Next oFile
解决方案
您的数组是Variant
. 在String
使用Trim()
. 像下面这样的东西应该可以工作:
'Store cells in COL A that have values as a range
Dim arrSOPID As Variant: Set arrSOPID = .Range("A1", .Range("A1").End(xlDown))
Dim cel As Range
Dim myStr as String
'Loop through each SOP audit file
For Each oFile In oFiles
'Strip audit date out of filename and trim off the file extension
Dim auditDate As Date: auditDate = CDate(DateSerial(Right(Left(Split(oFile.Name, "-")(3), 8), 4), _
Left(Left(Split(oFile.Name, "-")(3), 8), 2), _
Mid(Left(Split(oFile.Name, "-")(3), 8), 3, 2)))
Dim i As Long: i = 0
'Loop through all SOP IDs stored in COL A
For i = 1 To UBound(arrSOPID, 1)
myStr = arrSOPID(i, 1)
'MsgBox (Trim(myStr))
'See if SOP ID in COL A matches SOP ID in Audit file name
If Trim(RemoveLeadingZeroes(Split(oFile.Name, "-")(2))) = Trim(myStr) Then
'Insert link to audit, change background color, etc of selected cell
With cel.Offset(0, 3 + Month(auditDate))
.Hyperlinks.Add Anchor:=cel.Offset(0, 3 + Month(auditDate)), Address:=oFile.Path, TextToDisplay:="X"
.Interior.Color = RGB(34, 139, 34)
.Font.Color = vbBlack
.Font.Bold = True
End With
End If
Next i
Next oFile
推荐阅读
- graphql - 记录 gql 标记的模板文字的输出?
- entity-framework - 使用 Include() 优化 EF Core 查询
- python - 不断更改 Tensorflow 中的相同变量
- amazon-web-services - 将数据从 AWS SQS 传输到 S3 的最佳方式是什么?
- php - php/mysql 响应式成像
- python - 使用 Python 线程的可执行文件的高 CPU 负载
- c# - 如何将 Windows.Forms.Checkbox(图像/绘画)覆盖为自定义复选框?
- oracle - Oracle 到 Redshift 查询迁移
- python-3.x - python for循环计算太低效/太长
- javascript - 在 Node 和 webpack 之间共享 babelified ECMAScript 模块