VBA Excel 2007 - Merge values in a string - vba

I have a string who looks like this:
a -> 80
b -> 40
c -> 20
a -> 10
What I'd like to know is if it's possible to have this output:
a -> 80 + 10 = 90
b -> 40
c -> 10
The code would find the values that are repeated and then "merge" the numbers who are next to it. It's a string, so I assume it's not possible to do the calculation, but if there's a way to do the a -> 80 + 10 it would be also fine (and save me a lot of time).
Thanks in advance.

ummm... not quite sure what you mean, but if the letters represent columns, what you could do is scroll to the furthers down cell, so for example is you have 10 cells with numbers in column a and 15 in column b and 17 in column c you'd scroll down one cell past 17, and put this formula: =SUM(C1:C17) in the formula bar, (its c1 to c17 because in my example to you there were 16 used cells in colujmn c, and that was the most used cells in one column) and then u drage that left and right so basically it will sum each column..

Related

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

Parse Google sheets cell input to place in adjacent cell

How can I set things up in google sheets such that it will take my input in, say cell B2, clean it up and put things in cells C2 and D2?
For example, say I put in cell B2 the string "ts 4 x 4 x 3/8 x 13'-5". I can of course write a script called std_shape that can parse this into the ouput array ["HSS4X4X3/8", "13'-5""] and when I put =std_shape(B2) into the cell C2, I'll get what I want: C2 will contain HSS4X4X3/8 and cell D2 will contain "13'-5"". But how to do this such that column C & D will automatically fill in from any row in B? Using an arrayformula() tends to fill downward on column C.
What would be the most efficient way of going about this? I want to be able to make a list of material in column B and then have columns C & D format my list in the way described. For instance columns C & D would be derived from my B column inputs as below (where I've separated the columns with the "|" symbol):
> B | C | D
> ------------------------+---------------+----------
> TS 4x4x1/4 x 13'-4 | HSS4X4X1/4 | 13'-4"
> L 5 x 3-1/2 x 5/16 x 6' | L5X3-1/2X5/16 | 6'-0"
> PL 1/2 x 8 x 10 | PL1/2X8 | 0'-10"
> W10x12 x 13' | W10X12 | 13'-0"
Columns C & D have standardized what was put into B. Further cells in the spreadsheet will work with these standard texts for vlookups and other calculations. I want to keep B as input though. I also wish to validate the input in B as well. For instance, say the W10x12 above cannot be found in a vlookup, then column C for that row would say something like "W10x12 is not a valid entry".
What I'm saying is that I don't know how to handle ranges in my script. I also would like to know the most efficient way of going about this. After all, for any given row, columns C & D only care about that particular row's B value - I don't want my function tossing arrays around willy nilly. Maybe some sort of "onEdit" type deal that creates columns C & D whenever the B entry is edited for any particular row?
I'm having brain stalls with this since user-defined functions can't touch any other cells except in overflow. There's GOT to be some way to do such a thing as I'm describing here?

Multiply column A values to Column B values and result in column C using excel VBA

I have two columns A and B. Range of A is A1:A6 and B is B1:B6. Now, I want to multiply these two columns and display result in column C using excel VBA. For example
A B C
2 3 6 (It is A1*B1)
3 8 24
9 2 18
7 3 21
2 4 8
5 4 20
I have tried this code but did not find it helpful:
Range("C1:C6").Value = Range("A1:A6") * Range("B1:B6")
Please guide me here as I am new to VBA. You help is really appreciated.
Alternatively,
for i = 1 to 6
cells(i,3) = cells(i,1) * cells(i,2)
next i
Try this. I have commented the code so that you will not have a problem understanding it. But if you still do then simply ask :)
'~~> This will enter the formula in the entire range in one go
Range("C1:C6").Formula = "=A1*B1"
'~~> This will convert the formula to values
Range("C1:C6").Value = Range("C1:C6").Value

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))

How to sum data in one column based on the same month in another column using macros

I am using macros in excel 2007 for my work. I am working with many data and I need to sum data from 2 or more rows in the same coloumn according to the same month. However the month column is expressed as date.
for example, i have series of data
A B
2/10/2008 2
2/10/2008 3
4/10/2008 3
5/11/2008 4
5/11/2008 5
I want the result to be displayed in column C and D as followed
C D
Oct/08 8
Nov/08 9
I am very thankful if anyone can help me.
regards,
Tifu
A B C D E F
1 10/ 1/2008 24106 1 Oct-08 24106 8
2 10/31/2008 24106 7 Nov-08 24107 11
3 11/ 1/2008 24107 8 Dec-08 24108 6
4 11/30/2008 24107 3
5 12/ 1/2008 24108 2
6 12/ 2/2008 24108 4
B1 =MONTH(A1)+YEAR(A1)*12
E1 =MONTH(D1)+YEAR(D1)*12
F1 =SUMIF(B$1:B$6,CONCATENATE("=",E1),C$1:C$6)
I had to overcome two problems to solve this. First, SUMIF can only do direct comparison, it cannot run a function on the source location (except for range functions, which the date and time functions are not), so I had to add the B column. The E column is optional, it could be implemented as part of the formula in F, but I left it independent for illustrative purposes. Second, SUMIF takes a string parameter describing the comparison, so I built the necessary string (it is "=24106" for F1) on the fly.
using array functions:
C1:
=date(2008,small(month($A$1:$A$10),1),1)
C2:
=date(2008,small(month($A$1:$A$10),2),1)
right click on these cells and format them as mmm/yy
D1:
=sum(if(month($C1)=month($A$1:$A$10),($B$1:$B$10),0))
make sure to press ctrl-shift-enter when done writing this formula.
then copy an paste it down as needed.
If you are unfamiliar with VBA, I would start off by recording a macro while doing what you want to do by using the Subtotals feature under the Data menu (i.e. through Excel's interface).
Once the macro is recorded, you can look at the VBA code produced, and alter it to suit your needs.
This should be possible to do using regular excel formulas:
extract the month of the date (some Month-function)
then do a conditional sum (function sumwhen (?)) like this:
= SumWhen(A:B, ReferenceDate, B:B)
(with ReferenceDate = C2 C3 etc.)
(All I have is a German excel and the function names got translated as well, so my function names may be off. Maybe someone can check against an English excel and update if necessary.)