将范围名称放入变量

时间:2015-06-10 22:22:15

标签: excel vba

我成功地将数字单元转换为文本单元格作为休闲:

dim celdactiva as string
dim mcard as objectlist
dim rango as range

Set mcard = ActiveWorkbook.ActiveSheet.ListObjects("mcard")

with mcard  **(this is an excel objectlist that contains cell(2,4))**

celdactiva=.Cells(2, 4).Address(RowAbsolute:=False, ColumnAbsolute:=False)

end with

现在我想将celdactiva(现在只是一个文本)放入变量中作为休闲:

1。 设置rango =范围(celdactiva)

但rango获取“celdactiva”的值而不是范围的名称。在这种情况下,“D2”属于表MCARD。

  1. 稍后我想将一个粗体“X”粘贴到rango中,但是在celdactiva的值旁边,现在名为rango。需要知道vba代码中的select,copy和paste语句。
  2. 有些人知道这两个部分(1& 2)?

1 个答案:

答案 0 :(得分:0)

试试这个,这应该做(1)和(2):

Sub SO_30768321()
    Dim celdactiva As String
    Dim mcard As ListObject
    Dim rango As Range

    Set mcard = ActiveWorkbook.ActiveSheet.ListObjects("mcard")
    Debug.Print "mcard Address: " & mcard.Range.Address
    With mcard ' **(this is an excel objectlist that contains cell(2,4))**
        celdactiva = .Range.Cells(2, 4).Address(RowAbsolute:=False, ColumnAbsolute:=False)
        Debug.Print "celdactiva: " & celdactiva
        '===[ You could have set rango here ]===
        Set rango = .Range.Cells(2, 4)
        Debug.Print "rango address: " & rango.Address
        'With rango.Offset(0, 1)
        '   .Font.Bold = True
        '   .Value = "X"
        'End With
        Set rango = Nothing
    End With

    Set rango = Range(celdactiva)
    With rango.Offset(0, 1)
        .Font.Bold = True
        .Value = "X"
    End With
End Sub