修改VBA以拆分粘贴的单元格中的文本字符串

时间:2015-01-23 18:54:23

标签: excel vba excel-vba

我有以下代码,需要在y.sheets中添加一个函数,允许我在将范围单元粘贴到" Jan"之后在A列中拆分几个文本字符串。

基本上我想拆分字符串" PB Volume"当它出现在y.sheets上的A列时。

  Private Sub CommandButton22_Click()
Dim x As Workbook
Dim y As Workbook


'## Open both workbooks first:
Set x = Workbooks.Open("Worksheet to Copy")
Set y = Workbooks.Open("Worksheet to Paste")


Dim Rangecells As Variant
    For Each Rangecells In Split("A1:ax10000", ",")
        'Now, copy what you want from x:
        x.Sheets("Report Data").Range(Rangecells).Copy


        'Now, paste to y worksheet:

        y.Sheets("Jan").Range(Rangecells).PasteSpecial
        y.Sheets("Jan").Cells.UnMerge




    Next

'Close x:
x.Close
End Sub

1 个答案:

答案 0 :(得分:0)

未测试:

Dim c as Range, arr

With y.Sheets("Jan")
    .Range(Rangecells).PasteSpecial
    .Cells.UnMerge
    For each c in Application.Intersect(.UsedRange,.Columns(1)).Cells
        If c.value = "PB Volume" Then
             arr = split(c.value, " ")
             c.value = arr(0)
             c.offset(0,1).value=arr(1)
        End If
    Next c
End With