0

我有一个带有 excel 单元格的列,其中多个值由“,”或“&”(来自数据库)分隔。如何通过 excel VBA 控制写字板?

样本数据 -

1111


1112,12311,2321,12312 & 23123

12321


1111

1115

1123, 12312

1211

1111,2321 & 2321

2321 & 1211

所需的 O/P 在 1 列唯一值中没有重复项。

到目前为止,我一直在将数据复制到工作表中 ---> 删除 dup。values ---> Text To Columns ---> 1.seperate by '&' 2.复制第二列中的值,删除重复。和空白--->将其粘贴在原始数据下方--->文本到列--->用','分隔--->如果大于1,则按每个复制数据->在原始数据下方转置和pste数据。

我目前正在尝试在这里实现 -> 复制所有原始数据 -> 删除 dup -> 粘贴到写字板 -> 将 ',' 和 '&' 替换为 '^p' (这是我的好朋友建议的,为我节省了很多时间)并替换为原始数据。

我已经达到了将数据粘贴到写字板中并且不知道如何通过 excel VBA 替换写字板中的数据。谁能帮帮我?

            'Create and copying the required range to word
               
                Dim iTotalRows As Integer   ' GET TOTAL USED RANGE ROWS.
                iTotalRows = Worksheets("Quote #").UsedRange.Rows.Count
            
                Dim iTotalCols As Integer   ' GET TOTAL COLUMNS.
                iTotalCols = 2
                
                
                ' WORD OBJECT.
                Dim oWord As Object
                Set oWord = CreateObject(Class:="Word.Application")
                oWord.Visible = True
                oWord.Activate
                
                ' ADD A DOCUMENT TO THE WORD OBJECT.
                Dim oDoc
                Set oDoc = oWord.Documents.Add
                
                ' CREATE A RANGE FOR THE TABLE INSIDE WORD DOCUMENT.
                Dim oRange
                Set oRange = oDoc.Range
            
                ' CREATE AND  DEFINE TABLE STRUCTURE USING
                    ' THE ROWS AND COLUMNS EXTRACTED FROM EXCEL USED RANGE.
                oDoc.Tables.Add oRange, iTotalRows, iTotalCols
            
                ' CREATE A TABLE OBJECT.
                Dim oTable
                Set oTable = oDoc.Tables(1)
                'oTable.Borders.Enable = True
            
                Dim iRows, iCols As Integer
            
                ' LOOP THROUGH EACH ROW AND COLUMN TO EXTRACT DATA IN EXCEL.
                For iRows = 1 To iTotalRows
                    For iCols = 1 To iTotalCols
                        Dim txt As Variant
                        txt = Worksheets("Quote #").Cells(iRows, iCols)
                        oTable.cell(iRows, iCols).Range.Text = txt        ' COPY (OR WRITE) DATA TO THE TABLE.
            
                        ' BOLD HEADERS.
                        'If Val(iRows) = 1 Then
                         '   objTable.cell(iRows, iCols).Range.Font.Bold = True
                        'End If
                    Next iCols
                Next iRows
                
                'to replace text code reference source pasted below
                
                With oWord.ActiveDocument.Content.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                
                .Text = ","
                .Replacement.Text = "^p"
                .wrap = 1 '.Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute Replace:=2 'reference : - 
                End With
                
                Set oWord = Nothing
                
                 

              

我正在尝试用“^p”替换“,”或“&”,该单词将被视为新行。希望这可以帮助。

需要 O/P : -

1112

12311

2321

12312

23123

12321

1111

1115

1123

1211

4

1 回答 1

1

好的,根据评论和帖子不需要删除重复项。我建议在这里使用字典。以下情况的示例代码。根据我的理解,所需的输出应该类似于 C 列

在此处输入图像描述

以下代码将执行此操作

Option Explicit

Sub RemoveDuplicatesFromDBExtract()

Const COMMA = ","
Const AMPERSAND = "&"

Dim i As Long
Dim rg As Range
Dim sngCell As Range
Dim vDat As Variant
Dim vContent As Variant

' Add a reference to the Microsoft Scripting Runtime
' Select Tools->References from the Visual Basic menu.
' Check box beside "Microsoft Scripting Runtime" in the list.
Dim dict As Scripting.Dictionary

    Set dict = New Scripting.Dictionary

    ' Adjust for your need
    Set rg = Range("A1:A9")

    For Each sngCell In rg
        vContent = Replace(sngCell.Value, COMMA, vbCrLf)
        vContent = Replace(vContent, AMPERSAND, vbCrLf)
        vDat = Split(vContent, vbCrLf)

        For i = LBound(vDat) To UBound(vDat)
            On Error Resume Next
            ' If vdat(i) is already in the dictionary it will not be added twice
            dict.Add Trim(vDat(i)), Trim(vDat(i))
            On Error GoTo 0
        Next i

    Next sngCell

    ' Write output, adjust for your needs
    Range("C1").Resize(dict.Count, 1) = WorksheetFunction.Transpose(dict.Items)

End Sub
于 2018-10-15T07:56:07.000 回答