简体   繁体   English

Excel 2010-宏以检​​测单元格的特定内部颜色并产生结果

[英]Excel 2010 - Macro to Detect Specific Interior Color of a Cell and Produce a Result

I created an Excel spreadsheet with a command button that calls a macro. 我用一个调用宏的命令按钮创建了一个Excel电子表格。 The macro produces an new email through Microsoft Outlook that provides a list of data, specifically data from cells ranged A5 through A500 that have an interior color of RGB(255, 255, 204). 宏通过Microsoft Outlook生成一封新电子邮件,其中提供了数据列表,特别是内部颜色为RGB(255、255、204)的A5至A500单元格中的数据。

However, I'm not getting accurate results. 但是,我没有得到准确的结果。 The macro is not exclusively capturing the data from cells of the specified interior color and is giving me data from cells of alternate colors as well. 宏不是专门从指定内部颜色的单元格捕获数据,而是从备用颜色的单元格中也提供数据。 This may be a logic error on my part. 这可能是我的逻辑错误。

Here is the code I am currently working with: 这是我目前正在使用的代码:

Private Sub btnReport_Click()
'Create Email Message
Dim r As Range
Dim m As String
Set r = ActiveSheet.Range("A5:A500")
m = "Hello,<br><br>Here is some information:<br><br>" & _
    "<table border=""1"" style=""width:98.7%"" align=""left"" ><tr style=""vertical-align:top;""><td style=""width:9.3%"" nowrap>" & _
    "<b>Column 1</b></td><td style=""width:10%"" nowrap><b>Column 2</b></td><td style=""width:10%"" nowrap><b>Column 3</b></td></tr>"

For Each r In r.Cells
    If r.Interior.Color = RGB(255, 255, 204) Then
        m = m & "<tr><td>" & ActiveSheet.Range("D" & r) & "</td><td>" & ActiveSheet.Range("L" & r) & "</td><td>" & _
            ActiveSheet.Range("M" & r) & "</td></tr>"
    End If
Next

'Open Email
Dim olApp As Object
Dim olMail As Object
Dim name As String

Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(olMailItem)
With olMail
    .To = "Dee Wolf"
    .Subject = "This is a subject line"  
    .HTMLBody = m
    .Display
End With
Set olMail = Nothing
Set olApp = Nothing

End Sub

Why are you using the r variable twice for 2 purposes, and why are you building a range using ("D" & r). 为什么要两次出于两个目的使用r变量,为什么要使用(“ D”&r)建立范围。 r is an object (specifically a cell). r是一个对象(特别是一个单元格)。 I am assuming you want to take data from D and M, but on the same row? 我假设您想从D和M中获取数据,但是在同一行上?

If so this should read as below. 如果是这样,则应如下所示。

Dim aCell as Range 昏暗的单元格作为范围

For Each aCell In r.Cells
    If r.Interior.Color = RGB(255, 255, 204) Then
        m = m & "<tr><td>" & ActiveSheet.Range("D" & aCell.Row) & "</td><td>" & ActiveSheet.Range("L" & aCell.Row) & "</td><td>" & _
            ActiveSheet.Range("M" & aCell.Row) & "</td></tr>"
    End If
Next

You may of course be taking a shortcut, and actually are using r.Value when you build a range using Range("D" & r). 您当然可以走捷径,并且在使用Range(“ D”&r)建立范围时实际上正在使用r.Value。 ie this is shot for Range("D" & r.Value)...so if "r" references a cell with a number in it like 599, then you want to reference cell "D599" which is what your code does? 也就是说,这是为Range(“ D”&r.Value)拍摄的...因此,如果“ r”引用其中带有数字的单元格(例如599),那么您要引用单元格“ D599”,这是您的代码的作用?

这对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM