从Range.Areas的每个单元格中获取价值

时间:2015-11-13 12:30:03

标签: vba excel-vba excel

我有这个代码,我得到here

setConcatenatedData(String name) {   
     this.ConcatenatedData = name;
}

此代码的作用是创建一个带有其他Sub QuickMap() Dim FormulaCells Dim TextCells Dim NumberCells Dim Area If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub ' Create object variables for cell subsets On Error Resume Next Set FormulaCells = Range("A1").SpecialCells _ (xlFormulas, xlNumbers + xlTextValues + xlLogical) Set TextCells = Range("A1").SpecialCells(xlConstants, xlTextValues) Set NumberCells = Range("A1").SpecialCells(xlConstants, xlNumbers) On Error GoTo 0 ' Add a new sheet and format it Sheets.Add With Cells .ColumnWidth = 2 .Font.Size = 8 .HorizontalAlignment = xlCenter End With Application.ScreenUpdating = False ' Do the formula cells If Not IsEmpty(FormulaCells) Then For Each Area In FormulaCells.Areas With ActiveSheet.Range(Area.Address) .value = "F" .Interior.ColorIndex = 3 End With Next Area End If ' Do the text cells If Not IsEmpty(TextCells) Then For Each Area In TextCells.Areas With ActiveSheet.Range(Area.Address) .value = "T" .Interior.ColorIndex = 4 End With Next Area End If ' Do the numeric cells If Not IsEmpty(NumberCells) Then For Each Area In NumberCells.Areas With ActiveSheet.Range(Area.Address) .value = "N" .Interior.ColorIndex = 6 End With End If Next Area End If End Sub 地图的新worksheet,例如,它将背景颜色为黄色的N放置在另一个工作表上的数字或常量。

我想在地图上的单元格上将背景颜色设置为蓝色,其他工作表上的值是数字且大于130。

它看起来有一个非常简单的解决方案,但我尝试使用它,就像我使用worksheet但我没有得到任何满意的结果。

所以,我的问题是,我怎样才能获得每个单元格的值来使用条件语句?提前谢谢。

1 个答案:

答案 0 :(得分:1)

你可以在.area上循环项目,如果单个项目对应你的文本背景单元格将是蓝色,否则为黄色

Sub QuickMap()
        Dim FormulaCells
        Dim TextCells
        Dim NumberCells
        Dim Area

        If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub

    '   Create object variables for cell subsets
        On Error Resume Next
        Set FormulaCells = Range("A1").SpecialCells _
          (xlFormulas, xlNumbers + xlTextValues + xlLogical)
        Set TextCells = Range("A1").SpecialCells(xlConstants, xlTextValues)
        Set NumberCells = Range("A1").SpecialCells(xlConstants, xlNumbers)
        On Error GoTo 0

    '   Add a new sheet and format it
        Sheets.Add
        With Cells
            .ColumnWidth = 2
            .Font.Size = 8
            .HorizontalAlignment = xlCenter
        End With

        Application.ScreenUpdating = False

    '   Do the formula cells
        If Not IsEmpty(FormulaCells) Then
            For Each Area In FormulaCells.Areas
                With ActiveSheet.Range(Area.Address)
                    .Value = "F"
                    .Interior.ColorIndex = 3
                End With
            Next Area
        End If

    '   Do the text cells
        If Not IsEmpty(TextCells) Then
            For Each Area In TextCells.Areas
                With ActiveSheet.Range(Area.Address)
                    .Value = "T"
                    .Interior.ColorIndex = 4
                End With
            Next Area
        End If

    '   Do the numeric cells
        If Not IsEmpty(NumberCells) Then
            For Each Area In NumberCells.Areas
                For Each Item In Area
                    If Item > 130 Then
                        ActiveSheet.Range(Item.Address).Value = "N"
                        ActiveSheet.Range(Item.Address).Interior.ColorIndex = 5
                    Else
                        ActiveSheet.Range(Item.Address).Value = "N"
                        ActiveSheet.Range(Item.Address).Interior.ColorIndex = 6
                    End If
                Next Item
            Next Area
        End If
    End Sub