Pasted cells contain #N/A - excel

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

Related

Change colour of selected cell using values in cells based off the selected cell

I have a macro to colour a cell based on the RGB values.
I have cells A1, B1, and C1 for the RGB values.
Sub FillWithRBG()
Range("D1").Interior.Color = RGB(Range("A1").Value, Range("B1").Value, Range("C1").Value)
End Sub
I can select anywhere in the worksheet, run the macro and only cell D1 will change colour.
I want to select cell D2, run the macro and Cell D2 changes colour based off values in A2, B2, and C2.
I imagine I need to set the active cell with a reference, then 1, 2 and 3 will be offset from the selected cell.
An added bonus would be that the macro can only run in the D column to prevent errors.
This would be a simple approach
In the code Me. is refering to the current sheet
You have to place this code in the sheet's module
Columns in VBA are noted by numbers (so column A is referenced as column 1)
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' Prevent change if changed values are no in these columns (1,2,3) numbers are equivalent to A, B, C
If Target.Column > 3 Then Exit Sub
' Change color of D column (4 = D)
Me.Cells(Target.Row, 4).Interior.Color = RGB(Me.Cells(Target.Row, 1).Value, Me.Cells(Target.Row, 2).Value, Me.Cells(Target.Row, 3).Value)
End Sub
If you want it to work only if you're changing one cell at a time add these lines:
' Prevent change when more than one cell is changed
If Target.Cells.Count > 1 Then Exit Sub

How to use VBA to copy a cell and paste into a different cell as values when another cell is changed

I have a table where column L will be changed by the user. Column M returns a value based on a vlookup off of column L. I am looking for some VBA code that will notice anytime any cell in column L is changed, then copy the result from the Vlookup in column M and paste as values in Column N.
For Example:
User changes/adds value into cell L3
Cell M3 is updated based off of a vlookup using L3
VBA copies the new value in M3
VBA Pastes as values into N3
Any help is greatly appreciated!
Use the Worksheet.Change Event with the Application.Intersect method to check if a specific cell was changed.
Use the Range.Offset property to move from the changed cell to another cell where you want to write your value.
Private Sub Worksheet_Change(ByVal Target as Range)
Dim ChangedCells As Range
Set ChangedCells = Intersect(Target, Target.Parent.Range("L:L"))
Dim Cell As Range
If Not ChangedCells Is Nothing Then
'ChangedCells contains all cells that changed and are in column L
For Each Cell In ChangedCells 'loop through cells
Cell.Offset(ColumnOffset:=1).Value = "aaa" 'offset moves from L to M
Next Cell
End If
End Sub

Automatically Copying Value & Format - Excel

Automatically Copying one/many cell's value & colour format to another cell/range of cells specified by myself. The other cell also needs to update every time the first cell changes its value or format.
I've been able to see just one or two threads with answers to this question but it still doesn't serve my purpose. Can any genius help me out in here? I don't quite understand why a basic thing like this has taken me 1 full day to figure out (the answer shouldn't be that I'm not smart :D)
Attaching an Excel example of what I want to achieve.
I want to be able to;
Update the Sheet 2 with the data from Sheet 1. (C4 in sheet 2 has to be green and have the value 5). Also, if C4 in Sheet 1 changes its value and colour, I want C4 in Sheet 2 to change automatically)
Now, the above is not only my concern. I have many cells in Sheet 1 which I want to be able to select and have them copied into Sheet 2 in respective places. (eg - c4, d4,e4 from sheet 1 ,.etc to be copied into c4, d4, e4 in sheet 2). Not necessarily I would want to choose the cells in a sequential fashion, but if there's a way for me to specify which cell needs to be copied into which cell of the other sheet, I'll be even more convinced.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ping As Boolean
If Intersect(Target, Range("A3")) Is Nothing Then
If ping = False Then
Range("A3").Copy
Range("C10").PasteSpecial Paste:=xlPasteFormats
End If
ping = True
Exit Sub
Else
ping = False
End If
End Sub
One solution can be with a user defined function. I could not try the code, but just to show the idea:
Public Function CopyFromTo(rngFrom As Range, rngTo As Range)
Application.Volatile True ' "A volatile function must be recalculated whenever calculation occurs in any cells on the worksheet"
rngFrom.Copy rngTo
CopyFromTo = rngFrom ' I am not sure what the function should return
End Function
For example, formula in cell B2 would be =CopyFromTo(A2, B2)

SUMIF returning zero

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.

Refer cell value to range of cells on another sheet

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

Resources