简体   繁体   English

使用VBA跨列复制文本

[英]Copy Text across Columns with VBA

I have a column in an excel workbook that contains an 'output example' and another column that contains a definition. 我在excel工作簿中有一个列,其中包含一个“输出示例”和另一个包含定义的列。 I need the output example to be in the definition. 我需要输出示例在定义中。 So I wrote a macro that puts the phrase "Ex:" at the end of the text, then copies the text from the adjacent column and paste's it after the "Ex:" 所以我写了一个宏,在文本的末尾加上短语“Ex:”,然后复制相邻列中的文本并在“Ex:”之后粘贴它。

   Sub Exampletransfer()
'
' Exampletransfer Macro
'
' Keyboard Shortcut: Ctrl+Shift+E
'
    Range("H2").Select
    ActiveCell.FormulaR1C1 = _
        "The age range, in years of a customer. Ex: "
    Range("I2").Select
    ActiveCell.FormulaR1C1 = "1-4"
    Range("H2").Select
    ActiveCell.FormulaR1C1 = _
        "The age range, in years of a customer. Ex: 1-4"
    Range("H3").Select
End Sub

I attached a photo. 我附上了一张照片。 I need this to happen to the whole column. 我需要这个发生在整个专栏中。 I guess I don't understand how to get a macro to continue down a column. 我想我不明白如何让宏继续下一列。

What I have What I need 我拥有 什么我需要什么

Here you go. 干得好。 Make sure you change where you data starts, in both the Range and the Cells method! 确保在Range和Cells方法中更改数据的开始位置!

Sub AppendData()

Dim myRange As range
Dim myCell As range

'Change where you data starts. My practice started in B4, so that's
'what I used.
Set myRange = range("B4", Cells(Rows.count, "B").End(xlUp))


For Each myCell In myRange
    If myCell.Value <> "" Then
        myCell.Value = myCell.Value & " Ex: " & myCell.Offset(0, 1).Value
    End If
Next myCell


End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM