VBA error message that notes row number

ratchet1369

Board Regular
Joined
Mar 16, 2009
Messages
58
Office Version
  1. 365
Platform
  1. Windows
Hey all,

I had a VBA code that was working. It would look for an error condition when Ledger = ABC and Company = V3 and would prompt a message box advising the user "Error on row 20".

They now want to look for company V3 or V4 so I changed the IF into a Case to look for multiple items and now my rowcounter says "Error on row 70", which is the end of the range instead of telling me row 20, which is where the error exists.

Old Code
For rowCounter1 = vCompanyCell.Row To TotalsCell.Row - 1
If (CStr(ws.Cells(rowCounter1, vCompanyCell.Column)) = "V3" And vLedger = "ABC") Then
vResponseCode = -2
gCompanyMsg = "- [Error] Do not use V3 in combination with ABC Ledger, see row " & rowCounter1
End If


New Code

For rowCounter1 = vCompanyCell.Row To TotalsCell.Row - 1
If vLedger = "ABC" Then
Select Case compvalues
Case CStr(ws.Cells(rowCounter1, vCompanyCell.Column)) = "V3", "V4": vResponseCode = -2
End Select
gCompanyMsg = "- [Error] Invalid Company for ABC Ledger, see row " & rowCounter1

End If

Any suggestions on how to direct the user to the row in error?
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
How about
Code:
Select Case CStr(Ws.Cells(rowCounter1, vCompanyCell.Column))
Case "V3", "V4": vResponseCode = -2
End Select
 
Upvote 0
I'm still getting row 70 in my message box. It's as though it's not stopping when there's an error and noting the error row but going to the last row and reporting that one.
 
Last edited:
Upvote 0
That sounds as though the first instance of ABC is on row 70.
You may also need to move this line
Code:
gCompanyMsg = "-   [Error] Invalid Company for ABC Ledger, see row " & rowCounter1
Into the Select case
 
Upvote 0
Figured it out. I had to nest the Cases. Row 70 was the end of my range and with ABC being true, it was showing the last cell instead of the cell that met both criteria. I went with the below solution. Works. Unsure if it's best

Select Case vLedger
Case "ABC"
Select Case CStr(ws.Cells(rowCounter1, vCompanyCell.Column))
Case "V3", "V4": vResponseCode = -2
gCompanyMsg = "- [Error] Invalid Company for ABC Ledger, see row " & rowCounter1
End Select
End Select

Not the Message Box returns row 20 as desired.
 
Upvote 0
Glad you figured it out & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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