Excel VBA:组织selection.addressstring

下面的子程序正是我想要它做的,但只有当用户从左到右select列。

基本上,它会在所选列的每一列的左侧插入一列,并应用指定的格式。

我的问题是,是否有一个内置的方法,我可以用来组织select的地址,以便列从左到右的顺序?

Sub InsertColumns() Dim Columns() As String Dim x As Integer ReDim Columns(UBound(Split(Selection.Address, ","))) 'Organize the string before splitting it into an array? Columns = Split(Selection.Address, ",") 'Maybe a function to sort the array? For x = 0 To UBound(Columns) Range(Columns(x)).Offset(, x).Select 'Used for debugging Range(Columns(x)).Offset(, x).Insert With Range(Columns(x)).Offset(, x) .ColumnWidth = 1 .NumberFormat = "@" .HorizontalAlignment = xlLeft .Borders(xlDiagonalDown).LineStyle = xlNone .Borders(xlDiagonalUp).LineStyle = xlNone .Borders(xlEdgeLeft).LineStyle = xlNone .Borders(xlEdgeTop).LineStyle = xlNone .Borders(xlEdgeBottom).LineStyle = xlNone .Borders(xlEdgeRight).LineStyle = xlNone .Borders(xlInsideVertical).LineStyle = xlNone .Borders(xlInsideHorizontal).LineStyle = xlNone End With Next End Sub 

笔记:

select对象只计为1列,我还没有想出一种方法来循环每一列,而不使用地址。 如果这是可能的另一种方式,我愿意提出build议。

我利用了优秀的内容推送行为,并将select的格式应用到“新”列。 我不知道有一种方法来获取最近插入的列。 例如,像下面的工作?

 Dim NewCol as Dolumn Set NewCol = Range(columns(x).Offset(, x).Insert 

我使用math的力量确保列正确插入。 当我们添加列时,我们需要抵消后面的列,以确保我们移动正确的列数。 这就是为什么使用x来抓取起始列并用于按所需的数量抵消select的原因。

我可以完全编写代码来parsingstring或函数来sorting数组。 我正在寻找更多的内置方法来做到这一点。

再次,子工程完全按照预期。 我真的只需要find一种方法来补偿不遵循方向的用户。 任何想法赞赏!

编辑1:地址string将包含一个地址,如:

 $H:$H,$I:$I,$J:$J,$K:$K,$L:$L,$M:$M 

如果您没有从左到右select,则地址get按列的select顺序填充。 所以如果我按顺序select列D,F,E,地址是

 $D:$D,$F:$F,$E:$E 

而不是D,E,F(从左到右)这是我想要的字母顺序。

EDIT2:

我得到一个select是一个范围对象。 我想强调的是如下:

 Sub adsfd() Dim a As Range Set a = Selection Debug.Print a.Columns.Count End Sub 

在这里,输出文本是1,因为不能看到select中的各个列。

地区将工作,但是:

 Sub adsfd() Dim a As Range Set a = Selection Debug.Print a.areas.count End Sub 

这显示了正确的列数。 这里的问题是,他们仍然没有秩序

我也想强调 – 我可以使它工作。 我的问题是现在“我该怎么做”这是“是否有一个内置的方式来访问select从左到右的列”。

 dim a as range for each a in selection 'This cycles through independent cells. Which is not what I want next a 

迄今为止最好的答案仍然需要经过select中的每个细胞。 我的方法没有。

如果这是用户select的方式,这是从右到左的,但它似乎仍然达到了既定的目标 – 在每列的左侧插入一列,并对其进行格式化。

 Sub inscol() Dim rCol As Range Dim rNew As Range For Each rCol In Selection.Columns rCol.Insert With rCol.Offset(, -1) .ColumnWidth = 1 .NumberFormat = "@" .HorizontalAlignment = xlLeft .Borders(xlDiagonalDown).LineStyle = xlNone .Borders(xlDiagonalUp).LineStyle = xlNone .Borders(xlEdgeLeft).LineStyle = xlNone .Borders(xlEdgeTop).LineStyle = xlNone .Borders(xlEdgeBottom).LineStyle = xlNone .Borders(xlEdgeRight).LineStyle = xlNone .Borders(xlInsideVertical).LineStyle = xlNone .Borders(xlInsideHorizontal).LineStyle = xlNone End With Next rCol End Sub 

你可以使用一个函数(例如下面)find最右边的select列,然后从该列向后工作:

 Sub Tester() Dim MaxCol As Long, c As Long Dim rng As Range Set rng = Selection.EntireColumn MaxCol = LastCol(rng) For c = MaxCol To 1 Step -1 If Not Application.Intersect(Selection.Parent.Columns(c), rng) _ Is Nothing Then 'insert your new column End If Next c End Sub Function LastCol(rng As Range) As Long Dim a As Range, c As Range LastCol = 0 For Each a In rng.Areas For Each c In a.Columns If c.Column > LastCol Then LastCol = c.Column Next c Next a End Function