'How to find last empty row among many named ranges in worksheet and push the textbox value for selected named range without showing worksheet
off late I got developed interest in excel VB applications and practicing it since last 2 years. ( I use Excel 2016 version )
I have a problem with my recent Vb program. I have designed a main Userform with two ComboBoxes and out of it, ComboBox2 list is depending on ComboBox1 value.
For ComboBox1 row source, I have linked it to one of the named ranges in worksheet Sheet4("Settings") ColumnB which is actually the list of process names and in same sheet @ different columns I have many named ranges (task list) bearing the name of all the items listed for ComboBox1 as headings.
So when I choose list in ComboBox1 dropdown , (Process names, which is also named range) only the relevant options will appear in ComboBox2 (task item) and upto this file is working fine.
Now to add more task line items to named ranges which will appear in combobox2 , I have designed another Userform (2nd one)so that user can add more task options himself under any selected process and he don't have to approach me.
So what I did, in a 2nd Userform I added combobox3 and TextBox1 with submit button. The Combobox3 will show same list as ComboBox1 does -Process names.
Intention of 2nd Userform is when user select process name from Combo3 followed by new task list entered in textbox1 and after submitting, Vb will look for named range selected in combo3, should go to worksheet "Settings" and find empty row in selected process range and will push the value of textbox1-task item. So that user will be able to get newly added value under his selected process in a main Userform.
But the problem is here, while operating the file the excel file is to set to show only Userform, to feel like App and will not display any worksheets during Userform operation. (No excel environment)
However while using 2nd Userform the code is forcing to display " Settings" worksheet (excel environment), since it is forbidden in code for not to navigate to excelworksheet, UF2 action failing in completing task.
However, If I keep Worksheets displaying in background (& 2nd Userform in front) then no issue, I was able to append the textbox1 value to desired named ranges in " Settings" ws.
The Error i get while using 2nd Userform without having excel sheets Opened is : " Run-time Error '1004' , Select method of Range class failed" . I tried using " application.screenUpdate=False/True", but no use, code stops working, unless excel worksheet is displayed...
any help in this regard will be appreciated
Code written :
Private Sub UserForm2_Initialize() ' UF name "addCat"
Dim i As Long
For i = 2 To Sheet4.Range("B50").End(xlUp).Offset(1, 0).Row
Me.SelFnCB.AddItem Sheet4.Cells(i, 2).Value
Next i
End Sub
'---------------------------------------------------------------
Private Sub CatUpdtCmd_Click() 'this is to add new items under selected namedRanges in worksheet
Dim lastRow As Long
Dim ws As Worksheet
i = Me.SelFnCB.Value 'Combobox 3 value with process names as named Range
Set ws = Worksheets("Settings")
Worksheets("Settings").Unprotect "Bnm0000"
Range(i).Select ' at this stage of code execution , screen tries physically go to worksheet and tries to display it.which i dont want.
Range(i).Find("").Activate
ActiveCell.Value = Me.NwCat.Value '( "NwCat" is TextBox1 with new task to be added to selected named range)
Worksheets("Settings").Range("A1").Select
Worksheets("Settings").Protect "Bnm0000"
Me.NwCat.Value = ""
ActiveWorkbook.Save
MsgBox " Done, Select the process again to view your update ", vbInformation, Title:=" Category added "
Unload Me
addCat.Show
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
