Msg box error.

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello guys,

I am facing 2 error messages in my code.

The first time I press match portal, the error is at this line:
Rich (BB code):
wsDestination.UsedRange.Clear,

Maybe this is due to the destination sheet is not created yet. But the second time I press match portal without doing anything the code runs and gives the correct result. How do I avoid this error.?

The second error is that when the code is running it displays a msgbox in the middle of the code and once again at the end of the code. That is twice while running the code. The msgbox line is written only once in the end of the code. I wonder why...
Need your expert help to make these 2 errors vanish.
Two Error messages to remove.xlsm
 
You are initializing your variable wsDestination here:
VBA Code:
Option Explicit

Dim DestinationLastRow          As Long
Dim DestinationRemarksColumn    As String
Dim wsDestination               As Worksheet

Sub Match_Portal()
This would be okay since you use it in both subs but when for some reason you delete the sheet "Edited Portal" the variable will still be "True" and will not get your code which recreates the sheet to work:
Code:
' Create DestinationSheet if it doesn't exist
If Not wsDestination Is Nothing Then DestinationSheetExists = True ' Check to see if the DestinationSheet exists
If DestinationSheetExists = False Then        ' If DestinationSheet does not exist then ...
Sheets.add(after:=wsSource).Name = DestinationSheet '   Create the DestinationSheet after the Source sheet
Set wsDestination = Sheets(DestinationSheet)  '   Assign the DestinationSheet to wsDestination
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Do you mean to say that I shouldn't delete the 3 created sheets to match the next data.?
 
Upvote 0
The only quick workaround is to set the variable wsDestination to Nothing at the beginning of the sub Match_Portal.
 
Last edited:
Upvote 0
Yes Yes Yes. I tested 3 times after replacing true with false and It worked without error. Thank you so much rollis 13.?
 
Upvote 0
Sorry, I edited my last post in the meantime, please check, should be:
HTML:
Set wsDestination = Nothing
 
Upvote 0
Sorry, I edited my last post in the meantime, please check, should be:
HTML:
Set wsDestination = Nothing
Rich (BB code):
If Not wsDestination Is Nothing Then DestinationSheetExists = False                      ' Check to see if the DestinationSheet exists
    If DestinationSheetExists = False Then
I edited the above line. Ain't that right?
 
Upvote 0
Not sure, needs testing but I would just leave that part as it was and add Set wsDestination = Nothing at the begining of the macro.
 
Upvote 0
I changed again like you said and changed false back again to true. Still it is working right.
Rich (BB code):
Set wsDestination = Nothing                                            ' Assign DestinationSheet to wsDestination
         Set wsSource = Sheets(SourceSheet)                                                 ' Assign SourceSheet to wsSource
        Set wsMatched = Sheets(MatchedSheet)                                                ' Assign MatchedSheet to wsMatched
     Set wsMismatches = Sheets(MismatchesSheet)                                             ' Assign MismatchesSheet to wsMismatches
    On Error GoTo 0                                                                         ' Turn Excel error handling back on
'
' Create DestinationSheet if it doesn't exist
    If Not wsDestination Is Nothing Then DestinationSheetExists = True                      ' Check to see if the DestinationSheet exists
    If DestinationSheetExists = False Then                                                  ' If DestinationSheet does not exist then ...
        Sheets.add(after:=wsSource).Name = DestinationSheet                                 '   Create the DestinationSheet after the Source sheet
        Set wsDestination = Sheets(DestinationSheet)                                        '   Assign the DestinationSheet to wsDestination
    End If
 
Upvote 0
It is working fine in both the sample and original data. Thanks once again rollis 13. Good Night.?
 
Upvote 0
The logic of your change in post #16 wasn't correct.
Set wsDestination = Nothing goes before On Error Resume Next
Anyway, glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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