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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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