所选区域连接的Excelmacros

我希望有人对如何处理下面的Excelmacros需求有一些了解。

开始条件:列A中的文本值的可变数量

build议的解决scheme:我希望能够在列A中select可变数量的连续单元格,然后将文本连接在一起,用逗号分隔,并与列B中最靠上的单元格相邻的单列中。

例如:在表上selectA2-A4。 在运行macros之后, B2 (直接相邻于select的顶部)的内容将以“A2,A3,A4”的forms包含文本。

选定A5-A10:运行macros后, B5 (直接与select的顶部相邻)的内容将以“A5,A6,A7,A8,A9,A10”的forms包含文本。

什么是杀我是如何利用多select的变化和外加,我不清楚如何处理在Excelmacros循环。 我有一个CS学位,但是我最终在基础设施方面工作,所以我有点生疏。 有人可以帮助,这将每天节省我的时间。 感谢任何回应。

下面的代码就是你所寻求的。 我还没有添加太多的评论,因为我不确定什么级别的评论是适当的。 例如,如果您的CS学位允许您猜测,我不想解释每个陈述的目的。 我也怀疑你的问题比显而易见的更多。 例如,我应该使这个函数的工作表和行号作为parameter passing。 请回来的问题,我会根据需要改善我的答案。

Option Explicit Sub JoinCells() Dim ColFirst As Long Dim ColLast As Long Dim JoinedValue As String Dim RowCrnt As Long Dim RowFirst As Long Dim RowLast As Long RowFirst = Selection.Row ' First row of selection ' Selection.Rows.Count returns the number of rows in the selection. ' Warning! You can fool this code by making multiple selections. RowLast = RowFirst + Selection.Rows.Count - 1 ColFirst = Selection.Column ColLast = ColFirst + Selection.Columns.Count - 1 If ColFirst <> 1 Or ColLast <> 1 Then Call MsgBox("Please select a range within column ""A""", vbOKOnly) Exit Sub End If With Worksheets("xxxxxxx") ' Worksheet of your choice. JoinedValue = .Cells(RowFirst, "A").Value For RowCrnt = RowFirst + 1 To RowLast JoinedValue = JoinedValue & "," & .Cells(RowCrnt, "A").Value Next .Cells(RowFirst, "B").Value = JoinedValue End With End Sub