+ Reply to Thread
Results 1 to 14 of 14

Data to Column, remove unwanted fields

Hybrid View

  1. #1
    Registered User
    Join Date
    01-08-2015
    Location
    Mumbai, India
    MS-Off Ver
    2010
    Posts
    5

    Data to Column, remove unwanted fields

    Hello

    I have long list of data saved in word, i want to copy it to excel and with the help of Macro, i want to convert it data to column, but remove some duplicate fields and have same data in one column.

    i have created a macro with steps records, however some of the fields have multiple entries and due to which the row has additional fields, now i want to remove those additional fields, so that i have one data type in one column only, please see the attached word and excel file for example

    below is the code from VBA editor

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '
    Columns("A:A").Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.EntireRow.Delete
    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=True, Comma:=True, Space:=False, Other:=True, OtherChar:= _
    ":", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), _
    Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1) _
    , Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array( _
    19, 1), Array(20, 1), Array(21, 1), Array(22, 1), Array(23, 1), Array(24, 1), Array(25, 1), _
    Array(26, 1), Array(27, 1), Array(28, 1), Array(29, 1), Array(30, 1), Array(31, 1), Array( _
    32, 1), Array(33, 1), Array(34, 1), Array(35, 1), Array(36, 1), Array(37, 1), Array(38, 1), _
    Array(39, 1), Array(40, 1), Array(41, 1), Array(42, 1), Array(43, 1)), _
    TrailingMinusNumbers:=True
    Range("A1").Select
    End Sub
    now i want to remove the additional name, or do something that same type data is in same column
    Attached Files Attached Files
    Last edited by alansidman; 04-19-2022 at 12:50 PM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Data to Column, remove unwanted fields

    With one record from the Word file (e.g. the first one) can you show the result you want in Excel
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    01-08-2015
    Location
    Mumbai, India
    MS-Off Ver
    2010
    Posts
    5

    Re: Data to Column, remove unwanted fields

    Quote Originally Posted by PCI View Post
    With one record from the Word file (e.g. the first one) can you show the result you want in Excel

    example file added on top

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Data to Column, remove unwanted fields

    Is the list of headers shown in your example, the one you want ?
    In other words, there is not the same list of data in all records: Record 3 has 2 allias

  5. #5
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Data to Column, remove unwanted fields

    A possibility could be:
    1 - You copy paste the data from Word to an "working sheet" in Excel*
    2 - You launch next macro
    The first record is used to prepare the list of items
    Option Explicit
    Option Base 1
    
    Sub Act1()
    Const StStr = "%"
    Const EndStr = "µ"
    Dim WkAr
    Dim WkRg  As Range
    Dim I  As Long, II As Long, J As Long, JJ As Long
    Dim Re As Integer
    Dim T, TT, K
    Dim HdDic   As Object
    Set HdDic = CreateObject("Scripting.Dictionary")
    
        On Error Resume Next
        Columns(1).Select  '.SpecialCells(xlCellTypeBlanks).EntireRow.DeleteSomething
        Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        Set WkRg = Cells(1, 1).CurrentRegion
        WkAr = WkRg: WkRg.ClearContents
        For I = LBound(WkAr, 1) To UBound(WkAr, 1)
            Re = 1   '   not 0
            While Re <> 0
                Re = InStr(WkAr(I, 1), ":")
                If Re <> 0 Then
                    WkAr(I, 1) = Replace(WkAr(I, 1), ":", "%", 1, 1)
                    T = InStrRev(WkAr(I, 1), ",", Re)
                    WkAr(I, 1) = Left(WkAr(I, 1), T - 1) & "µ" & Mid(WkAr(I, 1), T + 1)
                End If
            Wend
        Next I
    
        For J = 0 To UBound(Split(WkAr(1, 1), "%"), 1) - 1
            K = Split(WkAr(1, 1), "%")(J)
            T = Split(K, "µ")
            TT = T(UBound(T, 1))
            Cells(1, J + 1) = TT
            HdDic.Item(TT) = J + 1
        Next J
        
        Dim ResAr
        ReDim ResAr(1 To UBound(WkAr, 1), 1 To J)
        II = 1
        For I = LBound(WkAr, 1) To UBound(WkAr, 1)
            For Each K In Split(WkAr(I, 1), "µ")
                TT = Split(K, "%")(0)
                If HdDic.exists(TT) Then
                    JJ = HdDic.Item(TT)
                    T = Split(K, "%")(1)
                    ResAr(II, JJ) = T
                End If
            Next K
            II = II + 1
        Next I
        Cells(2, 1).Resize(UBound(ResAr, 1), UBound(ResAr, 2)) = ResAr
        
    End Sub
    Attached Files Attached Files
    Last edited by PCI; 04-18-2022 at 01:41 PM. Reason: Attach file

  6. #6
    Registered User
    Join Date
    01-08-2015
    Location
    Mumbai, India
    MS-Off Ver
    2010
    Posts
    5

    Re: Data to Column, remove unwanted fields

    ok, so i have data in either in word or text

    now each records alread has header like Name, Alias, NTLT, followed by ":" how can i sort as per the header

    the only problem i face with text to column is thas some of record has Alias, Alias 1, and two city of birth separated by comma.

    please guide me how can i do it

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Data to Column, remove unwanted fields

    What are your comments about the macro sent, is it doing the job, if not give details

  8. #8
    Registered User
    Join Date
    01-08-2015
    Location
    Mumbai, India
    MS-Off Ver
    2010
    Posts
    5

    Re: Data to Column, remove unwanted fields

    thanks for the macro, but i did not understand it, there are two macro and one act 1 thing, also there are multiple tabs like, old, original working etc.

    i did not understand where i can paste data and what steps i follow

    i appreciate your effort, but please explain, or make it little simple

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Data to Column, remove unwanted fields

    OK
    In the file attached in sheet "working Sheet" paste the data from the Word file.
    Then, while you are in "Working Sheet" do control + a to launch macro "Act1"
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Data to Column, remove unwanted fields

    For the data uploaded.

    Select source Word document when the code opens dialog box.
    Sub test()
        Dim fn As String, x, a, b, e, i As Long, n As Long
        Dim myList, m As Object, dic As Object
        Sheets(1).Cells.Clear
        Application.Goto Sheets(1).Range("a1")
        fn = Application.GetOpenFilename("WordDoc,*.doc*", , "Select Word Document")
        If fn = "False" Then Exit Sub
        Application.ScreenUpdating = False
        With GetObject(fn)
            .Range.Copy
            Sheets(1).Paste
            .Application.Quit
        End With
        Set dic = CreateObject("Scripting.Dictionary")
        dic.CompareMode = 1
        myList = Split("Name|Date Of Birth|Gender|City Of Birth|Country Of Birth|" & _
        "Occupation|Employer|Book Number|Category Class|Arrival Date|Case Number", "|")
        With Sheets(1)
            a = .Range("a1", .Range("a" & Rows.Count).End(xlUp)).Value
            ReDim b(1 To UBound(a, 1), 1 To UBound(myList) + 2): n = 1
            .Columns(1).ClearContents: b(1, 1) = "Sr no"
            For Each e In myList
                dic(e) = dic.Count + 2
            Next
            For i = 0 To dic.Count - 1
                b(1, i + 2) = dic.keys()(i)
            Next
            With CreateObject("VBScript.RegExp")
                .Global = True
                .IgnoreCase = True
                .Pattern = "(^" & Join(myList, "|") & ") *: *([^,:]+?)(,)([^,:]+?(?=,))?"
                For i = 1 To UBound(a)
                    If .test(a(i, 1)) Then
                        n = n + 1: b(n, 1) = n - 1
                        For Each m In .Execute(a(i, 1))
                            b(n, dic(m.submatches(0))) = m.submatches(1) & _
                            IIf(m.submatches(3) <> "", ",", "") & m.submatches(3)
                        Next
                    End If
                Next
            End With
            With .[a2].Resize(n, UBound(b, 2))
                .Value = b
                .Columns("c").EntireColumn.Insert
                .Range("b1:c1") = Array("Last Name", "First Name")
                With .Range("b2").Resize(n)
                    .TextToColumns .Cells(1), 1, Comma:=True
                    .Columns(2).Value = Application.Trim(.Columns(2).Value)
                End With
            End With
            .Columns.AutoFit: .[a1].Select
        End With
    End Sub
    Attached Files Attached Files

  11. #11
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,733

    Re: Data to Column, remove unwanted fields

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  12. #12
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,733

    Re: Data to Column, remove unwanted fields

    crossposted: https://chandoo.org/forum/threads/da...-fields.47894/

    Rule 03: Cross-posting Without Telling Us

    Your post does not comply with Rule 3 of our Forum RULES. Do not cross-post your question on multiple forums without telling us about your threads on other forums.

    Post a link to any other forums where you have asked the same question.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Read this to understand why we ask you to do this.

    I have added the crosspost reference for you today. Please comply with this and all our rules in the future

  13. #13
    Registered User
    Join Date
    01-08-2015
    Location
    Mumbai, India
    MS-Off Ver
    2010
    Posts
    5

    Re: Data to Column, remove unwanted fields

    i request admin to delete this post, as i have already one post going on Chandoo forum.. please delete this i am new had no idea its not allowed here, i will continue my search for answer on other forum

    thanks

  14. #14
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,733

    Re: Data to Column, remove unwanted fields

    First, we do not delete posts as this is a large data base that is searched regularly by many and any posts may be of help to others. Secondly, we do not forbid crossposting, we expect you to read the rules and understand that we expect you to let us know when you crosspost. This is standard practice across most Excel Forums including Chandoo. If you read our rules and theirs, you will understand better this request.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Parse data to 2 columns and remove unwanted data with VBA
    By pdiddy9590 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-13-2018, 10:26 AM
  2. Replies: 7
    Last Post: 05-15-2015, 11:57 AM
  3. [SOLVED] Can some one please help me with V.B.A code to remove the unwanted data in the sheet.
    By boddulus in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-08-2014, 12:30 PM
  4. [SOLVED] Macro Remove Unwanted Data
    By timbo1957 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-14-2013, 06:01 AM
  5. Formula to remove unwanted data & rearrange last name & first name in a cell
    By rbecker69 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-20-2013, 03:12 AM
  6. [SOLVED] Need macro to remove unwanted data from large tables
    By s4driver in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 04-26-2013, 08:07 AM
  7. remove unwanted data
    By ahmedalhoseny in forum Excel General
    Replies: 1
    Last Post: 06-14-2010, 04:51 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1