简体   繁体   English

在 excel 中复制一列并将每个条目/单元格加倍。 如何?

[英]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 个条目,所以手动操作不是一种选择。

For example:例如:

在此处输入图片说明

Formula in F1 :公式F1

=INDEX($A1:$D1,1,ROUNDUP((COLUMN()-5)/2,0))

Drag right and down...向右和向下拖动...

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:结果:

在

Formula:公式: 在此处输入图片说明

Result:结果:
在此处输入图片说明

Hold and drag along the rows按住并沿行拖动

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:此过程执行两个步骤:

  1. it assigns data to a 1-based 2-dim array v by one code line,它通过一个代码行将数据分配给一个基于 1 的二维数组v
  2. it restructures the complete array via 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.

相关问题 解析Excel单元格。 怎么样? - Parsing excel cell. How? Excel在同一列中向所有人发送电子邮件,只需要向特定单元格中的所有人发送电子邮件。 怎么样? - Excel sending emails to all people in one Column, need to email only person in specific cell. How? 将数据从Word复制到Excel单元格。 然后在相邻单元格中触发公式。 怎么样? - Copy data from Word to Excel Cell. Then trigger formula in adjacent cell. How? Excel SumProduct在单元格中带有if语句。 - Excel SumProduct with if Statement in a cell. EXCEL:单元格中的数组。 如何取回数组? - EXCEL: Array in a cell. How to get the array back? 如何在Excel中的列中添加每个第n个单元格? - How to add every nth cell in a column in Excel? 从 D 列中提取唯一值并组合在一个单元格中。 Excel VBA - Extract unique values from Column D and combine in a single cell. Excel VBA 在 Excel 检查两个单元格中的短语,如果在两个单元格中找到任何列出的单词,则需要在另一个单元格中添加该单词。 对整列重复此操作 - In Excel Check phrases in two cell and in the two cell if any listed word found,need to add the word in another cell. repeat this for entire column 如何在单个 Excel 单元格中插入多个值并将其导出为该单元格的每个条目的 pdf - How to insert multiple values in a single excel cell and export it as a pdf for every entry of that cell 如何在excel的每个单元格的第一列中插入第二列数字? - how to insert second column of digits in the first in every cell in excel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM