Qi Xuan
Qi Xuan

Reputation: 43

VBA if the value same as specific Dim's name, then get the Dim of value

how to link CSVTypeColumn = CSVType01 = "AL"? Its has anyway to let if CSVTypeColumn.value = CSVType01 Then CSVTypeColumn.value = "AL"?

'''''Module1
Public CSVType01 As String
       CSVType01 = "AL"

'''''Module2
For CSVTypeColumnCount = 1 To 6
Dim CSVTypeColumn As String                               
    CSVTypeColumn = "CSVType0" & CSVTypeColumnCount
Dim FindCSVType As Range
    Set FindCSVType = .Range(CSVTypeColumn & 1).Column '<-- Wrong here!

Upvotes: 1

Views: 53

Answers (2)

Pᴇʜ
Pᴇʜ

Reputation: 57743

That does not work. When ever you have the feeling you need no use numbers in your variable names then you are doing it wrong!

Use arrays instead.

Public CSVType(1 To 6) As String
CSVType(1) = "AL"

CSVTypeColumn = CSVType(CSVTypeColumnCount)

Upvotes: 1

Rory
Rory

Reputation: 34075

Use an array:

Public CSVType(1 To 6) As String
       CSVType(1) = "AL"
' etc
For CSVTypeColumnCount = 1 To 6
Dim CSVTypeColumn As String                               
    CSVTypeColumn = CSVType(CSVTypeColumnCount)

Upvotes: 2

Related Questions