very simple and basic help needed

nckbck

New Member
Joined
Mar 28, 2014
Messages
5
hey guys i am new to vba, but have some experience in C++.

long story short, i want to categorize my credit card purcahses (manually) and then wrtie a function to return the total amount spent on that category.

this is what my spreadsheet looks like:

A B
1 clothes 10
2 clothes 10
3 food 2
4 gym 5
5 clothes 10
6 food 3
7 trans 3
8 food 2

Below is the code I cam up with. Note I only want this function to return the price of clothes. my plan was to write seperate functions for the other categories. let me know if you know an easy to do all categories at once.
Function clothes(category As Variant, price As Variant)
'category will be column A in the spreadsheet and price will be column b in the spreadsheet'

Dim i As Integer
i = 0
clothes = 0

Do While (i < 100)
If category(i) = "clothes" Then
clothes = clothes + price(i)
i = i + 1
Else
i = i + 1
Loop

End Function

My problems:

1. I don't know how to properly pass the columns of the spreadsheet into the fuction. I was doing =clothes(A1:A8, B1:B8) . It didn't like that, and I don't know how to debug properly yet. So I am struggling heh.

2. I don't know if my do while loop is messed up. I used the condition while i < 100 just because I don't know a good way to create a terminating condition based on the size of the column. The size of the column will obviously change from month to month so any input here would be appreciated.

3. How come the function does not need a return type? If I were writing this in C++ my function declaration would be: int clothes(string [8] category, int [8] price) . That way I would know i could only assign a int value to clothes.

Let me know what is wrong with my code and/or how to accomplish what I want to do. I appreciate the feedback in advance.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Wonderful. Looks like I will save learning VBA for another day. Thanks for the quick response.
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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