Excel check if same value exist across all sheets [duplicate] - vba

This question already has answers here:
Excel - Using COUNTIF/COUNTIFS across multiple sheets/same column
(4 answers)
Closed 7 years ago.
I have an excel file which has 82 sheets in it, all sheets have the same title in column A but on Column B has different status, think of it as a task name on column A and its status on Column B.
I want to check if the status of task on Column A5 is same across all sheets or how many times the status is critical and how many times status is ok
If I was using a single sheet I know the following formula would give how many tasks have critical status on the same sheet
=COUNTIF(B5:B24,"critical")
But I want to check how many times B5 has status critical across all sheets, how can i do this?
Any assistance will be really appreciated.

This code iterates through all the sheets, and creates a formula that sums up all the countifs
Then, it stores the formula in A1
s = ""
For i = 1 To Sheets.Count
s = s & "+COUNTIF(" & Sheets(i).Name & "!B5:B24,""critical"")"
Next
range("a1")=s

Related

How to match multiple columns and get values in Excel [duplicate]

This question already has answers here:
Vlookup using 2 columns to reference another
(2 answers)
Closed 5 years ago.
I need to match Column A and Column B values in Sheet 1 with Column A and Column B values in Sheet 2. If both are same then Copy C values from Sheet 2, and paste in Sheet 1 in C. I will enter values manually in sheet 2 Column C.Here each country will have 2 or more Number. So, both Column A and Column B must match.
I used the formula below. But not working. Most of the Column A and B values are not in order. Help me
=INDEX(Sheet2!$C:$C; MATCH(Sheet1!$A2:B2; Sheet2!$A:$B; 0);COLUMNS($A:B))
You can use the following formula to return what you're looking for. It is an array formula so will need to be entered with Ctrl+Shift+Enter
=INDEX(Sheet2!$C$2:$C$22; MATCH(1; (Sheet2!$A$2:$A$22=Sheet1!$A2)*(Sheet2!$B$2:$B$22=Sheet1!$B2);0))
As it is an array formula I recommend defining your ranges from beginning to end instead of just selecting the whole column. Non-array formulas Excel actively finds the beginning and end of the range and only calculates that subset; however, with array formulas it considers the whole range (even if there's nothing in it) so it can suddenly take a very long time even when there isn't much being calculated
Use the following as a matrix formula:
=INDEX(Sheet2!$C:$C; MATCH(Sheet1!$A2&$B2; Sheet2!$A&$B; 0))
Paste this into cell C2:
=INDEX(C2:C22,MATCH(G2&H2,A2:A22&B2:B22,0))
and use Ctrl+Shift+Enter instead of Enter since it's an array formula.
Then copy that cell as many rows down as needed.
Here are some more examples.

How to get last column used in excel using vba [duplicate]

This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 5 years ago.
Hi I am having issues getting to the last column used in my sheet.
I do know that as it stands my last column is 43 and i want it to enter a value at column 44, so then when i search again it will go to column 44 as last column used and then enter a value at 45 and so on and so on. I am using a ref num to find the correct row.
The code I am using is as follows to try get the last column
'find last empty Col in database
LastCol = sou.Cells(2, Columns.count).End(xlToLeft).Column + 1
MsgBox LastCol
'For Loop to cycle through the columns
For x = 2 To LastCol
'If the row value at column 1 = the ref number then continue
If sou.Cells(x, 1).Value = refNum Then
sou.Cells(x, LastCol).Value = Me.commentBox.Text
Exit For
End If
Next
The issue is that for some reason it only goes to column 22. I have tried to use the other ways to get the last column but they have just given me the last column in the whole entire sheet which again is not what I want.
If someone can lend me a hand as I'm a newbie to this it would be greatly appreciated!
To find the last row used, have you tried
lastRow = ActiveSheet.UsedRange.Rows.Count
If that didn't work, please elaborate what went wrong and I can submit more code.

Large Data set Macro query & alternative to Vlookup request

This query is two fold. Any assistance would be much appreciated and unfortunately there is a lot of detail behind this so apologies for the great deal of text.
I am working on updating a 12 month trending report that pulls data from one sheet using a series of complex vlookups based on several criteria (Project, unique project id, Order, yr-month etc). Yes it would likely be easier to use pivot tables, but unfortunately because of the way this document is used the people with access to it want it to remain as is, from a format and function perspective but want the document sped up (takes roughly 5 minutes to calculate when ever something is changed.)
The data set is 36 columns by 50000 rows with 7 columns containing identifiers used to pull in specific data on the trending sheet
As it current sits on a monthly basis new data (approximately 6000 rows) of additional data is added.
I've added two macros to the document in an attempt to reduce user error
I am currently using the following code to take the order number remove the duplicates and then apply an index/match to pull in the relevant Project and then use countif to apply a unique key to the end of the project (using concatenate)
Sub ProjTrending1()
Dim s1 As Worksheet, s2 As Worksheet
'Defines S1 as a Worksheet
Set s1 = Sheets("All Data")
'Defines S2 as WorkSheet
Set s2 = Sheets("Workings")
'Defines LastR1
Dim LastR1 As Long, DataRange1 As Range
'Finds last row cell working sheet
LastR1 = s2.Range("A1").CurrentRegion.Rows.Count
'Takes Data from Order Column of defined data Sheet and copy & pastes it to Working Sheet Column B
s1.Range("J1:J" & LastR2).Copy s2.Range("A1")
'Removes Duplicates from Column B Working sheet
s2.Range("A2:A" & LastR1).RemoveDuplicates Columns:=2, Header:=xlNo
'Copies the formula from B2 and applies it to all cells in column B where column A has values
s2.Range("B2").Copy s2.Range("B2:B" & LastR1)
'Copies the formula from C2 and applies it to all cells in column C where column A has values
s2.Range("C2").Copy s2.Range("C2:C" & LastR1)
End Sub
The formula is column B is as follows
=INDEX('All Data'!E:E,MATCH(Workings!A2,'All Data'!J:J,0))
The formula in column C is as follows
=B2&COUNTIFS($B$2:B2,B2)
The data set in worksheet All Data is approx 50k rows long and will continue to grow. The issue I'm not running into is that this macro is starting to take a great deal of time to run because the information in column C is used as a unique identifier on the trending report.
Is this because I have a vast number of Vlookups on the trending tab (approx 20k) pulling data based on this or is there an issue with the macro I have created to create a unique identifier?
I've been looking at alternative ways in which I could draw the data through to the trending tab (dictionaries etc) with no luck so far. It would be much appreciated if someone could offer an alternative to 20k vlookups.

Excel Macro that reads the number of rows in another spreadsheet and fills down formulas

So I have two spreadsheets. One of them contains a lot of data with each column titled a few times, so the actual data for each column ends up starting in row 4 and on. In my other sheet (Note: they are in the same excel file though), I have some of the columns titles, a formula in row 3, and then the output of my formulas from rows 4 and on. Essentially, I have made formulas that check certain columns and the information in them for each row that there is data. What I want to do is have a button macro that looks at how many rows of data there is in that first spreadsheet starting at row 4, and then automatically have my formulas in the second sheet fill down to that certain amount of rows.
I have attempted to write the macro, but I end up getting a run-time error. Here is the code:
Range("A4:S4").AutoFill Destination:=Range("A4:S" & Cells(Row.Count, "Core Data2!A").End(xlUp).Row)
A-S are the amount of columns I have in my formula sheet. Core Data 2 is the spreadsheet with all the data
If someone could please show me how to get this to work, that would be greatly appreciated.
Use the following code for each of the columns A to S:
Range("A4:A" & Sheets("Core Data2").Range("A" & Rows.Count).End(xlUp).Row).Formula = "=[INSERT FORMULA HERE]"
In this example, A is the column that will be 'autofilled' up to the amount of rows populated with data in column A of the 'Core Data2' sheet.

Copy columns from one Excel sheet to another, skipping columns in between [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Copy column from one Excel sheet to another Excel sheet
I have 5 sheets in my Excel file. Out of these 5, one is the master sheet which has all the data I want to copy to the other 4 sheets.
All the sheets have one common column, ID. The master has more than 10000 IDs.
What I want to do is:
If ID in sheet1 = 24356 = ID in master sheet then copy x, y, z columns from master sheet to sheet1. This is same for all other sheets.
Also, since the master sheet is from another source than the rest of the sheets, its formatting is different. Is there a way to remove all formatting in sheets before running copy/paste?
Can anyone please tell me the VBA code to do this.
This is what my Master sheet looks like:
I want the other sheet (e.g. sheet1) in this case to look like:
Also, while searching the master sheet, is it possible for the code to look through the last column of the sheet?
Nupur
Here is the non-VBA way.
Assume your master data is on a sheet called Master and the data is in range A1:H9. Now assume I have Sheet1 with a list of IDs in range A2:A5.
1 ID Name Type Question1 Type3 Type4
2 475
3 479
4 501
5 503
Then in Sheet1 in cell B2 I have the following to retrieve Name for ID=475:
=VLOOKUP($A2,Master!$A$2:$H$9,2,0) //returns f1
To get Type for ID=475 I use:
=VLOOKUP($A2,Master!$A$2:$H$9,3,0) //returns adm1
All I am doing is setting up a reference to the data table on Master (note absolute ref with $ signs), using ID as the lookup value, and then changing the column value (3rd input in VLOOKUP) to retreive the relevant value.
Once you have set this up for the first row you can then just copy down and get the right data populated.
Does this solve the issue?