2017-02-08 5 views
1

クリップボードから選択したセルの複数行のデータに貼り付けるマクロがあります。各行に新しい行が挿入されます。列Aと行1にはヘッダーが含まれ、挿入された行に対してヘッダーが記入されます。VBA内のクリップボードデータから引用符を削除する

Sheet1 
Header0 Header Header Header 
Header1 Data   
Header2 Data Data1 
       Data2 Data 
Header3 Data 

時には引用符が追加されることがありますが、時には引用符が追加されないことがあります。正当な引用符文字を削除せずにクリップボードのデータを消去する方法はありますか?本当に

cellContent array

Sub ClipboardToRows() 
' Split multi-lined data into separate rows for the current selection 
' Assumption is that Column A contains row headers 
Dim currRange As Range, currCell As Range, pasteCell As Range 
Dim rowHeader As String 
Dim cellContent 
Dim cellStr 

Dim clipboard As MSForms.DataObject 
Dim str1 As String 
Set clipboard = New MSForms.DataObject 

clipboard.GetFromClipboard 
On Error GoTo clipEmpty 
str1 = Trim(clipboard.GetText()) 

Application.CutCopyMode = False 
Set currCell = Selection 

rowHeader = Cells(currCell.Row, 1).Value 
'Skip Column A 
If (currCell.Column > 1) Then 
    cellContent = Split(str1, Chr(10)) 
    For i = LBound(cellContent) To (UBound(cellContent)) 
     cellStr = Trim(cellContent(i)) 
     If Len(cellStr) > 0 Then 
      Set pasteCell = currCell.Offset(i) 
      'Set current cell with line 1 
      If i = 0 Then 
       currCell.Value = cellContent(i) 
      Else 
       'If next cell down is not empty or the row header is different 
       If (Not IsEmpty(pasteCell.Value)) Or (Cells(pasteCell.Row, 1).Value <> rowHeader) Then 
        pasteCell.EntireRow.Insert 
        Cells(pasteCell.Row - 1, 1).Value = rowHeader 
       End If 
       currCell.Offset(i).Value = cellContent(i) 

      End If 
     End If 
    Next 
End If 

clipEmpty: 
    If Err <> 0 Then MsgBox "There was an issue with pasting. Please try again." 

End Sub 
+0

おそらく重複していないかもしれませんが、関連しています:http://stackoverflow.com/q/24910288/4996248 –

+0

私はそれを見ていましたが、私の場合、私がコピーしている私のソースは、 Excelセル。だから私はちょうどクリップボードの内容を取得したい(ユーザーがすでにCTRL + Cを何かに押したことを前提としている)。 – Esuriency

答えて

0

ありません。

コピープロセスでクリップボードに二重引用符が一貫して追加されておらず、保存したいソースデータに正当な引用符がある場合は、現在クリップボードにあるものを見て、元のデータにあり、複製プロセスで何が追加されたのかを示します。

あなたができることは、間違って追加された引用符のパターンを認識し、それらを削除しようとすることです。

たとえば、最初と最後のどちらかに二重引用符があるかどうかを調べる場合は、各値をテストします。その場合、その値から先頭文字と最後の文字を削除します。

関連する問題