I have a huge amount of people in my excel sheet and I want to split them by country with excel coding, here is an example of my data:
Country | Name
UK | Tom
Austria | Bobsky
UK | Ralf
Germany | Badolf
Germany | Schwartz
UK | Andy
So would it be possible to just separate the people who are in the UK into s different part of my spreadsheet?
I have already tried
INDEX(B1:B6, MATCH("UK", A1:A6,0)) - this returns a repeated row if the match function returns no result
I have also tried many things with
if(VLOOKUP(etc etc) = "UK".....
and I have found this doesn't work either. I thought this would be something excel could do simply without having to filter + copy & paste or use VBA but this is not easy.
This is doable for a couple thousand rows of data. If your 'huge amount of people' is much more than that, an advanced filter or pivot table is a more viable solution.
With UK in D3 use the following in E3.
=IFERROR(INDEX($B$2:$B$9999, SMALL(INDEX(ROW($1:$9995)+($A$2:$A$9996<>D3)*1E+99, , ), ROW(1:1))), "")
Fill down as necessary.
Without wasting time to draft a complex Excel formula:
Option 1
Use a PivotTable where you use the Country column as a filter
Option 2
Use a Microsoft Query Data->From Other Sources->Microsoft Query on each worksheet like this:
SELECT * FROM [Sheet1$] WHERE Country = 'UK' ORDER BY NAME
Sub processData()
'I have workout for only UK
'this macro copies records of UK people from Sheet1 to Sheet2
lastrows = Worksheets("Sheet1").Cells(Rows.count, 1).End(xlUp).Row
count = 1
For x = 2 To lastrows
If (Worksheets("Sheet1").Cells(x, 1) = "UK") Then
Worksheets("Sheet2").Cells(count, 1) = Worksheets("Sheet1").Cells(x, 1)
Worksheets("Sheet2").Cells(count, 2) = Worksheets("Sheet1").Cells(x, 2)
count = count + 1
End If
Next x
MsgBox "Task finished"
End Sub
Related
I have a table 1 of 500+ rows that are updated multiple times during a month by a request.
And there is a updated table of different formulas (200+) that should be calculated using values from table 1.
Table 1
year
product
value
1993
Apple
98.45
1994
Mushrooms
67.54
1992
Apple
95.45
2021
Melon
112.0
Table 2
id
formula
1
1994/1993*100-100
2
1994-1993
3
2021/1992*100-100
etc...
My way:
in table_1 I have created a key column - year.
I have modified the formula (below - a pic):
Code for table_2:
let
src = Excel.CurrentWorkbook(){[Name="table_2"]}[Content],
pq_auto1 = Table.PromoteHeaders(src, [PromoteAllScalars=true]),
pq_auto2 = Table.TransformColumnTypes(pq_auto1,{{"Id", Int64.Type}, {"formula", type text}}),
result = Table.AddColumn(pq_auto2,"value",each Expression.Evaluate([modified_formula],#shared))
in
result
Can you suggest me to find a better way to do this calculations by each row?
It is not possible to do that just like with Excel formula however BI Accountant showed elegant solution by shifting column by one row:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content]
,ShiftedList = {null} & List.RemoveLastN(Table.Column(Source, "ColumnToBeShifted"), 1)
,NewShiftedColumn = Table.FromColumns(Table.ToColumns(Source) & {ShiftedList}, Table.ColumnNames(Source) & {"NewColumnName"})
in
NewShiftedColumn
With this your calculation should be very easy.
I am working on a spreadsheet in which I have data of quarter-wise sales by co. and sector. My data is arranged in below mentioned format:
Row 1 has date
Data starts from row 2
Column B: Company Name
Column C: Sector Name of the co. for ex. energy, materials, technology etc
Column D: Sales figure of co.
Column E, F, G: Price, shares, volume of co.
Column H: Blank
From next column onwards I have same data fields for next quarter
Column I: Company Name
Column J: Sector Name of the co. for ex. energy, materials, technology etc
Column K: Sales figure of co
So on and so forth
Now, in another worksheet I need to get name and sales figures for top 3/5/10/15 etc (top n co.s depending on user input) within each sector. For ex. sales figures and co. name of top 3 companies in Energy sector.
I have been trying to write a vba code for this but I am struggling. I have mentioned below the code I was trying but its not flexible at all since in my code I have given reference of column C and D which would actually change after each quarter
Sub try()
Dim r As Long
n = Range("topcos").Value + 5
For r = 5 To n
p = 1
Worksheets("Top co. share with co name").Activate
Cells(r, 16).Select
Selection.FormulaArray = "=LARGE(IF('Co Wise'!$C$3:$C$600=P$3,'Co Wise'!$D$3:$D$600,""""),p)"
p = p + 1
Next r
End Sub
It seems that you merely forgot that p is a variable and should be treated as such in the formula:
Selection.FormulaArray = "=LARGE(IF('Co Wise'!$C$3:$C$600=P$3,'Co Wise'!$D$3:$D$600,"""")," & p & ")"
Alternatively:
Sub SO()
Sheets("Top co. share with co name").Range("P5:P" & [topcos] + 5).FormulaArray = _
"=LARGE(IF('Co Wise'!$C$3:$C$600=P$3,'Co Wise'!$D$3:$D$600,""""),ROW()-4)"
End Sub
Would do the same thing without the need for loops and variables.
Apologies for awkward title. I have searched through the forums but not yet come across a solution which is appropriate. I imagine what I am after will require the use of a macro.
I have a spreadsheet which outlines transactions by row, that is, one row for each transaction. Each transaction contains projected financial data. So, each row relates to one transaction which has financial impacts across multiple years.
For example, the header looks like
Transaction | field1 | field 2 | etc. | 2010-11 | 2011-12 | 2012-13 | etc. |
Beneath the years are dollar amounts.
All the fields contain data I need replicated across rows, except I want the year transposed into a single column, with the financials against it.
So if I have the record:
exampleid | data | data | 45000 | 56000 | 223145 | data
I would like it too become:
exampleid | data | data | 2010/11 | 45000 | data
exampleid | data | data | 2011/12 | 56000 | data
exampleid | data | data | 2012/13 | 223145 | data
There are around 30 columns.
You don't need VBA. You can use/adapt the formulas posted here.
That post was intended precisely as a reference for cases like this one.
For each of your input rows, you need two sets of formulas row-to-column, one for the headers and one for the data.
Use appropriate relative/absolute indexes in formulas, to be able to copy/paste.
Here's what I use to do this. You can change the variables at the start of the sub to dictate how many columns you have with data, how wide your pivoted data is, which sheet names are used for the import and the export, etc etc.
The exported data will always start at the same column that the original data started at.
The data needs to have headers to work correctly, and it assumes there are no blank cells in the first column of data.
Sub pivot_data()
data_sheet = "Sheet1" 'name of sheet containing original data
export_sheet = "Sheet2" 'name of sheet to export to
start_d_row = 2 'first row of data (headings are grabbed from row above)
start_d_col = 1 'first column of data
end_d_col = 7 'last column of data
start_t_col = 4 'first column to pivot
end_t_col = 6 'last column to pivot
xrow = 2 'first row to export DATA to (headings will be row above)
trailing_col = end_d_col - end_t_col 'calculated number of trailing columns
pivot_size = end_t_col - start_t_col + 1 'calculated size of pivot
'headings
For xcol = start_d_col To start_t_col - 1
Worksheets(export_sheet).Cells(xrow - 1, xcol).Formula = Worksheets(data_sheet).Cells(start_d_row - 1, xcol).Value
Next xcol
Worksheets(export_sheet).Cells(xrow - 1, xcol).Formula = "Narrative"
Worksheets(export_sheet).Cells(xrow - 1, xcol + 1).Formula = "Value"
For xcol = start_t_col + 2 To start_t_col + 2 + trailing_col - 1
Worksheets(export_sheet).Cells(xrow - 1, xcol).Formula = Worksheets(data_sheet).Cells(start_d_row - 1, xcol - 2 + pivot_size).Value
Next xcol
'data
For irow = 2 To Worksheets(data_sheet).Cells(start_d_row - 1, start_d_col).End(xlDown).Row
For icol = start_t_col To end_t_col
For xcol = start_d_col To start_t_col - 1
Worksheets(export_sheet).Cells(xrow, xcol).Formula = Worksheets(data_sheet).Cells(irow, xcol).Value
Next xcol
Worksheets(export_sheet).Cells(xrow, start_t_col).Formula = Worksheets(data_sheet).Cells(1, icol).Value
Worksheets(export_sheet).Cells(xrow, start_t_col + 1).Formula = Worksheets(data_sheet).Cells(irow, icol).Value
If end_d_col = end_t_col Then
Else
For xcol = start_t_col + 2 To start_t_col + 2 + trailing_col - 1
Worksheets(export_sheet).Cells(xrow, xcol).Formula = Worksheets(data_sheet).Cells(irow, xcol - 2 + pivot_size).Value
Next xcol
End If
xrow = xrow + 1
Next icol
Next irow
End Sub
Hope that helps
VBA can certainly be used to solve this problem but for regularly structured data such as this native Excel alone can also be used.
The basic problem is that you want to split each record in your "input" list into n records in your "output" list. So if you have i records in your input list you will have i*n records in the output.
I used to do a lot this sort of stuff on the UK Office for National Statistics population data, where the data was published in neatly formatted tables showing population levels by age group and location whereas I wanted it in a list format with each record showing just the population in a single location and age group combination. The list format was often then used either with Excel's data filtering and/or Pivot tables for analysis purposes and occasionally imported into Access.
The approach to transforming the data is as follows.
Assuming for example you have 25 years worth of data in your input table and say 200 input records, you want to generate 200*25=5000 output records so first generate a 2 column list of with 5000 rows. Start with 1,1 in the 1st row; 1,2 in 2nd row; 1,3 in 3rd row; ... ;1,25 in 25th row; 2,1 in 26th row; 2,2 in 27th row; ... ; 2,25 in 50th row; 3,1 in 51st row; and so on until you reach 200,25 in the 5000th row. Such a list is easy enough to generate: assuming cells A2 and B2
represent the first 1,1 (simply type these values into the two cells) then B3 has a formula such as
IF(B2=25,1,1+B2) which can be copied down the remaining rows of
column B. A3 also contains a formula based on the IF function, but this time
the first argument is based on the value in the adjacent cell (ie B3). The precise formula is left as an exercise and, again should be copied down the remaining rows of column A.
The 5000 rows correspond to your output records and each pair of values tells you which input record (first value) and which year within that record (second value) provides the data for the output record. You now have a choice of functions for using this information to create cells containing the output records. Possibilities include the INDEX(), OFFSET() and INDIRECT() functions though the latter will need to be used in conjunction with the ADDRESS() function.
INDEX() is probably most straightforward to understand and apply but the others are worth exploring and understanding. If your 200 input records, not including the header row, is held in say Sheet1!A2:AB201 (with cols A-C used for the non-year data and the 25 years of yearly data being in columns D-AB) and the output table is to appear on Sheet2 (with row 1 being a header row and columns A and B being used as described in step 1., above) then the formula for Sheet2!C2 would be =INDEX(Sheet1!A$2:A$201,$A2) and this formula can be copied both for all output rows and the next 2 (non-year) output columns (note the use of mixed absolute and relative addressing that achieves this). The year value for the 4th column of the output list (column F) is generated by picking out the correct value from the header row in Sheet1. The formula to use is =INDEX(Sheet1!$D$1:$AB$1,B2) in cell Sheet2!F2. Copy this formula down the remaining rows of column F. The formula for cell Sheet2!G2 uses the two dimensional version of the =INDEX() function - but I'll leave that one as an exercise. Once you've got the correct formula it should be copied down the remaining rows of column G.
If you have navigated the above successfully your output data should be in Sheet2!C2:G5001 and you can type your column headers into row 1.
One final comment on your terminology. You are not transposing your data, instead you are transforming the way it is presented. Transposing data usually means converting all the rows of a table into columns and vice-versa: so a table with I rows and J columns is transposed to become a table with J rows and I columns. In your case, the dollar values are transformed from I rows and J columns to I*J rows and 1 column.
I have my data coming back as a cell which looks like this (each trade is underneath each other in the cell ie its a 5 by 4 cell):
sell 50 FTSE 6500
buy 100 Eurostoxx 3300
buy 25 SP 1980
buy 30 FTSE 6490
sell 25 Eurostoxx 3315
Firstly because I have mixed data its coming back from my database query as a cell so not sure if thats a problem
What I'd like to do is mark these to market, so I have closing market price of each of these contracts as a variable lets call them FTSE_CLOSE, EUROSTOXX_CLOSE and SP_CLOSE.
I'd like to be able to do something that goes as follows:
where coloumn 3 = "FTSE" and column 1 = "sell" then column 5 = column2 * (column4 - FTSE_CLOSE)
where column 3 = "FTSE" and column 1 = "buy" then column 5 = column2 * (FTSE_CLOSE - column4)
and so on for the other contracts.
Basically I am struggling with the mixed use of strings and numbers
Do I need to cell2mat everything so I have several vectors and then somehow look into the string vectors and perform the calucaltion on the other relevant vectors i.e. quantity * price - close_price
Is there a simpler way of doing this
I think you are simply looking for the command str2num
Here is one idea:
input_data={"sell","50","FTSE","6500";
"buy","100","Eurostoxx","3300";
"buy","25","SP","1980",
"buy","30","FTSE","6490",
"sell","25","Eurostoxx","3315"};
closing_price.FTSE=6000;
closing_price.Eurostoxx=3000;
closing_price.SP=2000;
for i=1:size(input_data,1)
if strcmp(input_data{i,1},'sell')
diff_from_market(i)=str2num(input_data{i,2}) ...
*(str2num(input_data{i,4})-closing_price.(input_data{i,3}));
elseif strcmp(input_data{i,1},'buy')
diff_from_market(i)=str2num(input_data{i,2}) ...
*(closing_price.(input_data{i,3})-str2num(input_data{i,4}));
end
end
diff_from_market
i am using the following formula to index my data on sheet 2 and find the matching values which are similar or exact to that which is typed into my cell K22 on sheet 1.
I have used the below code and combined it together using & statements to give me multiple results from seperate columns.
=IF(ISERROR(INDEX(Sheet2!$A$1:$D$10004,SMALL(IF(LEFT(Sheet2!$A$1:$D$10004,5)=LEFT(Sheet1!$K$22,5),ROW($A$1:$D$10004)),ROW(1:1)),1)),"",INDEX(Sheet2!$A$1:$D$10004,SMALL(IF(LEFT(Sheet2!$A$1:$D$10004,5)=LEFT(Sheet1!$K$22,5),ROW($A$1:$D$10004)),ROW(1:1)),1))
so my finished combined code looks like this:
=`IF(ISERROR(INDEX(Sheet2!$A$1:$D$10004,SMALL(IF(LEFT(Sheet2!$A$1:$D$10004,5)=LEFT(Sheet1!$K$22,5),ROW($A$1:$D$10004)),ROW(1:1)),1)),"",INDEX(Sheet2!$A$1:$D$10004,SMALL(IF(LEFT(Sheet2!$A$1:$D$10004,5)=LEFT(Sheet1!$K$22,5),ROW($A$1:$D$10004)),ROW(1:1)),1)) & " - " & IF(ISERROR(INDEX(Sheet2!$A$1:$D$10004,SMALL(IF(RIGHT(Sheet2!$A$1:$D$10004,5)=RIGHT(Sheet1!$K$22,5),ROW($A$1:$D$10004)),ROW(1:1)),3)),"",INDEX(Sheet2!$A$1:$D$10004,SMALL(IF(RIGHT(Sheet2!$A$1:$D$10004,5)=RIGHT(Sheet1!$K$22,5),ROW($A$1:$D$10004)),ROW(1:1)),3)) & " - " & IF(ISERROR(INDEX(Sheet2!$A$1:$D$10004,SMALL(IF(LEFT(Sheet2!$A$1:$D$10004,5)=LEFT(Sheet1!$K$22,5),ROW($A$1:$D$10004)),ROW(1:1)),4)),"",INDEX(Sheet2!$A$1:$D$10004,SMALL(IF(LEFT(Sheet2!$A$1:$D$10004,5)=LEFT(Sheet1!$K$22,5),ROW($A$1:$D$10004)),ROW(1:1)),4))
so when i perform a search this displays the results like so
Supplier x - Manchester - V0001
i am using ROW() function because i have many of these formulas in seperate rows on my sheet to return multiple search results.
i am also using LEFT() function as a way of telling it to find similar results rather than exact, because someone may type 'Hotel' instead of 'Hotels' etc.
my data on sheet 2 looks like this:
Name Description Location Number
Supplier X Catering Manchester v0001
Supplier Y Hotels London v0002
Supplier Z Catering London v0003
so if i do a search for catering in my cell K22 on sheet 1 i would get a result like this:
Supplier X - Manchester - V0001
Supplier Z - London - V0003
But now if i want to just search for catering suppliers in London, i get the same search results again:
Supplier X - London - V0001
Supplier Z - London - V0003
however it changes the location of supplier x from manchester to london, even though in my column data on sheet 2, supplier x location is manchester?
Can someone please show me why this is happening as it should only be returning the suppliers which have a matching location of London next to them.
Thanks in advance,