我是这个论坛的新手,如果我无法准确解释我的问题,请道歉。
我在一个工作簿中的B列,1-1200行(估计)中有数据,我想在同一工作簿的C列中连接。我知道如何做到这一点并创建一个宏来完成这项任务。
当我的工作簿中包含B列中的数据,行1-2000(估计)时,问题就出现了。当我使用我的宏时,它会在第1200行停止。
有没有办法为连接函数选择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
答案 0 :(得分:1)
这是你在尝试什么?我已对代码进行了评论,因此您无需理解它。如果你这样做,然后发回:)
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
答案 1 :(得分:0)
我提供此选择。如果列B跳过数据行,这将保证您在列B中通过列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