Excel正则expression式:将报价(“)添加到两列中的值

所以我有一个CSV文件,列有两列,如下所示:

在这里输入图像说明

我们的目标是创build一个Excel VB代码,通过列H和I,并在每个6位组的开始和结尾添加一个引号(“),例如H67100到”H67100“。此外,逗号应该独自一人。

我知道代码还没有完成,但这是我迄今为止。 我认为我在开始部分没有问题,但在比赛结束后,我认为我的逻辑/语法是不正确的。 有一点指导和反馈是非常感谢:

Private Sub splitUpRegexPattern2() Dim strPattern As String: strPattern = "(^[a-zA-Z0-9]{6}(?=[,])" Dim regEx As New RegExp Dim strInput As String Dim Myrange As Range Set Myrange = ActiveSheet.Range("H:I") If strPattern <> "" Then strInput = Myrange.Value strReplace = """" & strInput & """" With regEx .Global = True .MultiLine = True .IgnoreCase = True .Pattern = strPattern End With End Sub 

更新代码:

 Function splitUpRegexPattern2 (Myrange As Range) as String Dim regEx As New RegExp Dim strPattern As String Dim strInput As String Dim Myrange As Range Dim strReplace As String Dim strOutput As String strPattern = "(^[a-zA-Z0-9]{6}(?=[,])" If strPattern <> "" Then strInput = Myrange.Value strReplace = """" & strInput & """" With regEx .Global = True .MultiLine = True .IgnoreCase = True .Pattern = strPattern End With If regEx.test(strInput) Then simpleCellRegex = regEx.Replace(strInput, strReplace) Else simpleCellRegex = "Not matched" End If End If End FUNCTION 

添加示例CSV文件。 下载示例CSV文件

这个答案假定你可以得到你感兴趣的每个单元格的值。

在这种情况下,您无需使用RegEx,因为您的值似乎是简单的逗号分隔数据。

 Public Const DOUBLE_QUOTE As String = Chr(34) ''' '''<summary>This function splits a string into an array on commas, adds quotes around each element in the array, the joins the array back into a string placing a comma between each element.</summary> ''' Public Function QuotedValues(ByVal input As String) As String Dim words As String() = input.Split(New Char() {","}) Dim result As String = String.Empty words = (From w In words Select DOUBLE_QUOTE & w.Trim & DOUBLE_QUOTE).ToArray result = String.Join(", ", words) Return result End Function