Writing VBA macro for generating missing variable

ryan8200

Active Member
Joined
Aug 21, 2011
Messages
357
Hello members,

I would like to write VBA macro for listing the missing variables as per below.

Among candidates A, B C, D and E. I would like to list down the missing variables (Hobbies), accompanied by the candidates name.

[TABLE="width: 500"]
<tbody>[TR]
[TD]Hobbies[/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]Swimming[/TD]
[TD]Y[/TD]
[TD]Y[/TD]
[TD]Y[/TD]
[TD]N[/TD]
[TD]N[/TD]
[/TR]
[TR]
[TD]Tennis[/TD]
[TD]N[/TD]
[TD]Y[/TD]
[TD]Y[/TD]
[TD]Y[/TD]
[TD]N[/TD]
[/TR]
[TR]
[TD]Badminton[/TD]
[TD]N[/TD]
[TD]N[/TD]
[TD]Y[/TD]
[TD]Y[/TD]
[TD]Y[/TD]
[/TR]
[TR]
[TD]Volleyball[/TD]
[TD]Y[/TD]
[TD]N[/TD]
[TD]N[/TD]
[TD]Y[/TD]
[TD]N[/TD]
[/TR]
</tbody>[/TABLE]

The expected outcome will be looked like as below:


[TABLE="width: 500"]
<tbody>[TR]
[TD]Hobbies [/TD]
[TD]Candidate[/TD]
[/TR]
[TR]
[TD]Tennis[/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]Badminton[/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]Badminton [/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]Volleyball[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]
Volleyball
<strike></strike>
[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]Swimming[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]Swimming[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]Tennis[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]Volleyball[/TD]
[TD]E[/TD]
[/TR]
</tbody>[/TABLE]


Your kind assistance is greatly appreciated.

Many thanks.

Regards,
Ryan
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
How am I confused here? It looks to me from the top section that Candidate A does swimming and volleyball rather than tennis and badminton?

Oh, sorry...I reread it clearly and you want what they don't have an interest in! Sorry.
 
Last edited:
Upvote 0
With your top table starting in A1, the results via this routine begin in A8.

Code:
Sub MissingHobbies()
Dim c As Integer, h As Integer, ans As Integer
ans = 8
For c = 2 To 6    ' candidates
  For h = 2 To 5  ' hobbies
  If Cells(h, c) = "N" Then
  Cells(ans, 1) = Cells(h, 1)
  Cells(ans, 2) = Cells(1, c)
  ans = ans + 1
  Else
End If
Next h
Next c
End
End
End Sub

Let me know.
 
Upvote 0
The F5 key will only run a macro if you are in the VB Editor & the cursor is located somewhere inside a macro. Otherwise, while on the sheet, it opens the GoTo dialog box.
Just assign a keyboard shortcut to the macro if you want to run it without selecting the Developer tab and the Macros option.
 
Upvote 0
I'm running the macro inside the VB editor. What do you mean by the cursor is located somewhere inside a macro ?
 
Upvote 0
You have to click somewhere on the macro's code while in the editor, then F5 will launch it.
 
Upvote 0
You have to click somewhere on the macro's code while in the editor, then F5 will launch it.


I hit F5 on the VBA window while I place my cursor on any cell on the data file. I have tried on other VBA code which executing other functions. They worked.
 
Upvote 0
You're misunderstanding something. Open the VBA window, click anywhere in this macro code. Now the cursor is ON THIS MACRO. Pressing F5 will run it.
Why are you going through all that effort when you could either (a) create a keyboard shortcut to run the macro or (b) click on Macros on the Developer tab and double-click on this one?
 
Last edited:
Upvote 0
You're misunderstanding something. Open the VBA window, click anywhere in this macro code. Now the cursor is ON THIS MACRO. Pressing F5 will run it.
Why are you going through all that effort when you could either (a) create a keyboard shortcut to run the macro or (b) click on Macros on the Developer tab and double-click on this one?

Hello Kweaver,

Thanks. I have placed the starting cell at B2. Now, it works. How careless were I :(

Now, I would like to add header "Candidate" and "Hobbies" on the output and displayed the output on new worksheet ?

Can you help me on that ?

Many thanks and sorry for occupying your precious time.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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