I need a loop for an unknown amount of variables

2 次查看(过去 30 天)
So here is my problem. I am having the user pick an account from a listdlg box, and then they will enter an expense to subtract from that account. That will then calculate what is already in the account (from an excel spreadsheet) and then do the calculation and put the remaining balance into the proper cell in excel. So, I know how to make it do all of this, my issue is I don't know how many accounts they will have. So they could pick account 1 through account 200. I need to be able to make a loop that will do the calculation and pick the correct cell from excel for that account to do the calculations and then put the balance back into the correct cell. Here is what I have so far...
function edit(~,~)
[E N] = listdlg('PromptString','Pick an Account','SelectionMode','Single', 'ListString',accounts);
if E == 1
acctblc = begbalance(2,3);
acctblc=str2mat(acctblc);
expense = inputdlg('Expense to subtract');
expense = str2mat(expense);
endblc = begbalce -expense;
xlswrite('budgetworkbook1.xlsx',endblc,1,D2);
I now realize my if statement wont work because I have no idea how many accounts they will have set up. So the accounts will be in order on the listdlg box how they are in excel. So if they pick account 1, its in cell A1, the beginning balance in is B1, the remaining balance is in C1, etc. help?
  3 个评论
Sergey Kasyanov
Sergey Kasyanov 2018-4-30
Are you need code which allows to choose a one account from excel then enter expense and then save result in the same excel file and that may be repeated many times?

请先登录,再进行评论。

回答(1 个)

Prajit T R
Prajit T R 2018-4-30
Hi Christina
I am attaching a code fragment which would help you:
data=xlsread('bank_data.xlsx')
accounts={'1','2','3','4','5','6'};
[E N] = listdlg('PromptString','Pick an Account','SelectionMode','Single', 'ListString',accounts);
dedn=input('Enter amount to deduct:')
data(E,2)=data(E,2)-dedn; %2 because account balance is in the second column
data
This is the simplest approach I could think of. The value held by the variable data in my example is:
data =
1 11000
2 12000
3 11500
4 11800
5 15000
6 14400
where 1 to 6 are the account numbers and the corresponding value is the account balance. You can make changes as per your requirement and explore this further. Once this is working fine, you can write the variable 'data' back to the Excel sheet.
Hope this helps.
Cheers

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by