Excel VBA troubles

Onimancer

New Member
Joined
Mar 18, 2015
Messages
3
Greetings!

I ve written the below snippet:

Code:
If IsNumeric(FillingForm.txtArel.Value) Then
    flag = False
    i = 2
    j = 2
    id = FillingForm.txtArel.Value
    
    Do While Cells(i, 1).Value <> ""
        If Cells(i, 1).Value = id Then
            flag = True
            
            Set FillingForm.txtSur.Value = Worksheets("mainDB").Cells(i, j + 1).Value
        End If
        
        i = i + 1
        
    Loop
End If
End Sub

It should read an employee's id from txtArel textbox (which works fine) and write his surname in textbox txtSur. Instead it gives me error '9' subscript out of range.

Code:
Set FillingForm.txtSur.Value = Worksheets("mainDB").Cells(i, j + 1).Value
is yellowed.

Thanks in advance!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Most likely cause is that is not the exact name of the worksheet (or the worksheet is not in the active workbook).
 
Upvote 0
You don't need to activate it, you can just qualify the Worksheets call:
Code:
Workbooks("some workbook.xlsx").Worksheets("mainDB")
rather than just:
Code:
Worksheets("mainDB")
for example.
 
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,892
Members
449,477
Latest member
panjongshing

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