将单个单元格中的多个值分配给多个变量

时间:2018-03-23 02:40:35

标签: excel-vba vba excel

我对Excel-Vb比较陌生。我基本上想从单个单元格中提取多个值并将其分配给多个变量。例如,

excel中的单个单元格包含格式(0.1:0.2:10)的值。我需要将单元格中的这些值分配给三个不同的变量。我的代码看起来应该是这样的,

[(5,6),(6,10),(10,12)]

理想情况下,o / p应该是单元格中的值0.1,0.2,10应分别分配给变量1,变量2,变量3。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

根据您对问题的理解,您可以编写如下代码 你需要提供单元格作为范围和位置值,如if(A1 =“0.1:0.2:10”)

=DeviceInfo(A1,0)'output is 0.1
=DeviceInfo(A1,1)'output is 0.2
=DeviceInfo(A1,2)'output is 10

'''
Public Function DeviceInfo(rng As Range, val As Integer)
'This function split string "0.1:0.2:10" on the basis of char ":"
'required input rng as informat of "0.1:0.2:10" and val as position of string after split

Dim LArray() As String 'Dim for output array

LArray = Split(rng.Value, ":") 'split on basis of ":" char

If Application.WorksheetFunction.IsNumber(val) = True Then

DeviceInfo = LArray(val)

Else
DeviceInfo = "Not Number"

End If

End Function