VBA删除列中的一些冒号

时间:2016-05-26 07:22:53

标签: json xml excel vba excel-vba

//:celebrity/:channel[:id='239']/:linear/:schedules[:scheduleId='TV_239_2036-05-11_03:00:00.000' and :startTime='2036-05-11T03:00:00.000+0000']/:authorization[:linearAuth='false' and :linearSubAuth='false' and :authCode='NA']

我有一个包含多个XPath的列。我需要JSON版本,所以我需要删除所有冒号字符(:)但不删除日期(不在scheduleId和startTime中)。这意味着只有在冒号之前删除它们:“/”或“[”或“”
如何使用VBA代码?感谢

3 个答案:

答案 0 :(得分:2)

假设您正在使用Column A,请尝试以下操作:

Sub Demo()
    Columns("A:A").Replace What:="/:", Replacement:="/", LookAt:=xlPart
    Columns("A:A").Replace What:="[:", Replacement:="[", LookAt:=xlPart
    Columns("A:A").Replace What:=" :", Replacement:=" ", LookAt:=xlPart
End Sub

EDIT1 :基于评论的代码

Sub Demo()
    i = WorksheetFunction.Match("iXpath", Range("A1:Z1"), 0)
    Columns(i).Replace What:="/:", Replacement:="/", LookAt:=xlPart
    Columns(i).Replace What:="[:", Replacement:="[", LookAt:=xlPart
    Columns(i).Replace What:=" :", Replacement:=" ", LookAt:=xlPart

End Sub

在这里,我在iXpath范围内搜索A1:Z1,您可以根据需要更改此范围。

EDIT2 :适用于所有工作表的代码

Sub Demo()
    Dim ws As Worksheet, i As Long
    For Each ws In ThisWorkbook.Worksheets
        With ws
            i = WorksheetFunction.Match("iXpath", .Range("A1:Z1"), 0)
            .Columns(i).Replace What:="/:", Replacement:="/", LookAt:=xlPart
            .Columns(i).Replace What:="[:", Replacement:="[", LookAt:=xlPart
            .Columns(i).Replace What:=" :", Replacement:=" ", LookAt:=xlPart
        End With
    Next
End Sub

答案 1 :(得分:0)

试试这个...... " MyString的"是您的内容的字符串 " MyReplacer"是替代冒号的东西(可以是空格,没有任何东西或任何其他字符)

MyString = "//:celebrity/:channel[:id='239']/:linear/:schedules[:scheduleId='TV_239_2036-05-11_03:00:00."
MyReplacer = " " 'here add what must replace the ":"
MyString = Replace(MyString," :",MyReplacer)
MyString = Replace(MyString,"[:",MyReplacer)
MyString = Replace(MyString,"/:",MyReplacer)

答案 2 :(得分:0)

您也可以这样做。我基于显示的输入,因此=的位置可能会改变,示例中有2个。

#include <stdio.h>

int x,y;

int main( void )
{
    for ( x = 0; x < 10; x++, printf( "\n" ) )
        for ( y = 0; y < 10; y++ )
            printf( "%c", 1 );

    return 0;
}