gpt4 book ai didi

vba - 选择多个单元格

转载 作者:行者123 更新时间:2023-12-02 10:24:25 24 4
gpt4 key购买 nike

我有这段代码,用于检查附件的大小是否大于 10MB。现在,如果附件大于 10MB,它会在 msgbox 上显示文件名,然后我想选择或突出显示该附件大于 10MB 的单元格,但不知道该怎么做。

这是我尝试过的:

Function checkAttSize()

Application.ScreenUpdating = False
Dim attach As Object
Dim attSize() As String
Dim loc() As String
Dim num As Long
Dim rng As Range

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

Set main = ThisWorkbook.Sheets("Main")
lRow = Cells(Rows.count, 15).End(xlUp).Row
efCount = 0
num = 0
With objMail
If lRow > 22 Then
On Error GoTo errHandler
For i = 23 To lRow
'attach.Add main.Range("O" & i).value
'totalSize = totalSize +
If (FileLen(main.Cells(i, "O").value) / 1000000) > 10 Then
ReDim Preserve attSize(efCount)
ReDim Preserve loc(num)
'store file names
attSize(efCount) = Dir(main.Range("O" & i))
'store cell address
loc(num) = i
efCount = efCount + 1
num = num + 1
found = True
End If
Next i
End If
End With

If found = True Then
MsgBox "Following File(s) Exceeds 10MB Attachment Size Limit:" & vbCrLf & vbCrLf & Join(attSize, vbCrLf) _
& vbCrLf & vbCrLf & "Please try removing the file(s) and try again.", vbCritical, "File Size Exceed"
'trying to select the cell addresses
For i = 1 To num
rng = rng + main.Range("O" & loc(i)).Select ' Ive also tried &
Next i
checkAttSize = True
Exit Function
End If
Exit Function
errHandler:
MsgBox "Unexpected Error Occured.", vbCritical, "Error"
checkAttSize = True
End Function

感谢您的帮助。

最佳答案

无需选择范围。用户单击失误就会使焦点偏离范围。另外,鲁莽地使用 .Select 可能会导致运行时错误。给它们涂上颜色。

此行之后

If (FileLen(main.Cells(i, "O").value) / 1000000) > 10 Then

添加此行

main.Cells(i, "O").Interior.ColorIndex = 3

单元格现在将被着色为红色。

最后,用消息提醒用户

If found = True Then
MsgBox "File(s) Exceeding 10MB Attachment Size Limit has been colored in red:"
End If

关于vba - 选择多个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39656221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com