Find colums vba code needed

gnusmas

Board Regular
Joined
Mar 5, 2014
Messages
186
I need vba code!

Exemple i have the following columns and i want to extract columns just they get : 06, 15, 43

Sheet 1

01010302010404010304
03060406020505050506
06080608060606060607
07140911101108081008
12161212121411101312
13171313171714131519
15201616182017141622
18241820212120151825
19302021222221202027
28322223252323222128
29372528292525292931
31392631302727303233
32412933312929313334
33423134373030353536
34433235383131363943
35443836393533374144
43474237434040384246
46494340444343434347
4746434544464744
4947474647474947
48474848
48
49

<tbody>
</tbody>


Sheet 2 output

010103
030505
060606
070810
121013
131315
151416
181518
192020
282221
292929
313032
323133
333535
343639
353741
433842
464343
474744
494947

<tbody>
</tbody>
Thank for help!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
gnusmas,

I have looked at your thread several times, and, I think I now understand what your actual raw data setup is?????

Sample worksheets:


Excel 2007
ABCDEFGHIJKLMNOPAPAQARAS
101010302010404010304
203060406020505050506
306080608060606060607
407140911101108081008
512161212121411101312
613171313171714131519
715201616182017141622
818241820212120151825
919302021222221202027
1028322223252323222128
1129372528292525292931
1231392631302727303233
1332412933312929313334
1433423134373030353536
1534433235383131363943
1635443836393533374144
1743474237434040384246
1846494340444343434347
194746434544464744
204947474647474947
2148474848
2248
2349
24
Sheet1



Excel 2007
ABC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Sheet2


After the macro in worksheet Sheet2:


Excel 2007
ABC
1010103
2030505
3060606
4070810
5121013
6131315
7151416
8181518
9192020
10282221
11292929
12313032
13323133
14333535
15343639
16353741
17433842
18464343
19474744
20494947
21
22
23
24
Sheet2


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub GetColumns_F_O_AQ()
' hiker95, 09/27/2014, ME808154
Dim w1 As Worksheet, w2 As Worksheet
Dim lr As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
w2.Columns("A:C").ClearContents
lr = w1.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
w1.Range("F1:F" & lr).Copy w2.Cells(1, "A")
w1.Range("O1:O" & lr).Copy w2.Cells(1, "B")
w1.Range("AQ1:AQ" & lr).Copy w2.Cells(1, "C")
Application.CutCopyMode = False
With w2
  .Columns("A:C").AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetColumns_F_O_AQ macro.
 
Upvote 0
If hiker has the correct interpretation of the requirement, then possibly also this
Rich (BB code):
Sub Get_Cols()
  Dim lr As Long
  
  lr = Sheets("Sheet1").Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, SearchFormat:=False).Row
  Sheets("Sheet2").Columns("A:C").ClearContents
  Sheets("Sheet2").Range("A1").Resize(lr, 3).Value = Application.Index(Sheets("Sheet1").Columns("A:AQ"), Evaluate("row(1" & ":" & lr & ")"), Split("6 15 43"))
End Sub
 
Upvote 0
Try:-
Code:
Dim Rng As Range
Set Rng = Sheets("Sheet1").Range("A1").CurrentRegion
Sheets("Sheet2").Range("A1").Resize(Rng.Rows.Count, 3).Value = Application.Index(Rng, Evaluate("Row(1:" & Rng.Rows.Count & " )"), Array(1, 8, 9))
 
Upvote 0
thank you for help but i need all columns that contains 06, 15, 43 not just allways 3 columns!!
exemple if sheet 1 contains 100 columns and just 35 columns contains 06, 15, 43 the Sheet 2 output 35 columns!!
Best rgds
 
Upvote 0
hi, friend another exemple here loocking for columns just contains 13, 25, 31

Sheet 1

02020102020801020102010303010302020202010202
03061006111102030203080505050503040303020407
04101309121703080405090708130705060404030509
09131511131906110507100914141008100505061110
10141913152109121308111115151209151606071214
18152119162411151513131216161310182012081315
21162321262514171614151320211412202213091616
23172422292616181722171422261513222314141817
24182525312722201923182123281614232416201919
25232828322826222025202226302116242518252021
28243029353128242729232728332219252621302522
29253238384129253031242931352320302723333125
31273739394331303135263032372424312827383226
34303840424532313836303234392532323128423329
35323941434635363937313635402734333233433431
36334042454740404139323940432836343534443832
42344146464842414344384043463242353640453933
43364247474946454445404545483346404041464438
4446464848474645464446464936424346484641
464848494746464846454647494843
49484846484846
4949

<colgroup><col span="22"></colgroup><tbody>
</tbody>
Sheet 2 output

0202
0304
0505
0711
0812
1313
1416
2218
2319
2520
2925
3131
3532
3633
3734
3938
4439
4544
4646
48

<colgroup><col span="2"></colgroup><tbody>
</tbody>
result just 2 columns contains 13, 25, 31

thank you for help!
 
Upvote 0
gnusmas,

thank you for help but i need all columns that contains 06, 15, 43 not just allways 3 columns!!
exemple if sheet 1 contains 100 columns and just 35 columns contains 06, 15, 43 the Sheet 2 output 35 columns!!
Best rgds

This makes a BIG difference.

Are you always looking in worksheet Sheet1 just for 3 numbers in each column?

Are the numbers in Sheet1, numbers, or, text?
 
Last edited:
Upvote 0
As it stands, this code assumes the values in Sheet1 are numerical, formatted to always show 2 digits, hence the leading zeros.
If Sheet1 contains text values then uncomment the two green lines of code and comment (or remove) the line immediately above each of those.
Rich (BB code):
SSub GetCols()
  Dim a, b
  Dim i As Long, j As Long, k As Long, x As Long, y As Long, rws As Long
  
  Const LookFor As Long = 3 '<- No. of values per column to find. See 'Case' statement below
  
  a = Sheets("Sheet1").UsedRange.Value
  rws = UBound(a, 1)
  ReDim b(1 To rws, 1 To 1)
  For j = 1 To UBound(a, 2)
    k = 0
    For i = 1 To rws
      Select Case a(i, j)
        Case 13, 25, 31
'        Case "13", "25", "31"
          k = k + 1
      End Select
      If k = LookFor Then
        x = x + 1
        ReDim Preserve b(1 To rws, 1 To x)
        For y = 1 To rws
        b(y, x) = a(y, j)
        Next y
        Exit For
      End If
    Next i
  Next j
  With Sheets("Sheet2")
    .UsedRange.ClearContents
    .Range("A1").Resize(rws, x).NumberFormat = "00"
'    .Range("A1").Resize(rws, x).NumberFormat = "@"
    .Range("A1").Resize(rws, x).Value = b
  End With
End Sub
 
Last edited:
Upvote 0
gnusmas,

The following macro will ask for three, 2 digit text numbers, with a single space character between the three text numbers.

For the below example, I entered:
13 25 31


Sample raw data in worksheet Sheet1:


Excel 2007
ABCDEFGHIJKLMNOPQRSTUVW
102020102020801020102010303010302020202010202
203061006111102030203080505050503040303020407
304101309121703080405090708130705060404030509
409131511131906110507100914141008100505061110
510141913152109121308111115151209151606071214
618152119162411151513131216161310182012081315
721162321262514171614151320211412202213091616
823172422292616181722171422261513222314141817
924182525312722201923182123281614232416201919
1025232828322826222025202226302116242518252021
1128243029353128242729232728332219252621302522
1229253238384129253031242931352320302723333125
1331273739394331303135263032372424312827383226
1434303840424532313836303234392532323128423329
1535323941434635363937313635402734333233433431
1636334042454740404139323940432836343534443832
1742344146464842414344384043463242353640453933
1843364247474946454445404545483346404041464438
194446464848474645464446464936424346484641
20464848494746464846454647494843
2149484846484846
224949
23
Sheet1


After the macro in worksheet Sheet2:


Excel 2007
ABC
10202
20304
30505
40711
50812
61313
71416
82218
92319
102520
112925
123131
133532
143633
153734
163938
174439
184544
194646
2048
21
Sheet2


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub GetColumnsV2()
' hiker95, 09/29/2014, ME808154
Dim w1 As Worksheet, w2 As Worksheet
Dim lr As Long, lc As Long, nc As Long, c As Long
Dim n1 As String, n2 As String, n3 As String, tns As String
Dim s, i As Long, nlr As Long
Dim n1r As Range, n2r As Range, n3r As Range
tns = InputBox("Enter the 3, 2 digit, text numbers " & vbLf & " separated by a single space character.")
If Len(tns) <> 8 Then
  MsgBox "You have entered the incorrect string '" & tns & "' - macro terminated!"
  Exit Sub
End If
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
w2.UsedRange.ClearContents
s = Split(tns, " ")
n1 = s(0): n2 = s(1): n3 = s(2)
With w1
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  For c = 1 To lc
    Set n1r = .Columns(c).Find(n1, LookAt:=xlWhole)
    Set n2r = .Columns(c).Find(n2, LookAt:=xlWhole)
    Set n3r = .Columns(c).Find(n3, LookAt:=xlWhole)
    If (Not n1r Is Nothing) * (Not n2r Is Nothing) * (Not n3r Is Nothing) Then
      nc = nc + 1
      nlr = .Cells(Rows.Count, c).End(xlUp).Row
      w1.Range(w1.Cells(1, c), w1.Cells(nlr, c)).Copy Destination:=w2.Range(w2.Cells(1, nc), w2.Cells(nlr, nc))
      Application.CutCopyMode = False
    End If
    Set n1r = Nothing
    Set n2r = Nothing
    Set n3r = Nothing
  Next c
End With
With w2
  .Columns(1).Resize(, nc).AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetColumnsV2 macro.
 
Last edited:
Upvote 0
gnusmas, it would be really useful to know if your values are numbers or text so we know, for example, if "02" is the actual number two formatted to show a leading zero or if it is a text string.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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