Concatenate a range with a single value

南楼画角 提交于 2019-12-11 02:19:39

问题


I'm trying to Concatenate a range with a single value.

Sub Macro1()
     Dim rngOne As Range, strngOne as String, combos As Range

     'finds the last row and sets it as the ending range
     Dim LastRowColC As Integer
     LastRowColC = Range("C65536").End(xlUp).Row

     Set rngOne = Worksheets(1).Range("C3" & LastRowColC)
     strngOne = "00000"
     combos = rngOne & strngOne

     Range("D3").Select
     Insert combos
End Sub

Why does this not insert the variable "combos" into the cell?

More Explanation (Copied from comments)

Basically I want to take the values in each Cell of column C and add 00000 to the end of all of them. so if C1 was 50 I want the end result to copy over the 50 and replace C1 with 5000000, if C2 was 575 then replace that with 57500000, all throughout the range of data in C.

I would prefer to have it paste over the values in the same column, if that isn't possible. Then for the example you gave I'd want D1= AAA00000, D2=BBB00000, D3 =CCC00000, etc


回答1:


Is this what you are trying? I have given you two ways.

WAY 1

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long
    Dim rng As Range

    '~~> Change this to the relevant worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Get last row in Col C
        lRow = .Range("C" & .Rows.Count).End(xlUp).Row

        '~~> Construct your range
        Set rng = .Range("C3:C" & lRow)

        '~~> Multiply all the cells in the range with 100000
        '~~> so that 55 become 5500000, 123 becomes 12300000 and so on
        rng.Value = Evaluate(rng.Address & "*100000")
    End With
End Sub

WAY 2

Type 100000 in cell D1 and then run this macro

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long
    Dim rng As Range

    '~~> Change this to the relevant worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Get last row in Col C
        lRow = .Range("C" & .Rows.Count).End(xlUp).Row

        '~~> Construct your range
        Set rng = .Range("C3:C" & lRow)

        '~~> This cell has 100000
        .Range("D1").Copy

        '~~> Paste Special Value/Multiply
        rng.PasteSpecial Paste:=xlPasteValues, _
                         Operation:=xlMultiply, _
                         SkipBlanks:=False, _
                         Transpose:=False

        Application.CutCopyMode = False
    End With
End Sub


来源:https://stackoverflow.com/questions/27070173/concatenate-a-range-with-a-single-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!