从单元格中读取范围值

时间:2013-07-05 21:42:35

标签: vba excel-vba excel

在以下代码中

Sub SetColorScheme(cht As Chart, i As Long)

    Dim y_off As Long, rngColors As Range
    Dim x As Long

    y_off = i Mod 10
    'this is the range of cells which has the colors you want to apply
    Set rngColors = ThisWorkbook.Sheets("colors").Range("A1:C1").Offset(y_off, 0)

    With cht.SeriesCollection(1)
        'loop though the points and apply the 
        'corresponding  fill color from the cell
        For x = 1 To .Points.Count
            .Points(x).Format.Fill.ForeColor.RGB = _
                             rngColors.Cells(x).Interior.Color
        Next x
    End With

End Sub

读取数据的范围是代码中规定的。是否有可能从工作表中的asheet中读取它?这样一个人就可以输入A1:C1,它会将它放在代码中的那一刻吗?

2 个答案:

答案 0 :(得分:1)

我不确定你想如何处理用户的输入,但当然范围可以是传入的变量。我把它作为一个字符串在下面,但优雅将是范围对象。对不起,如果这太简单了,我不确定你的问题。

Sub SetColorScheme(UserRange As String, cht As Chart, i As Long)
    ...

    'this is the range of cells which has the colors you want to apply
    Set rngColors = ThisWorkbook.Sheets("colors").Range(UserRange).Offset(y_off, 0)

    ...    
End Sub

答案 1 :(得分:0)

如果用户在单元格D1中输入“A1:C1”,则可以使用此范围:

Set rngColors = ThisWorkbook.Sheets("colors").Range(Range("D1").Value).Offset(y_off, 0)
' but you should refer to the w/sheet as well

Set rngColors = ThisWorkbook.Sheets("colors") _ 
    .Range(ThisWorkbook.Sheets("colors").Range("D1").Value).Offset(y_off, 0)

Range("D1").Value获取文本“A1:C1”,然后用于标识此范围。

相关问题