How to use the name of a cell in the Excel worksheet in VBA, so that it does not indicate row and column?

3

How to use the name of an Excel spreadsheet cell in VBA, not to indicate row and column?

For example, I gave the name "CustomerName" to cell B20, when I use this name in VBA to put in the label (label1.caption = CustomerName), it accepts and does not give error. However, it does not take the value of the cell.

As I'm inserting columns and rows, the cell changes, but VBA obviously does not hit the rows and columns of the range command.

How do I use the cell name directly?

    
asked by anonymous 23.11.2015 / 22:19

1 answer

3

I believe this is what you want: You get a Range because the name is not a primary key. Then he spins all his returns.

Sub Test()

    Dim r As Range
    Dim cell As Range

    Set r = Range("Something")

    If r.Cells.Count > 1 Then
        For Each cell In r.Cells
            MsgBox cell.Value
        Next cell
    Else
        MsgBox r.Value
    End If
End Sub

UPDATING

Do not do this in VB or C #, be it Python, for example, you could.

I suggest creating a list, something like this:

Dim dados As New Dictionary(Of String, String)()
dados.Add("nome", "30/nome")
dados.Add("endereco", "30/endereco")
dados.Add("data_cnsc", "8/dt_nasc")
dados.Add("municipio", "20/municipio")
MessageBox.Show(dados("municipio"))'acessamos os valores por índices
    
23.11.2015 / 22:29