2013-12-19 24 views
1

如果我無法準確解釋我的問題,我對此論壇很陌生,以致表示歉意。在工作簿中使用連接不同數量的行的宏

我在一個工作簿中的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

'當我用我的宏,它停止在1200行'你能告訴我們代碼? –

+0

子宏1() ' ' Macro1的宏 ' ActiveCell.FormulaR1C1 = 「= CONCATENATE(RC [-1], 」「 」「 」「 -0000」, 「)」 範圍( 「B2」),選擇 Selection.AutoFill Destination:= Range(「B2:B12」) Range(「B2:B12」)。Select End Sub – user3120550

+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 
+0

非常抱歉,但有什麼方法可以將評論發佈出去。我是新來的! – user3120550

+0

@ user3120550:評論有助於您理解代碼。一旦你理解了代碼,簡單地刪除它們:)這裏的想法並不僅僅是給你一個解決方案,而是爲了幫助你理解它是如何工作的,以便下一次當你被困住時,你知道該怎麼做:) –

+0

Gotcha ..我會盡力而爲。非常感謝你! – user3120550

0

我提供此作爲替代。如果B列曾經跳過數據行,這將保證你通過B列的最後使用的行獲得在C列的結果

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 
相關問題