简体   繁体   English

像数组一样使用Excel单元格

[英]Use an Excel cell like an array

I have an Excel file and I have to count the number of "." 我有一个Excel文件,我必须计算“。”的数量。 for each cell. 对于每个细胞。 One cell has some characters (for example 'A01.10.10.10') Is it possible transfer into an array the content of a single cell? 一个单元格有一些字符(例如'A01.10.10.10')是否可以将单个单元格的内容转换为数组?

You can use Split() for this: 你可以使用Split()

myArray = Split(Range("A1"), ".")

That will "Split" Range("A1") contents by a period into an array. 这将把一个句点“分割” Range("A1")内容分成一个数组。


You could also just go straight to the return of the split() function to get the ubound since the return is an array (skipping the need to dim the array): 您也可以直接返回split()函数来获取ubound,因为返回是一个数组(跳过需要调dim数组):

UBound(Split(Range("A1"),".")) 

Which would spit out 3 given your input. 根据你的意见,哪个会吐3

Why do you need an array to do this? 你为什么需要一个阵列来做这个?

You can get your answer in an Excel formula by using 您可以使用Excel公式获得答案

=LEN(A1)-LEN(SUBSTITUTE(A1,".",""))

Or VBA 或者VBA

Sub CountDots()
    Dim str As String
    str = "A01.10.10.10"
    MsgBox Len(str) - Len(Replace(str, ".", vbNullString))
End Sub

No need for an array. 不需要阵列。 If you are going to do something else with the string and need it in an array though then use @JNevill answer 如果你打算用字符串做一些其他事情,然后需要在数组中使用@JNevill回答

You can found the "." 你可以找到“。” in a single cell using a formula: 在使用公式的单个单元格中:

=Len(targetcell)-Len(Substitute(targetcell,".",""))

Counts the number of cells, then counts the number of cells after the "." 计算单元格数,然后计算“。”之后的单元格数。 has been replaced with "" (nothing). 已被“”(没有)取代。

You wouldn't need an array for that. 你不需要一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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