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!
 
Yes "02" is the actual number two formatted to show a leading zero.
thank very much for your help
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
gnusmas,

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

For the below example, I entered:
2 5 8


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
20303
30505
40708
50809
61310
71412
82213
92314
102516
112919
123120
133524
143632
153734
163936
174442
184546
1946
20
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 GetColumnsV3()
' hiker95, 09/30/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 Long, n2 As Long, n3 As Long, tns As String, n As Long
Dim s, i As Long, nlr As Long
Dim n1r As Range, n2r As Range, n3r As Range
tns = InputBox("Enter the 3 numbers " & vbLf & " separated by a single space character.")
n = Len(tns) - Len(WorksheetFunction.Substitute(tns, " ", ""))
If n <> 2 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 GetColumnsV3 macro.
 
Upvote 0
This would allow you to enter any number of numbers you want to match. Omit leading zeros when entering the numbers.
Rich (BB code):
Sub GetCols()
  Dim a, b
  Dim i As Long, j As Long, k As Long, x As Long, y As Long, rws As Long, LookFor As Long
  Dim Found As String, Crit As String
  
  Crit = InputBox("Enter search numbers separated by spaces. Omit leading zeros")
  If Len(Crit) > 0 Then
    LookFor = UBound(Split(Crit)) + 1
    Crit = " " & Crit & " "
    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
      Found = " "
      For i = 1 To rws
        If InStr(1, Crit, " " & a(i, j) & " ", 1) > 0 Then
          If InStr(1, Found, " " & a(i, j) & " ", 1) = 0 Then
            k = k + 1
            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
            Found = Found & a(i, j) & " "
          End If
        End If
      Next i
    Next j
    With Sheets("Sheet2")
      .UsedRange.ClearContents
      If x > 0 Then
        .Range("A1").Resize(rws, x).Value = b
      End If
      .Activate
    End With
  End If
End Sub
 
Upvote 0
Great job friends !!
here i just need vba code to delete last line of each group!!

Sheet 1

050631394449
061822293744
040812162744
092426374446
052021263147
081022263137
081426363942
050934383942
042227363846
062122374142
031618193742
071022253243
021925303239
011214313246
101921243335

<colgroup><col span="6"></colgroup><tbody>
</tbody>

Sheet 2 output like this

050631394449
061822293744
040812162744
092426374446
081022263137
081426363942
050934383942
042227363846
031618193742
071022253243
021925303239
011214313246

<colgroup><col span="6"></colgroup><tbody>
</tbody>

I apreciate so much your help!
 
Upvote 0
gnusmas,

Thanks for the feedback.

You are very welcome. Glad we could help.


here i just need vba code to delete last line of each group!!

I would suggest that you start a NEW thread for your very different new request.
 
Upvote 0

Forum statistics

Threads
1,216,404
Messages
6,130,376
Members
449,578
Latest member
TT123

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