macros使用与工作簿中不同数量的行连接

如果我无法准确解释我的问题,我是新来这个论坛,所以道歉。

我在列B,行1-1200(估计)在一个工作簿中的数据,我想连接在同一工作簿的C列中的数据。 我知道如何做到这一点,并创build一个macros完成这个任务。

问题出现在当我有一个工作簿有列B,行1-2000(估计)的数据。 当我使用我的macros,它停在行1200。

有没有办法为连接函数select列C,并在列B中停止数据时停止?

我使用的每个工作簿都需要相同的连接函数,但是,每个工作簿中的行都不相同。

Sub Macro1() ' ' Macro1 Macro ' ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-1], "" "", ""-0000"")" Range("B2").Select Selection.AutoFill Destination:=Range("B2:B12") Range("B2:B12").Select End Sub 

这是你正在尝试? 我已经评论了代码,所以你不会理解它的问题。 如果你这样做,然后回发:)

 Sub Sample() Dim ws As Worksheet Dim rng As Range Dim col As Long, lRow As Long Set ws = ActiveSheet '~~> Check if what the user selected is a valid range '~~> User has to select the range which '~~> has data for example Col B If TypeName(Selection) <> "Range" Then MsgBox "Select a range first." Exit Sub End If Set rng = Selection '~~> Ensure that the user doesn't select range with multiple columns If rng.Columns.Count > 1 Then MsgBox "Please select only one column or cell" Exit Sub End If With ws '~~> Get the last row of say Col B lRow = .Cells(.Rows.Count, rng.Column).End(xlUp).Row '~~> Identify the column which will have formulas col = rng.Column + 1 '~~> Identify the range which will contain the formulas Set rng = .Range(.Cells(2, col), .Cells(lRow, col)) '~~> Fill formuals in all in one go rng.FormulaR1C1 = "=CONCATENATE(RC[-1], "" "", ""-0000"")" End With End Sub 

我提供这个作为替代。 如果B列跳过了数据行,这将保证你在C列中的最后一行使用B列。

 Sub Macro1() Dim DataRange As Range Set DataRange = Application.Intersect(ActiveSheet.UsedRange, Range("B:B")) DataRange.Offset(0, 1).FormulaR1C1 = "=CONCATENATE(RC[-1], "" "", ""-0000"")" End Sub