Check and increment the count on another sheet based on the data in this sheet - vba

I have two sheets in a workbook. I want to find the input given in one sheet and increment the count in another sheet. For eg, I am having nearly 25 questions and the answers to the questions would be Yes / No / n/a. I have kept a drop down input selector for these three options. After the 25 questions have been answered with either of these three options, at the click of submit button, I want the count of number of yes, no and N/As. For eg. 9 N/As, 10 Yes and 6 No.
The point is, I am planning to use the Sheet 1 as a form and Sheet 2 as the accumulation of results. So it needs to check the previous counts of Yes/No/N/As and increment the count.
The code I have tried at this point is as follows,
Sub Button3_Click()
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set sht1 = ThisWorkbook.Sheets("Table 1")
Set sht2 = ThisWorkbook.Sheets("Sheet1")
sht2.Range("A2") = sht1.Range("D8")
sht2.Range("B2") = sht1.Range("D9")
sht2.Range("C2") = sht1.Range("D10")
End Sub
This macro is assigned to the submit button and the macro copies the data(Yes,No,N/As) to another sheet. I want to find the count on A2,B2,C2 and increment it with number of Yes,No and N/As.
Can anybody help me in doing this?

Assuming you have two sheets in your workbook labeled "Table 1" and "Sheet1".
Assuming "Table 1" cell D8 contains the number of Yes's (probably computed by a COUNTIF or COUNTIFS), and similarly D9 contains Number of Nos and and D10 contains the number of N/As.
Your code works, but you seem to mention that you want to increment.
If so, you probably want
sht2.Range("A2") = sht2.Range("A2") + sht1.Range("D8")
sht2.Range("B2") = sht2.Range("B2") + sht1.Range("D9")
sht2.Range("C2") = sht2.Range("C2") + sht1.Range("D10")
If all you want is the total numbers of Yes, No, N/A in the 25 questions, then there is no need for a macro. Assuming that the answers to the 25 questions are in cells D8 to D32, on the other sheet you would write:
Cell A1: Yes
Cell A2: No
Cell A3: N/A
Cell B1: =COUNTIFS('Table 1'!$D$8:$D$32,A1)
You could now copy the formula from B1 to B2 and B3, and they would all work.

Related

MAX function : Display name of sheet where the answer is

I have several sheets in my Excel file, all named according to a day of the month (So from 1 to 31). I also have another sheet named "totals". In this sheet I have a MAX function that returns the highest number in cell Q31 from ALL sheets. In short, I ask to Excel: What is the highest number among the 31 "Q31" cells. Excel gives me the answer, for example, 54.
I would rather have Excel give me the name of the sheet where it finds the answer.
Is it possible?
Thanks!
In A1 through A31 enter:
=INDIRECT(ROW() & "!Q31")
then in B1 enter:
=MATCH(MAX(A:A),A:A,0)
(assumes the sheets are "named" 1,2,3,... and the worksheet name corresponds to the row number in column A)

Overwrite row data in one sheet with data from a second sheet satisfying 4 conditions

I've looked at several threads and there are some that touch on my problem, however, I've never used VBA and haven't a clue how to change the coding to suit my problem.
I would like to overwrite rows of data on sheet 2 from sheet 1, providing the data in columns A, B, C & D (live data starting row 2) are a match on both sheets 1 & 2.
Essentially sheet 2 is my data store, and sheet 1 is a template of sheet 2. All the possible combinations of data in the first four columns already exist in sheet 2 with the remaining data in the row unknown. So when I get that unknown data, I would like to overwrite that row in sheet 2.
A lot of people have made threads about copying rows over where one specific term is searched for in a column, whereas, I will have many different terms to search for, but as I said, they will need to be a match on both sheets.
Hope I've made sense! Please help!
You may be able to do this with a formula rather than using VBA.
Paste this formula into cell E1 of sheet1 if you have no headers:
=IF(AND(A1=INDEX(Sheet2!A:A,MATCH(A1,Sheet2!A:A,FALSE)),B1=INDEX(Sheet2!B:B,MATCH(B1,Sheet2!B:B,FALSE)), C1=INDEX(Sheet2!C:C,MATCH(C1,Sheet2!C:C,FALSE)),D1=INDEX(Sheet2!D:D,MATCH(D1,Sheet2!D:D,FALSE))),INDEX(Sheet2!E:E,MATCH(A1,Sheet2!A:A,FALSE)),"NO")
Or this one into E2 if you have a header row:
=IF(AND(A2=INDEX(Sheet2!A:A,MATCH(A2,Sheet2!A:A,FALSE)),B2=INDEX(Sheet2!B:B,MATCH(B2,Sheet2!B:B,FALSE)), C2=INDEX(Sheet2!C:C,MATCH(C2,Sheet2!C:C,FALSE)),D2=INDEX(Sheet2!D:D,MATCH(D2,Sheet2!D:D,FALSE))),INDEX(Sheet2!E:E,MATCH(A2,Sheet2!A:A,FALSE)),"NO")
Then drag that across using the little toggle on the bottom right of the cell as far across sheet1 as you want the columns to come in from your sheet2.
Then highlight the whole row you just created and drag the little toggle at the bottom right as far down the sheet as you have data (or try double clicking the toggle to auto fill down).
I tried this on a small set of data and it seems to work so it should work for your larger data set as long as all possible variations of the 4 columns on sheet1 are available on sheet2 with associated data in the following columns.
If you get a result of "NO" in any cell then Excel can't find a row in sheet2 which has the exact combination matching the one on sheet1.
EDIT - UPDATED ANSWER BELOW.
Try this, which is much more likely to work for you.
Sub CopyItOver()
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
For Each c1 In sh1.Range("A1", sh1.Range("A1").End(xlDown))
For Each c2 In sh2.Range("A1", sh2.Range("A1").End(xlDown))
If c2.Value = c1.Value Then
If c2.Offset(0, 1).Value = c1.Offset(0, 1).Value Then
If c2.Offset(0, 2).Value = c1.Offset(0, 2).Value Then
If c2.Offset(0, 3).Value = c1.Offset(0, 3).Value Then
c1.EntireRow.Value = c2.EntireRow.Value
End If
End If
End If
End If
Next c2
Next c1
End Sub

Using VBA in Excel to create a macro to fetch data between worksheets

I have two sets of data that need to be collated, the first has a list of questions (workbook 1), the second Workbook where those same questions are spread out over 5 sheets with ID numbers associated with them. I need a macro that will take a value from workbook 1, search for it in the entire workbook 2 and then locate its ID value and then copy this value into the first workbook.
I've tried numerous codes but I can't get anything near working, the closest I came was using a index, match function but that only does one column, whereas i need approximately 15 columns (I.e the whole worksheet)
Any help would be great!
I'm assuming your Excel workbooks are structured something like the following (based on your description).
Workbook 1
Sheet 1
A1 "What is the answer to life and all its questions?"
A2 "Who is the creator of Linux?"
A3 "What is your favorite text editor?"
Workbook 2
Sheet 1
A1 "What is the answer to life and all its questions?" B1 103401
A2 "Who is the creator of Linux" B2 104113
Sheet 2
A1 "What is your favorite text editor" B1 101031
We can take the index values from column B of workbook 2 (corresponding to our questions in column A of workbook 1) and copy these to column B of workbook 1. I'll provide a solution of how you could easily do this with cell formulas.
Workbook 1
Sheet 1
A1 "What is ..." B1 =Index([workbook2.xlsx]Sheet1!$B$1:$B$100$,
Match(A1, [workbook2.xlsx]Sheet1!$A$1:$A$100,0))
A2 "Who is ..." B2 =Index([workbook2.xlsx]Sheet1!$B$1:$B$100$,
Match(A2, [workbook2.xlsx]Sheet1!A$1:$A$100,0))
...
You could do the same thing in column C for Sheet 2 of workbook 2
Workbook 1
Sheet 1
A1 "What is ..." C1 =Index([workbook2.xlsx]Sheet2!$B$1:$B$100$,
Match(A1, [workbook2.xlsx]Sheet2!$A$1:$A$100,0))
A2 "Who is ..." C2 =Index([workbook2.xlsx]Sheet2!$B$1:$B$100$,
Match(A2, [workbook2.xlsx]Sheet2!A$1:$A$100,0))
...
Then one of the columns B-E would contain the ID number and the rest of the columns would contain N/A. Note that I only search the first 100 rows of the first 5 sheets of workbook 2.

How to tell excel automatically add value in a cell of a sheet when the value is added in another cell of another sheet? [duplicate]

This question already has an answer here:
How to make excel automatically fill a value in a cell of a sheet when another corresponding cell of another sheet is filled the same value?
(1 answer)
Closed 8 years ago.
I have cells A1 and A2 in sheet1 with values 3, 4 respectively. I link the cell A1 and A2 to B1 and B2 in sheet2 respectively. I mean when A1 and A2 change, B1 and B2 change accordingly.
This is what I want to manipulate my spearsheet: When I add value 5 in cell A3 in sheet1, value 5 is also added in cell B3 in sheet2.
Without filling =A3 in B3 or using autofill, how can I do so?
Imagining that each time when you make a report you have to add a lot new cells -- A4 to A100 --, that the number of cells is varied each time, and that you have a lot of corresponding sheets to sheet1 -- sheet2 to sheet100 -- you will find why I need your help.
The best solution to your problem is to select the sheets on which you want the values to appear in corresponding cells by holding down the control key and clicking on sheet tab. So when you make an entry on Sheet1 in any cell the same appears in the corresponding cell of another sheet (s) selected.

Macro to find data from cell and replace data on different sheet

I'm after a hopefully simple Macro/VBA code for my sheet i am using, i have searched far and wide but my VBA knowledge is quite limited so am not sure what the coding is doing in other answers.
Basically i have two sheets, Sheet2 has two columns, Column A has a number in it i.e. 2158 and column B has a name in it. What i want this macro to do is in Sheet1 i want to type in a number in Cell A1 and a name in cell B1 and then the macro finds that number used in Sheet1 Cell A1 in sheet 2 and then replaces the name from Sheet1 B1 with the correpsonding name in sheet2?
Hope that makes sense!!
Thanks
Daniel
Give this a try:
Sub dural()
Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
v1 = s1.Range("A1")
v2 = s1.Range("B1")
s2.Activate
For Each r In Intersect(ActiveSheet.UsedRange, Range("A:A"))
If r.Value = v1 Then
r.Offset(0, 1).Value = v2
End If
Next
End Sub
If you don't do anything with the name in sheet1!b1, you can pull the corresponding value from sheet2!b1 to sheet1!b1 using the VLOOKUP function.
In Sheet1 Cell B1, enter the formula below.
=VLOOKUP(A1,Sheet2!A:B,2,FALSE)
Then you can enter your number on sheet1!A1, the corresponding value from sheet2!b2 should display on sheet1!b1. If there's no matching found, you will get the #N/A.