Len(Columns(lastColumn +“:”+ c))
我一直在这行代码已经搞乱一段时间了,我似乎还不能让它运行。
我有问题的部分代码是这样的:
TempW = Len(Columns(lastColumn + ":" + c))
我试图根据列获取位于特定单元格内的文本的长度,并将所有长度连接成一个string。
例如,7,8,2,13,40 。
我想输出是这样的,因为我想用它作为一个数组像这样CWSize = Array(CWStore)
。
列数不是恒定的。
这是我的代码:
Sub testColumnLen() Dim CWArray As Variant, CWSize As String, TempW As Long, c As Integer, lastColumn As Long Dim CWStore As String lastColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column c = 1 TempW = Len(Columns(lastColumn + ":" + c)) c = 2 CWStore = TempW Do While c < lastColumn TempW = Len(Columns(lastColumn + ":" + c)) CWStore = CWStore + "," + TempW c = c + 1 Loop CWSize = Array(CWStore) CWArray = CWSize Debug.Print CWArray End Sub
更新你的代码:
Sub testColumnLen() Dim CWSize As Variant, TempW As Long, c As Integer, lastColumn As Long Dim CWStore As String, i As Long lastColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column c = 1 Do While c <= lastColumn TempW = Len(Cells(1, c)) If CWStore = "" Then CWStore = TempW Else CWStore = CWStore & "," & TempW End If c = c + 1 Loop Debug.Print CWStore '---> this will give you comma separated values CWSize = Split(CWStore, ",") For i = LBound(CWSize) To UBound(CWSize) Debug.Print CWSize(i) '---> this will give you individual lengths of cell Next i End Sub
尝试这个
Option Explicit Sub testColumnLen() Dim CWArray As Variant Dim CWStore As String Dim cell As Range With ActiveSheet For Each cell In .Range("A1", .Cells(1, .Columns.Count).End(xlToLeft)) CWStore = CWStore & Len(cell) & " " Next cell End With CWArray = Split(Left(CWStore, Len(CWStore) - 1)) End Sub
或者你可能需要将最后的数组设置为
CWArray = Array(Left(CWStore, Len(CWStore) - 1))