将Excel / VBA附加到字符串的前面时,Excel / VBA的吃量超过1撇号

时间:2016-05-23 14:58:22

标签: excel vba excel-vba

我在列表中有一些不相交的数据,我通过将数据连接在一起来修复。不幸的是,一些断点在断点处有撇号,导致excel省略了所述撇号(后来导致分界问题)。

数据分段如下:

(18547,'royalcaribbean','VY04I239','4 Night  Sanya & Chan May Cruise','Voyager of the Seas',4,'Hong Kong','2016-07-17 00:00:00',862,942,1022,1102,0,0,0,0,'2016-05-21 08:11:14'),(18714,'royalcaribbean','VY04I240','4 Night  Sanya & Chan May Cruise','Voyager of the Seas',4,'Hong Kong','2016-08-05 00:00:00',902,969,1119,1129,0,0,0,0,'2016-05-21 08:11:25'),(18277,'royalcaribbean','VY04I241','4 Night  Sanya & Chan May Cruise','Voyager of the Seas',4,'Hong Kong','2016-09-15 00:00:00',729,796,876,982,0,0,0,0,'2016-05-21 08:10:56'),(18122,'royalcaribbean','VY04I242','4 Night  Kaohsiung & Taipei Cruise','Voyager of the Seas',4,'Hong Kong','2016-10-07 00:00:00',662,729,796,916,0,0,0,0,'2016-05-21 08:10:43'),(18266,'royalcaribbean','VY04I251','4 Night  Kaohsiung & Taipei Cruise','Voyager of the Seas',4,'Hong Kong','2016-08-27 00:00:00',737,814,886,982,0,0,0,0,'2016-05-21 08:10:55'),(18465,'royalcaribbean','VY05I279','5 Night  Nha Trang & Chan May Cruise','Voyager of the Seas',5,'Hong Kong','2016-06-21 00:00:00',796,889,982,0,0,0,0,0,'2016-05-21 08:11:08'),(18466,'royalcaribbean','VY05I279','5 Night  Nha Trang & Chan May Cruise','Voyager of the Seas',5,'Hong Kong','2016-07-26 00:00:00
',1116,1209,1289,1472,0,0,0,0,'2016-05-21 08:11:08'),(18793,'royalcaribbean','VY05I280','5 Night  Best Of Okinawa Cruise','Voyager of the Seas',5,'Hong Kong','2016-06-26 00:00:00',995,1036,1102,1222,0,0,0,0,'2016-05-21 08:11:32'),(18794,'royalcaribbean','VY05I280','5 Night  Best Of Okinawa Cruise','Voyager of the Seas',5,'Hong Kong','2016-07-03 00:00:00',942,1036,1129,1249,0,0,0,0,'2016-05-21 08:11:32')

在第2行中,excel没有显示出撇号。当我遍历我的数据时,我尝试将chr(39)添加到开头但是excel也删除了那个!我已经添加了其中两个,但是为之前没有撇号的数据添加了撇号。为什么excel吃第二个撇号?

'Replaces the delimiter between rows by a strange character ~
ch1 = Chr(19)
Selection.Replace What:="),(", Replacement:=ch1, LookAt:=xlPart, _
SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Starts the separation process
Selection.TextToColumns Destination:=Range("A" & dataRowstart), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:=ch1, TrailingMinusNumbers:=True
'Heals data that was chopped in the inital copy into the spreadsheet
For i = dataRowstart + 1 To dataRowEnd 'For each row append the data
    SQLData.Cells(i, 1).Value = Chr(39) & SQLData.Cells(i, 1).Value 'add apostrophe
    SQLData.Cells(i - 1, FindNextEmpty(SQLData.Cells(i - 1, 1)).Column - 1).Value = SQLData.Cells(i - 1, FindNextEmpty(SQLData.Cells(i - 1, 1)).Column - 1).Value & Right(SQLData.Cells(i, 1).Value, Len(SQLData.Cells(i, 1).Value) - 1)
    SQLData.Cells(i, 1).Delete Shift:=xlShiftToLeft 'After concatenating delete the broken piece
Next i

1 个答案:

答案 0 :(得分:0)

使用正则表达式和您的示例,似乎很容易创建一个替代丢失的单引号的规则。

不确定你的所有CleanUp要求,我不确定它是如何适应的。但是在连接线之后,正则表达式模式会查找一个字符串

  • comma开头,然后是single-quote
  • a single-quote
  • 的一系列字符
  • 后跟comma

然后进行替换,以便在逗号之前添加single-quote

Option Explicit
Function ReplaceMissingQuote(R As Range) As String
    Dim S As String
    Dim C As Range
    Dim RE As Object
    Const sPat As String = "(,'[^']+?),"
    Const sRepl As String = "$1',"

'Initialize Regex Engine
Set RE = CreateObject("vbscript.regexp")
With RE
    .Global = True
    .Pattern = sPat
End With

'Concatenate the lines
For Each C In R
    S = S & C.Text
Next C

'Replace the missing single quote
S = RE.Replace(S, sRepl)

ReplaceMissingQuote = S

End Function

以下是使用的正则表达式模式的解释:

( '[^'] +?),

sPat :(,'[^'] +?),

选项:区分大小写; ^ $匹配在换行

  • 匹配下面的正则表达式并将其匹配捕获到反向引用号1 (,'[^']+?)
    • 匹配字符串“,”字面,'
    • 匹配任何不是“{”[^']+?的字符
      • 在一次和无限次之间,尽可能少地根据需要进行扩展(懒惰)+?
  • 匹配字符“,”字面意思,

sRepl:$ 1',

  • 通过捕获组号1 $1
  • 插入上次匹配的文本
  • 插入字符串“',”字面意思',

使用RegexBuddy

创建

编辑:查看您的代码,我想知道以下内容是否有帮助。和以前一样,我们

  • 将所有内容(在本例中为第1列)连接成一个长字符串
  • 替换丢失的单引号
  • ),(序列替换为)cr-lf(
  • 将其分别写回工作表。

您可能需要更改源数据的位置,以及您想要写入结果的位置,但它应该让您入门

Sub CleanUp()
    Dim V As Variant, S As String
    Dim vTemp As Variant
    Dim R As Range
    Dim RE As Object
    Const sPat As String = "(,'[^']+?),"
    Const sRepl As String = "$1',"
    Const sPat2 As String = "\),\("
    Const sRepl2 As String = ")" & vbCrLf & "("
    Dim I As Long

'Initialize Regex engine
Set RE = CreateObject("vbscript.regexp")
    RE.Global = True

'Concatenate the lines
V = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
For Each vTemp In V
    S = S & vTemp
Next vTemp

'Put back the single quote
With RE
    .Pattern = sPat
    S = .Replace(S, sRepl)

'Split at "),("
    .Pattern = sPat2
    S = .Replace(S, sRepl2)
End With

'Place each line into separate cells
vTemp = Split(S, vbCrLf)

ReDim V(1 To UBound(vTemp), 1 To 1)
For I = 1 To UBound(V)
    V(I, 1) = vTemp(I)
Next I

Set R = Range("C3").Resize(UBound(V, 1))
R = V
R.EntireColumn.AutoFit

End Sub

根据您提供的数据,输出如下:

enter image description here