Search code - See any issues?

erutherford

Active Member
Joined
Dec 19, 2016
Messages
458
found this piece code and its exactly as presented. It works in the provider demo, but not in my new workbook.

Error occurs at the bold red print (object define error)

Rich (BB code):
Sub search_and_extract_singlecriteria()
Dim datasheet As Worksheet
Dim reportsheet As Worksheet
Dim typename As String
Dim finalrow As Integer
Dim i As Integer

'set variables
Set datasheet = Sheet1
Set reportsheet = Sheet2
typename = reportsheet.Range("B2").Value

'clear old data
reportsheet.Range("A5:L100").ClearContents

'goto datasheet and start searching and copying
datasheet.Select
finalrow = Cells(Rows.Count, 1).End(x1Up).Row

For i = 2 To finalrow
    If Cells(i, 2) = typename Then
    Range(Cells(i, 1), Cells(i, 12)).Copy
    reportsheet.Select
    Range("A200").End(x1Up).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
    datasheet.Select
    End If
Next i

reportsheet.Select

Range("B2").Select

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Still doing a little troubleshooting on this piece of code, now that all the typos are fixed. I've stepped through the macro and everything functions correctly except the red highlighted area (most important piece!)

Rich (BB code):
Sub search_and_extract_singlecriteria()
Dim datasheet As Worksheet
Dim reportsheet As Worksheet
Dim typename As String
Dim finalrow As Integer
Dim i As Integer

'set variables
Set datasheet = Sheet1
Set reportsheet = Sheet2
typename = reportsheet.Range("B2").Value

'clear old data
reportsheet.Range("A5:L100").ClearContents

'goto datasheet and start searching and copying
datasheet.Select
finalrow = Cells(Rows.Count, 1).End(x1Up).Row

For i = 2 To finalrow
    If Cells(i, 2) = typename Then
    Range(Cells(i, 1), Cells(i, 9)).Copy
    reportsheet.Select
    Range("A200").End(x1Up).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
    datasheet.Select
    End If
Next i

reportsheet.Select
 
Upvote 0
Other than you still have x1up not xlup in two places, what is not working?
 
Upvote 0
I did fix those last night (pasted the wrong version) Those "1" show up better here!

correct version

Rich (BB code):
Sub search_and_extract_singlecriteria()
Dim datasheet As Worksheet
Dim reportsheet As Worksheet
Dim typename As String
Dim finalrow As Integer
Dim i As Integer

'set variables
Set datasheet = Sheet1
Set reportsheet = Sheet2
typename = reportsheet.Range("B2").Value

'clear old data
reportsheet.Range("A5:I25").ClearContents

'goto datasheet and start searching and copying
datasheet.Select
finalrow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To finalrow
    If Cells(i, 2) = typename Then
    Range(Cells(i, 1), Cells(i, 9)).Copy
    reportsheet.Select
    Range("A25").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    datasheet.Select
    End If
Next i

reportsheet.Select

Range("B2").Select

End Sub

The code its not performing the search, displaying, then pasting
 
Upvote 0
When you step through the code what is it doing?
 
Upvote 0
Is it right that you are pasting to the next blank cell after any data above cell A25?
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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