vba Excel - 将文本从一个单元格复制到另一个单元格而不删除原始内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15886046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:29:27  来源:igfitidea点击:

Excel - copying text from one cell to another without deleting original content

excelexcel-vbacopy-pastevba

提问by bs3ac

Basically I have the following scenareo:

基本上我有以下场景:

2 columns, with 600 rows of data.

2 列,600 行数据。

I need to copy the data from column 2 and place it at the end of the content in column1 for the same rows. This would result in column 1 having its original content plus the additional content of column 2.

我需要复制第 2 列中的数据,并将其放在第 1 列中相同行的内容末尾。这将导致第 1 列的原始内容加上第 2 列的附加内容。

Any information in how I can do this will be greatly appreciated.

任何有关我如何做到这一点的信息将不胜感激。

Thanks in advance!

提前致谢!

采纳答案by bs3ac

As this question received a number of views (10,000+) I thought it was important to also share another and far simpler solution:

由于这个问题收到了很多意见(10,000+),我认为分享另一个更简单的解决方案很重要:

In cell C1 use the formula:

在单元格 C1 中使用公式:

=(A1 & B1)

This will copy the content of cell A1 and B1 into cell C1. Drag the formula to all other rows (row 600 in my case).

这会将单元格 A1 和 B1 的内容复制到单元格 C1 中。将公式拖到所有其他行(在我的例子中是第 600 行)。

Then copy the column and paste using 'values only'.

然后复制列并使用“仅值”粘贴。

You will then have cells in column C containing the content of column A and column B in a single cell on a row-to-row basis.

然后,您将在 C 列中的单元格中逐行地在单个单元格中包含 A 列和 B 列的内容。

回答by David

Here's a VBA in a simple form. Just create a macro, add these lines to it. Then select your original column (what you're calling column 1), and run the macro.

这是一个简单形式的 VBA。只需创建一个宏,将这些行添加到其中。然后选择您的原始列(您称之为第 1 列),并运行宏。

a = ActiveCell.Value
b = ActiveCell(1, 2).Value

ActiveCell.Value = a + b

The bracketed cell reference is a relative statement - 1, 2 means "same row, one column to the right" so you can change that if you need. You could make it loop by expanding thusly:

括号中的单元格引用是一个相对语句 - 1, 2 表示“同一行,右侧一列”,因此您可以根据需要进行更改。您可以通过这样扩展来使其循环:

Do
a = ActiveCell.Value
b = ActiveCell(1, 2).Value

ActiveCell.Value = a + b

ActiveCell.Offset(1, 0).Select

If ActiveCell.Value = "" Then
    Exit Do
End If
Loop

That loop will carry on until it finds a blank cell, then it'll stop. So make sure you have a blank cell where you want to stop. You could also add extra characters into the line that combines.. so in the above example it's ActiveCell.Value = a + b, but you could make it ActiveCell.Value = a + " - " + bor anything else that may help.

该循环将继续进行,直到找到一个空白单元格,然后它会停止。因此,请确保您有一个要停止的空白单元格。您还可以在组合的行中添加额外的字符.. 所以在上面的例子中它是ActiveCell.Value = a + b,但您可以制作它ActiveCell.Value = a + " - " + b或任何其他可能有帮助的东西。

回答by David Zemens

This should take the values from column 2 and place them sequentially at the bottom of column 1.

这应该从第 2 列中获取值并将它们按顺序放置在第 1 列的底部。

Sub test()
Dim rng1 As Range
Dim rng2 As Range
Dim cl As Range
Dim r As Long

Set rng1 = Range("A1", Range("A1048576").End(xlUp))
Set rng2 = Range("B1", Range("B1048576").End(xlUp))

r = rng1.Rows.Count + 1

For Each cl In rng2
    Cells(r, 1).Value = cl.Value
    r = r + 1
Next

End Sub

回答by Santosh

Just keep it simple. Here is the code.

保持简单。这是代码。

Sub copyCol()
  Dim lastRow As Long
  lastRow = Range("A65000").End(xlUp).Row
  Range("B1:B" & lasrow).Copy Range("A" & lastRow).Offset(1, 0)
End Sub