简体   繁体   中英

Excel VBA: Combining two columns into one column

So currently I am using this code

    wsData.Range("b4:b" & lr).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("M6")
    wsData.Range("H4:H" & lr).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("N6")

(lr is the last row)

As well as this cell formula =CONCATENATE(M6," - ",N6)

To combine two columns into one

I would really like to combine these values in the same way into a single column without using the formula so end users don't have to paste-special out of it, is there an easy way to do this?

like this?

wsDest.Range("O6:O" & lr+2).formula = "=CONCATENATE(M6,"" - "",N6)"

Eventually you just want to record the formula with the macro recorder while you enter it manually and you get the correct formula with R1C1 notation!

UPDATE:
The recorded script is:

Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False

adapted to your script a possible solution could look like:

wsDest.Range("O6:O" & lr + 2).Formula = "=CONCATENATE(M6,"" - "",N6)"

wsDest.Range("O6:O" & lr + 2).Copy
wsDest.Range("O6").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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