VBA: check if string in cell has a value. If yes, add 1 to a specific cell, if not, leave it.

TerenceTitus

New Member
Joined
Feb 8, 2017
Messages
20
Hi,

sorry I am stuck, your help is very much appreciated. I am struggling to think of how I can use VBA to check for a number in a cell (string - e.g. 8, 11, 18). Then I would like to tabulate the total numbers into a table of how many cells contained that number.

Here is how the data looks like. Act is for the type of activity. The numbers in the data represent the type of material used.
E.g. for person 1 (row 1), for Act 2 he used let's say (number 3) sand. Person 4 (row 4) also used (number 3). It should add up the total number of times each material is used for each activity.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"]Act1
[/TD]
[TD="align: center"]Act2
[/TD]
[TD="align: center"]Act3
[/TD]
[TD="align: center"]Act4
[/TD]
[TD="align: center"]Act5
[/TD]
[TD="align: center"]Act6
[/TD]
[TD="align: center"]Act7[/TD]
[/TR]
[TR]
[TD="align: center"][/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]9[/TD]
[TD="align: center"][/TD]
[TD="align: center"]6,11[/TD]
[TD="align: center"][/TD]
[TD="align: center"]11[/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="align: center"][/TD]
[TD="align: center"]3,5[/TD]
[TD="align: center"]11[/TD]
[TD="align: center"][/TD]
[TD="align: center"]4,8[/TD]
[TD="align: center"]3[/TD]
[/TR]
[TR]
[TD="align: center"][/TD]
[TD="align: center"]5,11[/TD]
[TD="align: center"][/TD]
[TD="align: center"]8[/TD]
[TD="align: center"]5[/TD]
[TD="align: center"]11[/TD]
[TD="align: center"]6[/TD]
[/TR]
[TR]
[TD="align: center"]9[/TD]
[TD="align: center"]3,10[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]9[/TD]
[TD="align: center"]8[/TD]
[TD="align: center"]11[/TD]
[/TR]
</tbody>[/TABLE]

Here is an example of how the tabulated table would look like based on the data above. Mat is for materials.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]Mat1
[/TD]
[TD]Mat<strike></strike>2
[/TD]
[TD]Mat<strike></strike>3
[/TD]
[TD]Mat<strike></strike>4
[/TD]
[TD]Mat<strike></strike>5
[/TD]
[TD]Mat6<strike></strike>
[/TD]
[TD]Mat7<strike></strike>
[/TD]
[TD]Mat<strike></strike>8
[/TD]
[TD]Mat<strike></strike>9
[/TD]
[TD]Mat10
[/TD]
[TD]Mat<strike></strike>11[/TD]
[/TR]
[TR]
[TD]Act1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]Act<strike></strike>2[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]2[/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD="align: center"]Act<strike></strike>3[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]Act<strike></strike>4[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1<strike></strike>[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1<strike></strike>[/TD]
[/TR]
[TR]
[TD="align: center"]Act<strike></strike>5[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD="align: center"]Act<strike></strike>6[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]2[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[/TR]
[TR]
[TD="align: center"]Act<strike></strike>7[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]2[/TD]
[/TR]
</tbody>[/TABLE]

The VBA does not need to generate a pivot table, just create this table in a different sheet is good enough.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Does it really need to be vba?
I think this formula copied across and down does what you want.

Excel Workbook
ABCDEFGHIJKL
1Act1Act2Act3Act4Act5Act6Act7
2396,1111
333,5114,83
45,1185116
593,109811
6
7
8Mat1Mat2Mat3Mat4Mat5Mat6Mat7Mat8Mat9Mat10Mat11
9Act100100000100
10Act200201000011
11Act300101000100
12Act400000001001
13Act500001100101
14Act600010002001
15Act700100100002
Count Materials (2)
 
Upvote 0

Forum statistics

Threads
1,224,743
Messages
6,180,686
Members
452,994
Latest member
Janick

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