在VBA中循环以添加扩展名.jpg

时间:2012-10-27 22:49:28

标签: excel vba loops concatenation

我是VBA的新手,我想在列中包含的值中添加.jpg扩展名。

Header Col G
11125;111545
154645
154511;145521;165421
1156556;13165
567418

我想循环到这个列并为每个值添加一个.jpg(每个单元格多个图像)

编辑:2n尝试并使用一个

Sub image()
Dim c As Range


    For Each c In Selection.Cells
        Dim Words() As String
        Words() = Split(c.Value, ";")
        Dim x As Integer

        For x = 0 To UBound(Words)
        Words(x) = Words(x) & ".jpg"
        Next x
        c.Value = Join(Words, ";")
    Next
End Sub

可能不是最短路..但它有效 谢谢

2 个答案:

答案 0 :(得分:0)

这是一种非标准的方法......

选择您的单元格

dim c as range
for each c in Selection.cells
    c.value = Join(Split(c.value,";"),".jpg;") & ".jpg"
    'This is what I'd actually use:
    'c.value = Replace(c.value, ";", ".jpg;") & ".jpg"
next

要回答您编辑的问题,您所做的错误是:

c.Value = Words(x) & ".jpg"

在这里,您将通过每次迭代覆盖c.Value的值并添加“.jpg”,因此它最终只会包含最后一个值。

如果你想继续使用你的方法,我会建议像:

dim temp
temp = ""
for x = Lbound(Words) to Ubound(Words)
   temp = temp & Words(x) & ".jpg"
next x
c.value = temp

答案 1 :(得分:0)

Sub image()
Dim c As Range


    For Each c In Selection.Cells
        Dim Words() As String
        Words() = Split(c.Value, ";")
        Dim x As Integer

        For x = 0 To UBound(Words)
        Words(x) = Words(x) & ".jpg"
        Next x
        c.Value = Join(Words, ";")
    Next
End Sub