0

我对 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

可能不是最短的方法..但它的工作原理谢谢

4

2 回答 2

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
于 2012-10-27T22:55:38.773 回答
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
于 2012-10-28T01:15:59.823 回答