VBA Find Value and paste ll in between value if seen twice in search (if not, report single)

steveaus

New Member
Joined
May 21, 2015
Messages
11
Hi All,

I've inherited a spreadsheet here and it has stopped working. The macro currently finds a value and reports lines from the data in a new sheet. If the value is found twice it will report every cell in between the first and last value. Example data;

MEMBER NUMBERCOLUMN1COLUMN2
123456ABC
123ABC123
123TOTAL
123456TOTAL


<colgroup><col><col><col></colgroup><tbody>
</tbody>

If i input 123456 into the function box and hit the macro, it will report all of the above. If i use 123 it will report;

MEMBER NUMBERCOLUMN1COLUMN2
123ABC123
123TOTAL

<tbody>
</tbody>

The current macro that is written is here;

Sub createReport()
Dim i As Long
Dim j As Long


Sheet2.Activate
Dim temp As String
Dim str As Variant




temp = Sheet1.TextBox21.value
i = findmin(temp)
j = findmax(temp, i)




If (i = 0 Or j = 0) Then

MsgBox "Account Number Not Found"
Sheet1.Activate
Exit Sub

End If

Sheet3.Cells.Clear
Rows("1:1").Select
Selection.Copy
Application.CutCopyMode = False
Selection.Copy
Sheets("Report").Select
Rows("1:1").Select
ActiveSheet.Paste
'Application.CutCopyMode = False

Sheet2.Activate

Sheet2.range("A" & i & ":" & "N" & j).Select
Selection.Copy
Sheet2.range("A1").Select
Sheet3.Activate
range("A2").Select
ActiveSheet.Paste
Application.CutCopyMode = False

Cells.Select
Selection.Columns.AutoFit
range("A1").Select



MsgBox "Report Created"
End Sub


Function findmin(findvalue As Variant) As Long
Sheet2.Activate
Dim j As Long
Dim i As Long
Dim str As Variant
j = [A1000000].End(xlUp).Row

For i = 2 To j
'MsgBox Cells(i, 1).Value

str = Cells(i, 1).value
If (Mid(str, 1, 1) = "0") Then
If (str / 1 = findvalue / 1) Then
Cells(i, 1).Select
'MsgBox "found At " & i
findmin = i
Exit Function
'i = i + 1
Else
'i = i + 1
'Cells(i, 1).Select
End If
Else

End If
Next i
findmin = 0
Exit Function
End Function


Function findmax(findvalue As Variant, endpoint As Long) As Long
Sheet2.Activate
Dim j As Long
Dim i As Long
i = endpoint
Dim str As Variant
j = [A1000000].End(xlUp).Row

For j = j To i + 1 Step -1
str = Cells(j, 1).value
If (Mid(str, 1, 1) = "0") Then
If (str / 1 = findvalue / 1) Then
Cells(j, 1).Select

findmax = j
Exit Function

Else

End If
Else

End If
Next j
findmax = 0
Exit Function
End Function


At the moment, it doesn't find anything even if i'm searching for something I Know is within the data...

Any ideas? Or a way to simplify ? Or even to create a filter which can do this instead of a macro?

Thanks and appreciate ANY help anyone can give!

Regards,
Steve
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,054
Latest member
juliecooper255

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