Here are snippets of two worksheets
This is the code that I have on the first sheet meant for change in selection of the dropdown. There will be more Cases, but my issue is with the SUMIF. Anytime column B on Sheet2 matches the corresponding column A item on Sheet2, sum column C on Sheet2.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D1")) Is Nothing Then
Select Case Range("D1")
Case "2014-2015": Cells(2, "B") = WorksheetFunction.SumIf(Worksheets("2014-2015").Range("B2:B22"), A2, Worksheets("2014-2015").Range("C2:C22"))
Case Else: Cells(2, "B") = 8
End Select
End If
End Sub
The problem is that it is always returning 0. Hoping for help as to an edit to the code to make it display the proper total.
Thanks,
It looks like the problem is with the value it is trying to match, specified only as A2 in your Sumif formula. It isn't recognizing that you want to match the value in cell A2. It would need to be referenced with something similar to Worksheets("SummarySheet").Range("A2").Value like you did for the other parameters of that function.
Related
I am trying to highlight the cell of a second entry (and beyond) within a range of cells. I am tyring to implement this feature in my attendence tracking.
For example, in the image below, I have "Peter", "John" and "Tom". If there are only one person who is absent, I do not need to highlight anything, as shown below.
However, if either "Tom" or "John" is the second person to be entered as "Not in" (absent), I want to highlight the cells, as shown below.
Finally, if the last person left is also absent, I would like to highlight it as well, like below image
The values in Column C is not restricted to these phrases ("Absent","Away","Not in").
From another point of view, I want to highlight the second and the later entries within a range of cells. Is there a way to implement such feature without using VBA or macro? Can it be donne using conditional formating?
Please advise. Thank you so much!
You can use the Worksheet_Change event to add a timestamp to a helper column. In my case, I added it to column D:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myrange As Range
Set myrange = Range("C3:C9")
If Not Application.Intersect(myrange, Range(Target.Address)) Is Nothing And Target.Count = 1 Then
If Target.Value = "" Then
Target.Offset(0, 1).Value = ""
Else
Target.Offset(0, 1).Value = Now()
End If
End If
End Sub
Then use conditional formatting on column C using something like this:
=$D3>=SMALL($D$3:$D$9,2)
This will highlight any rows where the the timestamp is greater than the second smallest timestamp (ignoring blanks).
Here's an example of it in action. For illustration purposes, I put the order I typed things in column C rather than "Absent", "Not Here", etc.
When I copy cells from one column only the pasted cells always have #N/A rather than the text they should have.
This happens when I either;
Copy a range of cells including any in this one affected column (Column A)
Copy a range of cells from only in the affected column
Delete a row (all values on the next row for the entire length of the row then have #N/A
Copy more than one cell from not in the affected column and paste any of them into the affected column
If I however copy a single cell from the affected column is pastes as it should. If I copy multiple cells from the affected column and paste them elsewhere in the sheet they paste OK.
I have zero idea how to solve this and searching on google did not help find an answer.
The cells in the affected column (Column A) are either standard text or are cells with a drop down list data validation type; None have formulas.
EDIT -
I have narrowed it down to the following code. The code is to change the value the user selects in the drop down list to something else.
Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice 20161026
selectedNa = Target.Value
If Target.Column = 1 Then
selectedNum = Application.VLookup(selectedNa,
Worksheets("refList_Hidden").Range("table_dropDown"), 2, False)
If Not IsError(selectedNum) Then
Target.Value = selectedNum
End If
End If
End Sub
I'm pretty sure the answer is to check if the range contains only one cell, if it does then run the VLOOKUP, if it contains more than one cell then then do not run the above code. Does anyone know how to do this?
As further explanation to #SolarMikes comment -
If range A1:A3 contains A, B and C respectively this formula in B1:
=MATCH("A",A1:A3,0) will return 1.
If you then copy cell B1 (including the formula) to cell C1 the formula will update to:
=MATCH("A",B1:B3,0) - there are no matches in that range so it returns #N/A.
To work around this I'd suggest using absolute cell referencing.
That is change A1:A3 to $A$1:$A$3, or as Mike suggested, copy and paste values.
In the code above i just needed to ensure there was only one cell in the range rather than lots;
Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice 20161026
selectedNa = Target.Value
If Target.Column = 1 And Target.Rows.Count = 1 And Target.Columns.Count = 1 Then
selectedNum = Application.VLookup(selectedNa, Worksheets("refList_Hidden").Range("table_dropDown"), 2, False)
If Not IsError(selectedNum) Then
Target.Value = selectedNum
End If
End If
End Sub
I currently have the following in excel for other users.
The Number column does not change. Users will input values in Multiplier 1 and Multiplier 2 columns and they will get a value in Outcome column.
Is there an automatically way where the user can also input a value in Outcome column and the Multiplier 1 column will change?
Currently Number and Outcome column are protected, will I need to unprotect the Outcome column?
I recommend you use some VBA code using the Sub Worksheet_Change(ByVal Target as range) in the worksheet.
If the user enters a value in D2 it will put the formula "=D2/(A2*C2)" in B2
If the user enters a value in B2 it will put the formula "=A2*B2*C2" in D2
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Cells.Count > 1 Then Exit Sub
On Error GoTo LastLine
If Not Intersect(Target, Range("D2")) Is Nothing Then
Range("B2").Formula = "=D2/(A2*C2)"
ElseIf Not Intersect(Target, Range("B2")) Is Nothing Then
Range("D2").Formula = "=A2*B2*C2"
End If
LastLine:
Application.EnableEvents = True
End Sub
This can be done. Begin by noticing that your formula is so simple that you
Can easily solve for multiplier1 as a function of:
outcome divided by (number times multiplier2)
Next get tricky by adding a 1 pixel wide column that will contain your formula
for outcome to the right of (now an input not a formula) the original
outcome column.
Similarly add a 1 pixel wide col to the right of multiplier1 that contains
your new formula for multiplier1. Adjust data validation on original input
cells for multiplier1 and outcome to allow only one of the two to be
entered at a time (use isblank function appropriately in an excel custom data
validation rule for each cell).
Finally right align the two 1 pixel wide cells so that their calculated values
appear within the corresponding enterable cells when they are blank.
Yes it's tricky but it works.
Sorry this is just an outline but I have used this in an application and it
works great. No vba, so undo redo don't break. Try it you'll like it.
Looking for a formula for specific range of values to a reference range on another sheet...
i.e. if cell A1 sheet 1 contains a specific value, then i would like it to find that value on column A of sheet 2 and result with equivalent row value of column B
I have a range on sheet 2 showing temperatures in column A with equivalent adjustment values in column B to go with each particular temperature.
I would like a formula that automatically fills in the temperature adjustment value into cell K23 on sheet 1 when I input the temperature into cell J22 (to be more specific)
Thanks for any help in advance!!!!
Here is some 'bare bones' code with some main concepts for you to start work with.
You will notice that you have received a number of downvotes for your Q. This is likely becase the spirit of this site is for you to demonstrate that you have made some attempt and/or research to 'help yourself' before posting your Q. For this reason I have not included any explanation.
Private Sub Worksheet_Change(ByVal Target As Range)
If Application.Intersect(Target, Range("J22")) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
adj = Application.VLookup(Target.Value, Sheets("Sheet2").Range("A1:B10"), 2, False)
If IsError(adj) Then
Range("K23") = 0
Else
Range("K23") = adj
End If
End Sub
So, in Sheet1 I have base of some names and it looks like this:
In Sheet2 I'm working with these names from Sheet1. I'm doing that in a way that I'm entering Code value in column A and in column B I get the Name, in column C I get the Last Name. That looks like this:
I've done this with formulas, entering it in the formula bar. For column A(or Name) I've used this formula: =IFERROR(VLOOKUP(A2;Sheet1!A:C;2;FALSE);"") and for column B(or Last Name) I've used this one: =IFERROR(VLOOKUP(A2;Sheet1!A:C;3;FALSE);""). I've dragged these formulas to row 20 and it works great.
Now, what I'd like to do is to put these formulas into Excel VBA code and them to work for noted range. I've just started to use VBA and I don't know how to do it in it, tried something but doesn't work, ..., I've done this so far. I'm new to this Excel/Macro/VBA thing so any help would be appreciated.
The below code will work if you type in your Code values in sheet2 and highlight them, and run this macro:
Selection.Offset(0, 1).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],Sheet1!C[-1]:C,2,FALSE),"""")"
Selection.Offset(0, 2).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-2],Sheet1!C[-2]:C,3,FALSE),"""")"
Selection.Offset(0, 1).Value = Selection.Offset(0, 1).Value
Selection.Offset(0, 2).Value = Selection.Offset(0, 2).Value
Edit: If you are wanting to update values as you type use (thank you #PeterAlbert for added optimisation!):
Private Sub Worksheet_Change(ByVal Target As Range)
'end if the user made a change to more than one cell at once?
If Target.Count > 1 Then End
'stop system activating worksheet_change event while changing the sheet
Application.EnableEvents = False
'continue if column 1(A) was updated
'and
'dont continue if header or row 1 was changed
If Target.Column = 1 And Target.Row <> 1 Then
With Target.Offset(0, 1) 'alter the next cell, current column +1 (column B)
'RC1 = current row and column 1(A) e.g. if A2 was edited, RC1 = $B2
'C1:C2 = $A:$B
.FormulaR1C1 = "=IFERROR(VLOOKUP(RC1,Sheet1!C1:C2,2,FALSE),"""")"
.Value = .Value 'store value
End With
With Target.Offset(0, 2) 'alter the next cell, current column +2 (column C)
'C1:C3 = $A:$C
.FormulaR1C1 = "=IFERROR(VLOOKUP(RC1,Sheet1!C1:C3,3,FALSE),"""")"
.Value = .Value 'store value
End With
End If
Application.EnableEvents = True 'reset system events
End Sub
Explinatioin of RC:
The FormulaR1C1 formula types are good to use when referencing a cell with respect to the current cell. There a few rules to remember:
The R stands for Row and C is for Column and the integer after it, if any, defines the row or column;
As a basis the RC formula references itself;
Any number following the R or C wraped in [] is an offset to itself, e.g. if you are in cell A1 and use R[1]C[1] you would be referencing cell B2;
Also any number following the R and C is an exact, e.g. if you reference R2C2 no matter the cell you are in would also point to B2; and
To complicate things if you were in cell C5, e.g. using Range("C5").FormulaR1C1 = and coded the follwing:
"=RC[-1]" references cell B5
"=RC1" references cell A5, more rightly $A5
"=R[1]C[-2]" references cell A6
"=Sum(C[-1]:C5)" is =Sum(B:E), more rightly =Sum(B:$E)
If I understand your question and comments correctly, you want to ensure that columns B&C always show you the right values based on your formula, but also want to protect (and maybe even hide the formula) from the users.
I'd suggest you use sheet protection instead: all you need to do is to unlock the cells you want the users to edit, i.e. select column A and in the _ Format cells_ dialog uncheck "Locked" in the Protection tab. Similarly for columns B&C, check "Hidden". Now right click the sheet name and select Protect Sheet. Once this is done, the user can edit column A - but will not see the formula in B&C and cannot edit those cells.
If for some reasons you need to ensure this in VBA, use the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False 'to prevent endless loop
With Target.Offset(, 2 - Target.Column).Resize(, 2)
.FormulaR1C1 = "=IFERROR(VLOOKUP(RC1,Sheet1!C1:C3,COLUMN(RC),0),"""")"
.Value = .Value
End With
Application.EnableEvents = True
End Sub
You need to place this in the module of the worksheet.