[英]Copying a column in excel and doubling every entry/cell. How to?
Let's say we have a row of elements, 1 per cell: 1,2,3,4.假设我们有一行元素,每个单元格 1 个:1,2,3,4。 I want to copy this row(or column) and double every entry: 1,1,2,2,3,3,4,4.
我想复制这一行(或列)并将每个条目加倍:1、1、2、2、3、3、4、4。
Is there any formula, function, etc that does this?是否有任何公式,函数等可以做到这一点? Thanks a lot.
非常感谢。
I have about 20k entries so doing it manually is not an option.我有大约 20k 个条目,所以手动操作不是一种选择。
You could use:你可以使用:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long, j As Long, LastColumn1 As Long, LastColumn2 As Long, Add1 As Long, Add2 As Long
Dim str As String
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow
LastColumn1 = .Cells(i, .Columns.Count).End(xlToLeft).Column
For j = 1 To LastColumn1
LastColumn2 = .Cells(i, .Columns.Count).End(xlToLeft).Column
If LastColumn2 = LastColumn1 Then
Add1 = 2
Add2 = 3
Else
Add1 = 1
Add2 = 2
End If
.Range(.Cells(i, LastColumn2 + Add1), .Cells(i, LastColumn2 + Add2)).Value = .Cells(i, j).Value
Next j
Next i
End With
End Sub
Results:结果:
Easy alternative using the advanced possibilities of Application.Index()
使用
Application.Index()
的高级可能性的简单替代方案
This approach demonstrates the advanced restructuring possibilities of the ► Application.Index()
function whose row and column arguments are fed by arrays instead of single numeric indices.这种方法展示了 ►
Application.Index()
函数的高级重构可能性,该函数的行和列参数由数组而不是单个数字索引提供。
Main procedure RedoubleCols
主程序
RedoubleCols
This procedure executes two steps:此过程执行两个步骤:
v
by one code line,v
,Application.Index
where the row and column arguments are arrays returned by helper functions AllRows()
and RDC()
;Application.Index
重构整个数组,其中行和列参数是辅助函数AllRows()
和RDC()
返回的数组; the resulting array is written back to a given target.Sub RedoubleCols(rng As Range, rng2 As Range)
' Purpose: get column values and write them back in pairs
' Param.: 1-rng: source range, 2-rng2: target range
' Method: uses the advanced features of the Application.Index function
Dim v ' declare variant (array)
' [1] get data
v = rng.Value2
' [2] rearrange data by redoubling columns (RDC) and write them to a given target range
rng2.Value2 = Application.Index(v, AllRows(UBound(v)), RDC(UBound(v, 2)))
End Sub
Helper functions used by main procedure above上述主要过程使用的辅助函数
Function AllRows(ByVal n&) As Variant
' Purpose: create transposed Array(1,2,...n)
Dim i&: ReDim tmp(n - 1)
For i = 0 To n - 1
tmp(i) = i + 1
Next i
AllRows = Application.Transpose(tmp)
End Function
Function RDC(ByVal n&) As Variant()
' Purpose: create Array(1,1,2,2,...n,n) containing pairs of each column number
Dim i&: ReDim tmp(2 * n - 1) ' declare counter and zero based counter array
For i = 0 To n - 1 ' redouble column counters
tmp(i * 2) = i + 1
tmp(i * 2 + 1) = i + 1
Next i
RDC = tmp ' return counter array
End Function
Example Call示例调用
The essential code line in section [3]
simply calls the main procedure RedoubleCols
:部分
[3]
的基本代码行简单地调用了主过程RedoubleCols
:
RedoubleCols src, target
where source range and target range can be defined following your needs - cf sections [1]
and [2]
.其中源范围和目标范围可以根据您的需要定义 - 参见第
[1]
和[2]
。
Sub ExampleCall()
' [1] Identify source range
Dim src As Range
Set src = ThisWorkbook.Worksheets("MySheet").Range("A1:D2")
' [2] define any target, e.g. 1 column to the right of source data
Dim target As Range, r&, c&
r = src.Rows.Count: c = src.Columns.Count
Set target = src.Offset(0, c + 1).Resize(r, c * 2) ' reserve double space for columns
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' [3] write redoubled source range columns back to target
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RedoubleCols src, target
End Sub
Recommended link推荐链接
Treating Some peculiarities of the the Application.Index function处理Application.Index 函数的一些特性
Assuming 1
is in A1 and you prefer rows.假设
1
在 A1 中并且您更喜欢行。
To avoid dragging down for 20k entries I suggest in E1:为了避免拖延 20k 个条目,我建议在 E1 中:
=INDEX($A1:$D1,,INT((COLUMN()-3)/2))
dragged across to L1 and then double click the fill handle.拖到 L1,然后双击填充手柄。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.