简体   繁体   English

VBA 如何将文本插入到已包含文本的单元格中

[英]VBA how to insert text into a cell that already contains text

I have a worksheet that contain some numbers in rows, I would like to convert to columns with "," in the end of the number, for example:我有一个工作表,其中包含一些行中的数字,我想转换为数字末尾带有“,”的列,例如:

5001 5002 5003

After convert转换后

5001,
5002,
5003,

I have this code already我已经有这个代码


Dim range1 As Range, Range2 As Range, Rng As Range
Dim rowIndex As Integer
Set range1 = Application.Selection
Set range1 = Application.InputBox("Source Ranges:", xTitleId, range1.Address, Type:=8)
Set Range2 = Application.InputBox("Convert to (single cell):", xTitleId, Type:=8)
range1.Select
    Selection.NumberFormat = "@"
rowIndex = 0
Application.ScreenUpdating = False
For Each Rng In range1.Columns
    Rng.Copy
    Range2.Offset(rowIndex, 0).PasteSpecial Paste:=xlPasteAll, Transpose:=False
    rowIndex = rowIndex + Rng.Rows.Count
Next

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub

图片示例

Will add a quick mock-up of some code to capture an array then iterate through the array and print to cell.将添加一些代码的快速模型以捕获数组,然后遍历数组并打印到单元格。

dim arr as variant, i as long
arr = range(cells(4,1),cells(4,7)).value
for i = 1 to 7
    cells(3+i,10).value = arr(1,i) & ","
next i

Note this is untested, but should give an idea about what is being displayed in your updated post (aligns with the comment i made).请注意,这是未经测试的,但应该让您了解更新后的帖子中显示的内容(与我所做的评论一致)。

Something very basic (needs work to catch errors):一些非常基本的东西(需要努力捕捉错误):

Sub TransposeWithComma()

Dim range1 As Range: Set range1 = Application.InputBox("Source Ranges:", xTitleId, Type:=8)
Dim range2 As Range: Set range2 = Application.InputBox("Convert to (single cell):", xTitleId, Type:=8)
range2.Resize(range1.Columns.Count).Value = Application.Evaluate("TRANSPOSE(" & range1.Address & ")&"",""")

End Sub
  • Let the user select a bunch of cells in a row, say A1:G1让用户 select 连续一堆单元格,比如说A1:G1
  • Let the user pick a single cell where you want the values transposed to让用户选择您希望将值转置到的单个单元格
  • Resize the single cell to match the source and concatenate the values from the source with a comma using Evaluate调整单个单元格的大小以匹配源并使用Evaluate将源中的值用逗号连接起来

在此处输入图像描述

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

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