Excelmacros一次连接一行到文件结尾

我需要一个Excelmacros来连接每行的七列数据,直到达到数据的末尾。 例如,如果我有这样的公式:

=A1&B1&C1&D1&E1&F1&G1 

我怎么写这个macros,以便它像这样一个序列中的每一行增加到文件的末尾?

 =A1&B1&C1&D1&E1&F1&G1 =A2&B2&C2&D2&E2&F2&G2 =A3&B3&C3&D3&E3&F3&G3 

有了这么多的答案,主要关注什么assylias和我突出已经浪费:)

但是,如果你仍然想要一个VBA的答案。 使用这种方法。 这比LoopingAutofill快得多

 Option Explicit Sub Sample() Dim LastRow As Long Dim Ws As Worksheet Set Ws = Sheets("Sheet1") LastRow = Ws.Range("A" & Ws.Rows.Count).End(xlUp).Row '~~> If your range doesn't have a header Ws.Range("H1:H" & LastRow).Formula = "=A1&B1&C1&D1&E1&F1&G1" '~~> If it does then Ws.Range("H2:H" & LastRow).Formula = "=A2&B2&C2&D2&E2&F2&G2" End Sub 

如果您有1000行,那么在运行代码之前,您可能需要closuresScreenupdating并将Calculation更改为Manual,然后在代码结束时重置它们。

我认为最简单的方法是按照assylias的说法填写,但如果你想使用VBA:

 Selection.AutoFill Destination:=Range("Your Fill Range"), Type:=xlFillDefault 

应该复制其他行。

我同意100%的意见和其他答案,为什么你需要VBA来做到这一点,但只是为了回答你原来的问题,这是我如何做到这一点:

 Sub FillAllWithFormula() Dim i As Variant Dim wsht As Worksheet 'If you are using this for a specific Worksheet use the following Set wsht = ThisWorkbook.Worksheets(yourWorksheetName) 'or if you are always using this for the active sheet use the following Set wsht = ActiveSheet For i = 1 To wsht.Rows.Count 'Replace "X" with the column letter you want your formula to appear in wsht.Range("X" & i).Formula = "=A" & i & "&B" & i & "&C" & i & "&D" & i & "&E" & i & "&F" & i & "&G" & i Next Set wsht = Nothing End Sub