简体   繁体   中英

Traverse multiple cells not in a range stored in a name using VBA in Excel

I have a name that contains this, for example:
=(B1;C3;E4)

It is stored with the name, "Cells1".

I want to take those cells using VBA and work with them, and change their cell value. I have the following code:

Sub Vaciar_nombres()
    Dim R1 As 

    REM Following line throws a 1004 error.
    Set R1 = test_sheet.Names("Cells1").RefersToRange

    Debug.Print (VarType(R1))
End Sub

I tried using a Range type variable. It throws a 1004 error.

Also, I don't know if I have to write those cells in that name like:
=(B1;C4;C5) OR =B2;C4;C5

Don't take into account the semicolon notation, in my language, it is written like that when I create a name and select different cells using the Ctrl key.

How about this:

Option Explicit

Sub Test()

  Dim i As Integer
  Dim x As Range
  i = 1

  For Each x In Range("Cells1")
     x.Value = i
     i = i + 2
  Next x
  
End Sub

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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