VBA code to filter, text2column and copy data

bombo

New Member
Joined
Nov 23, 2017
Messages
6
Hi everyone,

I need VBA code to filter, text2column, copy and paste processed data in a new sheet.

I need to return text in Question 1, question 2,... columns into number, then filter rows which in Group column is "ABC", and copy - paste these processed data in a new sheet. New sheet will include column Group, Role, Question 1, 2, 3,... (after split text). I also need a textbox to ask which group I want to filter.

My sample data is as below:

GroupRoleQuestion 1Open question 1Question 2Open question 2Question 3Open question 3
ABC

<colgroup><col width="64" style="width:48pt"> </colgroup><tbody>
</tbody>
13 - NeutralNA4 - SatisfiedNA3 - NeutralNA
XYZ24 - SatisfiedNA3 - NeutralNA4 - SatisfiedNA
ABC35 - Very satisfiedNA4 - SatisfiedNA4 - SatisfiedNA
ABC13 - NeutralNA3 - NeutralNA3 - NeutralNA
GHI15 - Very satisfiedNA3 - NeutralNA3 - NeutralNA
ABC23 - NeutralNA3 - NeutralNA3 - NeutralNA
XYI35 - Very satisfiedNA3 - NeutralNA3 - NeutralNA
HIQ34 - SatisfiedNA3 - NeutralNA5 - Very satisfiedNA
ABC34 - SatisfiedNA3 - NeutralNA5 - Very satisfiedNA

<tbody>
</tbody>

Please help me. Thank you!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi, welcome to the board.
Does this do what you're after?
Code:
Sub SplitColFltrCopy()
    
    Dim UsdRws As Long
    Dim Sht As Worksheet
    Dim Col As Long
    Dim Grp As String
    
    Grp = InputBox("Please enter a group")
    If Len(Grp) = 0 Then Exit Sub
    
    Set Sht = ActiveSheet
    UsdRws = Range("A" & Rows.Count).End(xlUp).Row
    
    For Col = 3 To 7 Step 2
        Columns(Col).TextToColumns Destination:=Cells(1, Col), DataType:=xlDelimited, _
           Other:=True, OtherChar:="-", FieldInfo:=Array(Array(1, 1), Array(2, 9))
    Next Col
    
    Sheets.Add(after:=Sheets(Sheets.Count)).Name = Grp
    
    If Sht.AutoFilterMode Then Sht.AutoFilterMode = False
    With Sht.Range("A1:H" & UsdRws)
        .AutoFilter field:=1, Criteria1:=Grp
        On Error Resume Next
        .SpecialCells(xlVisible).copy Sheets(Grp).Range("A1")
        On Error GoTo 0
        .AutoFilter
    End With
    
End Sub
 
Upvote 0
Hi Fluff,

Thank you for helping me!

This code works for my sample data, but it doesn't work in my real data and I don't know how to change it for suitability.

In my real data, the number or rows and columns is not only 9 rows and 8 columns like sample above. Rows and columns are different in each file while the structure is the same. (For example, there are many question and open question columns)

Could you please show me how can I change the code? I also need to convert choice "0 - Do not use services" in Question Columns into "NA" instead of "0".

While applying the code into my file, it said debug in those lines:

Columns(Col).TextToColumns Destination:=Cells(1, Col), DataType:=xlDelimited, _
Other:=True, OtherChar:="-", FieldInfo:=Array(Array(1, 1), Array(2, 9))

Thank you!
 
Upvote 0
This code works for my sample data, but it doesn't work in my real data and I don't know how to change it for suitability.

A Generalized "Please Note"
--------------------------------------
For future questions you may ask, please do not simplify your question for us... doing so will get you a great answer to a question you do not actually have and which you do not actually care about AND it will almost always lead to you coming back for help when the solution we give you for the simplified question cannot be applied to your actual data and its layout. One thing you must keep in mind when you ask a question in a forum... the people you are asking to help you know absolutely nothing about your data, absolutely nothing about how it is laid out in the workbook, absolutely nothing about what you want done with it and absolutely nothing about how whatever it is you want done is to be presented back to you as a result... you must be very specific about describing each of these areas, in detail, and you should not assume that we will be able to "figure it out" on our own. Remember, you are asking us for help... so help us to be able to help you by providing the information we need to do so, even if that information seems "obvious" to you (remember, it is only obvious to you because of your familiarity with your data, its layout and the overall objective for it).
 
Upvote 0
@bombo
Could you please supply a sample of your real data showing both before & after results?
A varying number of rows & columns can be dealt with, but (as pointed out by Rick Rothstein) without a sample of your genuine data, there is not much we can do to help.
 
Upvote 0
@Rick Rothstein: Noted with many thanks.
@Fluff: My actual data is like table below. I have many files which have the same structure like this, the difference is the number of column and row.

I need to:

1. Convert text from Question columns into number, expect choice "0 - Do not use services" into "NA" instead of "0"
2. Filter rows which in Group column is "ABC" (open text box to ask which group needed to be filtered)
3. Copy - paste these processed data in a new sheet

RESPONSE

<tbody>
</tbody>
SUBMITTED

<tbody>
</tbody>
USERNAME

<tbody>
</tbody>
GROUP

<tbody>
</tbody>
ROLE

<tbody>
</tbody>
Question 1Open question 1Question 2Open question 2Question nOpen question n
111/10/2017fdgwgABC1
3 - Neither Agree nor Disagree

<tbody>
</tbody>
NA5 - Strongly agreeNA5 - Strongly agreeNA
211/10/2017rgwrXYZ2
4 - Agree

<tbody>
</tbody>
NA3 - Neither Agree nor DisagreeNA3 - Neither Agree nor DisagreeNA
311/10/2017dgrwfABC30 - Do not use servicesNA3 - Neither Agree nor DisagreeNA0 - Do not use servicesNA
411/10/2017dgwrgJIE3
5 - Strongly agree

<tbody>
</tbody>
NA0 - Do not use servicesNA4 - AgreeNA
511/10/2017gwrfeABC1
5 - Strongly agree

<tbody>
</tbody>
NA5 - Strongly agreeNA3 - Neither Agree nor DisagreeNA
611/10/2017wrfewELO3
5 - Strongly agree

<tbody>
</tbody>
NA4 - AgreeNA4 - AgreeNA
711/10/2017wrfewdABC25 - Strongly agreeNA4 - AgreeNA4 - AgreeNA
811/10/2017gwrgwwGEW1
5 - Strongly agree

<tbody>
</tbody>
NA4 - AgreeNA4 - AgreeNA
n11/10/2017rgwwdsABC1
5 - Strongly agree

<tbody>
</tbody>
NA4 - AgreeNA4 - AgreeNA

<tbody>
</tbody>
 
Upvote 0
Assuming that the first 5 columns are always consistent, try
Code:
Sub SplitColFltrCopy()
    
    Dim Usdrws As Long
    Dim UsdCols As Long
    Dim Sht As Worksheet
    Dim col As Long
    Dim Grp As String
    
    Grp = InputBox("Please enter a group")
    If Len(Grp) = 0 Then Exit Sub
    
    Set Sht = ActiveSheet
    Usdrws = Range("A" & Rows.Count).End(xlUp).Row
    UsdCols = Cells(1, Columns.Count).End(xlToLeft).column

    For col = 6 To UsdCols Step 2
        Columns(col).TextToColumns Destination:=Cells(1, col), DataType:=xlDelimited, _
           Other:=True, OtherChar:="-", FieldInfo:=Array(Array(1, 1), Array(2, 9))
        Columns(col).Replace 0, "NA", xlWhole, , , False, False
    Next col
    
    Sheets.Add(after:=Sheets(Sheets.Count)).Name = Grp
    
    If Sht.AutoFilterMode Then Sht.AutoFilterMode = False
    With Sht.Range("A1", Sht.Cells(Usdrws, UsdCols))
        .AutoFilter field:=4, Criteria1:=Grp
        On Error Resume Next
        .SpecialCells(xlVisible).copy Sheets(Grp).Range("A1")
        On Error GoTo 0
        .AutoFilter
    End With
    
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,520
Messages
6,131,135
Members
449,626
Latest member
Stormythebandit

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top