How to change the row values and column values in excel vba - vba

Hi I am very new to Excel VBA
I want to change row and column values
My data is similar to this
My age is My height is My weight is
between 10-20 150 45
between 21-30 170 56
between 10-20 155 60
between 10-20 160 53
between 21-30 173 68
I want to make the data like this
Age Hght Wght
Category A 150 45
Category B 170 56
Category A 155 60
Category A 160 53
Category B 173 68
ie I changed the first row
Then I changed all "between 10-20" to "Category A" and "between 21-30" to "Category B"
I prefer not to use column numbers in code, instead I prefer to use column names.
ie like this,
If Age="between 10-20" then Age="Category A"
How can we do it?

First of all, make sure you put the data in the table. (not simply type in the data in the excel's cells) You should go to INSERT ribbon there insert the table.
Next, follow the advice from #Bryon, create another table called "DataTable" like following figure. You may create this table at another worksheet (e.g. MyData)
Lastly, you create another table called "MyResult". At the 1st row of the table, put the formula in the cell as following based on the columns :
=VLOOKUP(AgeTable[#[My age is]],DataTable,2,TRUE)
column 2
=VLOOKUP(AgeTable[#[My Height is]],AgeTable[#[My Height is]],1,TRUE)
column 3
=VLOOKUP(AgeTable[#[My Weight is]],AgeTable[#[My Weight is]],1,TRUE)
Following is the output of the table:
Whenever values had been change at column 2 and column 3 of table 1, it will change the column 2 and column 3 of table 3

Related

Compare two columns in excel and find the differences with VBA

Hello I am trying to write a macro that compares two columns and tells me if something from the first column does not exist in the second.
A B
1 55
23 68
55 97
68 58
97 90
14
25
So I need to take the values from A and check if they are present in B, if not it shopuld give back the value from A which was not found.
Can you please help me on that
You do not need vba.
This formula will do what you are asking.
=IFERROR(AGGREGATE(15,6,$A$1:INDEX(A:A,MATCH(1E+99,A:A))/(ISERROR(MATCH($A$1:INDEX(A:A,MATCH(1E+99,A:A)),B:B,0))),ROW(1:1)),"")
Put it in the first cell of the desired output and copy down till it returns blanks.

(VBA) Extract and copy and paste column values for specified date range for corresponding row values

I've been stuck on this for a while now. What I need is the title - what I have is:
Sheet 1:
1 billion mostly unnecessary columns.
Column D: Values I need Column F: Column M:
Revenue Names Date
12 John 1/24/2016 2:40:02 AM
15 Sarah 3/2/2016 4:35:17 PM
14 Sarah 7/17/2016 1:50:10 PM
20 Matt 8/20/2012 4:16:12 AM
10 John 11/19/2015 5:04:05 AM
etc. etc.
Current Sheet 2: Pivot Table*
Row Label:
Sarah
Matt
John
etc.
Desired Sheet 2 Pivot Table+*
Row Label: Column __:
Revenue
Sarah 29
Matt 0 *(note: see below, but = 0 because Matts value corresponded to date 2012)
John 22
etc.
The important thing about Sheet 2 is that I would like to tell VBA to find the nearest emptiest column within the sheet, and input the values from Column D from S1 (and sum for duplicates) but only sum and extract values that range from today's date to 11 months prior. Currently I made a module to automatically create a pivot table for the next sheet, but I have wanted to add the above for a while, just stuck.
I'm also assuming I won't have to specify sum if I tell VBA to extract data for corresponding row label names? And I was thinking about putting a Begin Date and End Date cells on Sheet 2 to refer to, or using the TODAY() function somehow for the date, but not sure how to specify to extract and sum values for the date range from today to 11 months prior.
Edit: *Please note this is a watered down version of my current situation, everything is bigger so to avoid confusion, pivots are necessary, but I choose to dilute it for the sake of this issue.
Do you really need a pivot table?
If you dont, just create a new sheet with all the names, set a cell for the starting date you want to consider and do =sumifs
For example
On Sheet2 B1 you put 1/24/2016.
Then on A2 you insert the name (Sarah, for example)
On B2 you put the formula
=Sumifs(Sheet1!D:D,Sheet1!F:F,A2,Sheet1!M:M,">"&B$1)
After that just make a list of name and drag the formula beside every name.
*My excel is in portuguese so maybe the formula needs some other ajustments to work.

Excel VBA selecting data from a sorted Table

I am running into a problem in VBA in excel.
I am trying to create a participant registration program in excel for a sports tournament. One can add data like the weight, age and name of a participant. And then based on that, The participants are divided into poules named with letters A, B... until Z. I have a table which can sort these poules by letters and then for example only display the participants which are in poule A such as below
Example
Now i want to count the numbers with VBA macros of participants in the poule which is displayed by the sorted table. For example when i sort on poule A it has to return 3 and when i sort on B, it has to return 2.
Determined by the number of participants in a poule the program will print a different poule scheme, depending on the number of participants. (i have this part in VBA)
The problem is when i have a sorted table like below
Example 2
It counts all the participants, and not just the ones in B or any other sorted letter.
I have this:
Sub Count()
Dim nRows As Integer
nRows = Range(Range("A18"), Range("A18").End(xlDown)).Rows.Count
MsgBox (nRows)
End Sub
This works well if you sort A, but when you sort any other letter, it counts All the table until that letter. Eg. when you sort B, it displays 5 (Number of A + B).
I have looked on the internet for a really long time to find a solution but without succes.
I hope my question is clear and that somebody can help me.
I am assuming that you are using worksheet functions. Use Subtotal when working with filtered data.
These parameters evaluate all cells visible or hidden
1 AVERAGE
2 COUNT
3 COUNTA
4 MAX
5 MIN
6 PRODUCT
7 STDEV
8 STDEVP
9 SUM
10 VAR
11 VARP
These parameters evaluate only visible cells
101 AVERAGE
102 COUNT
103 COUNTA
104 MAX
105 MIN
106 PRODUCT
107 STDEV
108 STDEVP
109 SUM
110 VAR
111 VARP
The code does work now, except that it only counts the first letters it encounters.
So when the first column for the poules is for example A A A E A A B B E.
And i sort to A and use the count function, it only returns a value of 3 and not of 5 (because there are 5 A's)
When I sort the table to A, it looks like this (column number, poule value):
14 A
15 A
16 A
18 A
19 A
And it returns just a count of 3, have you maybe got any fixes for that problem as well?
Pictures:
sorted tabel to E
Table
Range.SpecialCells will return a range of only visible cells.
Dim rSource As Range
Dim rVisibleCells
Set rSource = Range(Range("A2"), Range("A2").End(xlDown))
Set rVisibleCells = rSource.SpecialCells(xlCellTypeVisible)
MsgBox rVisibleCells.Rows.Count

Pivot Table Number Ranges - Novice

I am a complete novice to excel and having my first go with Pivot Tables.
I have sheets with lost of data mostly numeric, these sheets contain up to 80,000 rows and 25 columns.
I am using pivot tables to filter the data and have managed to get it going ok to give me the values I require.
However I would like to now add the ability to break down the filter ranges into numbers with between values.
For example I have one particular column with numbers ranging from -600 to +2450.
I would like to look at values between 35 and 180 for example without having to select multiple values in the filter and then tick every box i require.
Is this possible for a novice (I have no idea what or where VBA is having had a look around
IF it is possible would it then be possible to do the same for columns that contained a certain piece of text
Hope someone can help
Ian
this is how i would do it.
let's say your "one particular column with numbers ranging from -600 to +2450" is column A.
You can set a new column with the formula IF(OR(A*>=35,A*<=180),1,0),
drag the formula to fill the rest of the cells in the new column,
filter the new column for 1 and it should display all the rows where the values are more than 35 and less than 180.
if it doesn't work with the pivot table, try select all the data in the pivot table, copy and paste the data as values to a new sheet.
you can try it for text as well. just if you are searching for "APPLES" in a column A,
use IF(A*="APPLES",1,0) in a new column and filter out 1.
hope this helps.
For ad-hoc analysis, you could right-click on the relevant field header and select Value Filters and Between. This will save ticking/unticking every number in the range.
You can also group your data field. If you e.g. have the following pivot table layout:
Header1 Header2 Data
A AB 1234
AC 2345
AD 3456
B BB 4567
you can select e.g. AB-AC and right-click and Group... the items. This will add another column with the grouped Header2 fields:
Header1 Header2_2 Header2 Data
A Group1 AB 1234
AC 2345
AD AD 3456
B BB BB 4567

Using VLookup to find corresponding row

I have 5 sheets in an excel file named
roll 1to2.5
roll2.5to5
roll5to7
roll 7to9.5
roll 9.5to12
Each sheet has two columns with the following data:
A B
1 22
2 25
3 29
4 20
5 18
6 26
7 19
8 16
9 21
10 20
Now I have been able to do the following: In column C, if I enter a number from Col A, say "7", i get the corresponding value from Col B i.e. "19". I used the following formula
=VLOOKUP(C5,A1:B10,2,FALSE)
This works good uptill here.
Issue 1: Let's say I want to enter the roll in a cell say "5.5", it should automatically consider data from sheet 3 (roll5to7)
Issue 2: and then if I enter a value of ColB, say "20", it should pick up the corresponding value from ColA i.e. "4" (first match) in sheet 3.
How can I achieve this?
To get the right sheet name list your 5 sheet names in one column and in the previous column the lower bound for each (1, 2.5, 5, 7 and 9) and name that two column table Table
Now you can use this formula
=VLOOKUP(C5,INDIRECT("'"&LOOKUP(D5,Table)&"'!A1:B10"),2,FALSE)
where D5 contains the roll
LOOKUP finds the correct sheet name and INDIRECT converts text to a valid reference
Edit:
If you want to look for C5 in column B and find the corresponding value from column A then INDEX/MATCH would look like this:
=INDEX(INDIRECT("'"&LOOKUP(D5,Table)&"'!A1:A10"),MATCH(C5,INDIRECT("'"&LOOKUP(D5,Table)&"'!B1:B10"),0))