【问题标题】:How to change fixed numbers into ranges in VBA如何将固定数字更改为VBA中的范围
【发布时间】:2016-02-04 16:37:01
【问题描述】:

我有一个 VBA 代码,其中选择(固定)从 0 到 13 的数字,从这里将提供具有相应颜色的地图。但是...我想将其更改为每种颜色的范围。例如,我不希望将“1”与深蓝色耦合,将“2”与浅蓝色耦合等,但我想要一个范围。比如:0 - 50 --> 深蓝色,50 - 100 --> 浅蓝色等数字。

在下面的 VBA 代码中,您可以看到高于 13 (intStateValue) 的数字是由另一个协议提供的。但是代码中没有范围(For intState = 1 To rngStates.Rows.Count)。如何创建如上所述的范围(0 - 50、50 - 100、100 - 150 等)?

希望任何人都可以帮助我解决这个问题!提前致谢!!


Option Explicit

Sub Kleurgemeenten()
'
' Using the values from named range POSTCODEGEBIEDEN
' And the colours from named range KLEUREN
' re colour the map on sheet MainMap
'
Dim intState As Integer
Dim strStateName As String
Dim intStateValue As Integer
Dim intColourLookup As Integer
Dim rngStates As Range
Dim rngColours As Range

Set rngStates = Range(ThisWorkbook.Names("GEMEENTE").RefersTo)
Set rngColours = Range(ThisWorkbook.Names("KLEUREN").RefersTo)

    With Worksheets("MainMap")
    For intState = 1 To rngStates.Rows.Count
        strStateName = rngStates.Cells(intState, 1).Text
        intStateValue = rngStates.Cells(intState, 2).Value
        If intStateValue > 13 Then
            ' stripped
            With .Shapes(strStateName)
                intColourLookup = Application.WorksheetFunction.Match(CInt(Left(CStr(intStateValue), 1)), Range("KLEUREN"), True)
                .Fill.Patterned msoPatternWideUpwardDiagonal
                .Fill.ForeColor.RGB = rngColours.Cells(intColourLookup, 1).Offset(0, 1).Interior.Color
                intColourLookup = Application.WorksheetFunction.Match(CInt(Right(CStr(intStateValue), 1)), Range("KLEUREN"), True)
                .Fill.BackColor.RGB = rngColours.Cells(intColourLookup, 1).Offset(0, 1).Interior.Color
            End With
        Else
            ' single colour
            intColourLookup = Application.WorksheetFunction.Match(intStateValue, Range("KLEUREN"), True)
            With .Shapes(strStateName)
                .Fill.Solid
                .Fill.ForeColor.RGB = rngColours.Cells(intColourLookup, 1).Offset(0, 1).Interior.Color
            End With
        End If
    Next
End With

结束子

【问题讨论】:

    标签: excel vba colors range


    【解决方案1】:

    我不确定我是否正确理解了您的问题,但我认为这可能会对您有所帮助。

    您已经添加了创建我在 cmets 中谈到的表格的代码

    Option Explicit
    
    Sub Kleurgemeenten()
    '
    ' Using the values from named range POSTCODEGEBIEDEN
    ' And the colours from named range KLEUREN
    ' re colour the map on sheet MainMap
    '
    Dim intState As Integer
    Dim strStateName As String
    Dim intStateValue As Integer
    Dim intColourLookup As Integer
    Dim rngStates As Range
    Dim rngColours As Range
    
    Set rngStates = Range(ThisWorkbook.Names("GEMEENTE").RefersTo)
    Set rngColours = Range(ThisWorkbook.Names("KLEUREN").RefersTo)
    
    Dim colorTable(1 To 2, 1 To 13) As Long
    ' creaing the table
    
    With Worksheets("MainMap")
        For intState = 1 To rngStates.Rows.Count
            strStateName = rngStates.Cells(intState, 1).Text
            intStateValue = rngStates.Cells(intState, 2).Value
    
            Dim colorNumber As Long, i As Long
            colorNumber = 14
    
            For i = 1 To 13
                If intStateValue >= colorTable(1, i) And intStateValue < colorTable(2, i) Then colorNumber = i
            Next i
    
            If colorNumber > 13 Then
                ' stripped
                With .Shapes(strStateName)
                    intColourLookup = Application.WorksheetFunction.Match(CInt(Left(CStr(colorNumber), 1)), Range("KLEUREN"), True)
                    .Fill.Patterned msoPatternWideUpwardDiagonal
                    .Fill.ForeColor.RGB = rngColours.Cells(intColourLookup, 1).Offset(0, 1).Interior.Color
                    intColourLookup = Application.WorksheetFunction.Match(CInt(Right(CStr(colorNumber), 1)), Range("KLEUREN"), True)
                    .Fill.BackColor.RGB = rngColours.Cells(intColourLookup, 1).Offset(0, 1).Interior.Color
                End With
            Else
                ' single colour
                intColourLookup = Application.WorksheetFunction.Match(colorNumber, Range("KLEUREN"), True)
                With .Shapes(strStateName)
                    .Fill.Solid
                    .Fill.ForeColor.RGB = rngColours.Cells(intColourLookup, 1).Offset(0, 1).Interior.Color
                End With
            End If
        Next
    End With
    End Sub
    

    【讨论】:

    • 我假设您想要单元格 E2="0-1000"、E3="1000-2000" 等。我将创建一个 13x2 矩阵,其中第一列是提取的较低列边界,第二个是提取的上限列。然后对于每个 intState,您循环遍历表的行并检查 intStateValue 是否在边界之间,如果是,则相应地分配 colorNumber。我会更新我之前回答的代码。
    • 是的,没错。是的,这将是完美的解决方案!提前感谢您,也感谢您的快速回复!
    • 现在运行代码,单元格 E2 中的第一种颜色(灰色)为 0(或 1),单元格 E3 中为 1000,单元格 E4 中为 2000 会生成灰色地图,尽管值在 B 列中。这仍然与限制有关(当一个值超过 13 时,然后得到条带化的“版本”?--> 因为这部分可以省略,当值更高时不需要这个超过 13)?
    • F 列中的颜色分配给 E 列中提供的数字。只需替换 F 中的颜色将改变结果。 rngStates 的总数是通过使用 A 列中的市政总集创建的: (Set rngStates = Range(ThisWorkbook.Names("GEMEENTE").RefersTo) 和颜色 (var rngColours ) 由 F 列中的颜色设置(另请参阅“GEMEENTE”和“KLEUREN”的名称管理器):设置 rngColours = Range(ThisWorkbook.Names("KLEUREN").RefersTo)。
    • 我了解潜艇在做什么。我想找出为什么你只得到灰色作为输出的原因。当我使用此代码创建 colorTable 矩阵时,一切都很好` Dim j As Long` ` For j = 1 To 13` ` colorTable(1, j) = (j - 1) * 1000` ` colorTable(2, j ) = j * 1000` `下一个 j`
    【解决方案2】:

    事实上,它甚至更简单......这是最终答案! :-) -->


    Option Explicit
    
    Sub Kleurgemeenten()
    '
    ' Using the values from named range POSTCODEGEBIEDEN
    ' And the colours from named range KLEUREN
    ' re colour the map on sheet MainMap
    '
        Dim intState As Integer
        Dim strStateName As String
        Dim intStateValue As Long
        Dim intColourLookup As Integer
        Dim rngStates As Range
        Dim rngColours As Range
        Dim WS_Control As Worksheet
        Dim i As Long
    
        Set WS_Control = Worksheets("Control")
    
        Set rngStates = Range(ThisWorkbook.Names("GEMEENTE").RefersTo)
        Set rngColours = Range(ThisWorkbook.Names("KLEUREN").RefersTo)
    
        With Worksheets("MainMap")
            For intState = 1 To rngStates.Rows.Count
                strStateName = rngStates.Cells(intState, 1).Text
                intStateValue = rngStates.Cells(intState, 2).Value
                If intStateValue >= WS_Control.Range("E14").Value Then
                    'if value is higher than last number of the defined range
                    With .Shapes(strStateName)
                        .Fill.Solid
                        .Fill.ForeColor.RGB = WS_Control.Cells(14, 6).Interior.Color
                    End With
                Else
                    'if value is inside the defined range
                    'Loop through value ranges
                    For i = 3 To 14
                        'if relavent range found
                        If intStateValue < WS_Control.Range("E" & i).Value Then
                            intColourLookup = i - 2  'Had to reduce 2 because name range "rngColours" is defined from "E2"
                            Exit For
                        End If
                    Next i
    
                    With .Shapes(strStateName)
                        .Fill.Solid
                        .Fill.ForeColor.RGB = rngColours.Cells(intColourLookup, 1).Offset(0, 1).Interior.Color
                    End With
                End If
            Next
        End With
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 2019-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多