Forum

Running Excel Forms...
 
Notifications
Clear all

Running Excel Forms VBA Code in separate sheet

6 Posts
2 Users
0 Reactions
96 Views
(@anushreesinghh)
Posts: 3
Active Member
Topic starter
 

Hi,

I was following your Excel Forms tutorial available at this URL to create a Dairy Farm Data Management system in Excel. The code is working fine if the "Add Expense" button is placed within the "Expense Data" sheet. However I want to place the button in the sheet named "Module" and then make the form add data into the sheet named "Expense Data".

I tried the modification suggested by you in this comment:

Hi Kara,
Of course it’s possible, here is the place where you can change the destination:
‘Add row to bottom of Expenses table
ActiveSheet.ListObjects(“Expenses”).ListRows.Add

‘Enter data from form into our new row
Set ExpenseTable = ActiveSheet.ListObjects(“Expenses”)

You can change it to:

‘Enter data from form into our new row
Set ExpenseTable = ThisWorkbook.Worksheets(“Sheet1”).ListObjects(“Expenses”)
‘Add row to bottom of Expenses table
ExpenseTable.ListRows.Add

You should create the table in the sheet you want before running the code, it is expecting to find a table there, it will not create it from code.

However, I am getting “Compile Error: Variable not defined”. Can you please advice? I am attaching my workbook. Thanks.

 
Posted : 01/10/2020 12:56 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi

Please attach your workbook with the code that is generating the error.

Phil

 
Posted : 01/10/2020 2:14 am
(@anushreesinghh)
Posts: 3
Active Member
Topic starter
 

Hi, please find attached my workbook. Missed it by mistake last time.

 
Posted : 01/10/2020 6:04 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi,

This line

Set ExpenseTable = ThisWorkbook.Worksheets("ExpenseData").ListObjects("Expenses")

had curly quotes like this

“”

around ExpenseData and Expenses, instead of the straight quotes that VBA uses ""

The variable LastRow wasn't being set either so I added a couple of lines to find the LastRowNum and then set LastRow to the first cell in the last row of the table.

Everything works for me now.

Regards

Phil

 
Posted : 01/10/2020 6:35 am
(@anushreesinghh)
Posts: 3
Active Member
Topic starter
 

Hi, thank you so much for the solution. It works.

 
Posted : 01/10/2020 7:39 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

You're welcome.

 
Posted : 01/10/2020 8:27 am
Share: