2016-09-23 12 views
5

添付ファイルの添付ファイルサイズが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 

感謝。

答えて

6

範囲を選択する必要はありません。ユーザーによる1回のクリックミスは、範囲から離れてフォーカスを取ります。また、.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 
をユーザーに警告します
関連する問題