Message box for error

Edgarvelez

Board Regular
Joined
Jun 6, 2019
Messages
197
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Very new to VBA and I have a code that works great.
All it really does is selects the value of a cell in sheet one goes to another sheet finds it and copies the cell to the right of it and then goes on and paste the the vale to another sheet.
If it does not find this specific value then I get "Run-time error "91": Object variable or With block variable not set" which is ok because it could not find what it was looking for.
I would like to add a message box so it can display "Description Code Not Found Add to Database"
and I would like to reset the macro back to the beginning.

sh1.Range("D21").Select
rngY = ActiveCell.Value
Sheets(Range("D7").Value).Select
Columns("A:A").Select
Selection.Find(What:=rngY, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Copy
sh2.Cells(Rows.Count, 12).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
sh2.Cells(Rows.Count, 26).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
sh2.Cells(Rows.Count, 18).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
sh2.Cells(Rows.Count, 25).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
Application.CutCopyMode = False
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,

Try following update to your code & see if helps you

VBA Code:
Dim foundcell As Range
    
    rngY = ActiveCell.Value
    
    Set foundcell = Columns("A:A").Find(What:=rngY, LookIn:=xlFormulas, LookAt:=xlWhole, _
                                        SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                        MatchCase:=False, SearchFormat:=False)
    
    If Not foundcell Is Nothing Then
        foundcell.Offset(0, 1).Copy
        sh2.Cells(Rows.Count, 12).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
        sh2.Cells(Rows.Count, 26).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
        sh2.Cells(Rows.Count, 18).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
        sh2.Cells(Rows.Count, 25).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
        Application.CutCopyMode = False
    Else
        MsgBox "Description Code " & rngY & Chr(10) & "Not Found Add to Database", 48, "Record Not Found"
    End If

Dave
 
Upvote 0
Hi
Do you mean like below, keeping the code line for D21 & D7?


Dim foundcell As Range
sh1.Range("D21").Select
rngY = ActiveCell.Value
Sheets(Range("D7").Value).Select
Set foundcell = Columns("A:A").Find(What:=rngY, LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not foundcell Is Nothing Then
foundcell.Offset(0, 1).Copy
sh2.Cells(Rows.Count, 12).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
sh2.Cells(Rows.Count, 26).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
sh2.Cells(Rows.Count, 18).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
sh2.Cells(Rows.Count, 25).End(xlUp)(2).Resize(sh1.Range("D28").Value).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Else
MsgBox "Description Code " & rngY & Chr(10) & "Not Found Add to Database", 48, "Record Not Found"
End If

Or can we do this to shorten it up sine we are really not going to copy and paste anything until way further down the code and this is only to check that what it is searching for is available.

Dim foundcell As Range
sh1.Range("D21").Select
rngY = ActiveCell.Value
Sheets(Range("D7").Value).Select
Set foundcell = Columns("A:A").Find(What:=rngY, LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not foundcell Is Nothing Then
Else
MsgBox "Description Code " & rngY & Chr(10) & "Not Found Add to Database", 48, "Record Not Found"
End If
 
Upvote 0
Hi,
you don't need to use select in the code

VBA Code:
rngY = sh1.Range("D21").Value



Solution just answered resolving the error 91 issue you requested help with and then addition of msgbox displaying if search value not found which you should be able to adapt for your project. If need further guidance, helpful if you post all the code plus copy of of your worksheet using MrExcels XL2BB - Excel Range to BBCode addin with sample data and an explanation of outcome required.

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,888
Members
449,097
Latest member
dbomb1414

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