Paste value based on index match - excel

I'm attempting to get a macro(button click) to paste a value into another sheet if the value is not a formula. I'm not getting any errors with the below but the function won't fire. Thank you in advance for your help!
Sub Update_Numbers(ByVal Target As Range)
' Update_Numbers Macro
'
' Keyboard Shortcut: Ctrl+j
On Error GoTo Safe_Exit
Application.EnableEvents = False
Select Case Target.Address(0, 0)
Case Sheets("Wage Run").Range("B7")
If Target.HasFormula = False Then _
Sheets("wage run").Range("B7").Copy
Sheets("With Changes").Select
Range(Application.WorksheetFunction.Index(Sheets("With Changes").Range("E2:AB138"), Application.WorksheetFunction.Match(Sheets("Wage Run").Range("D1"), Sheets("With Changes").Range("A2:A138"), 0), 1)).Select
ActiveSheet.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Select
Safe_Exit:
Application.EnableEvents = True
End Sub
EDIT: Changed with the below suggestions, but it still won't fire.
Sub Update_Numbers(ByVal Target As Range)
With Sheets("wage run")
' Update_Numbers Macro
'
' Keyboard Shortcut: Ctrl+j
On Error GoTo Safe_Exit
Application.EnableEvents = False
Select Case Target.Address(0, 0)
Case "B7"
If Target.HasFormula = False Then
Sheets("wage run").Range("B7").Copy
Sheets("With Changes").Select
Range(Application.WorksheetFunction.Index(Sheets("With Changes").Range("E2:AB138"), Application.WorksheetFunction.Match(Sheets("Wage Run").Range("D1"), Sheets("With Changes").Range("A2:A138"), 0), 1)).Select
ActiveSheet.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
End Select
Safe_Exit:
Application.EnableEvents = True
End Sub

In addition to #Scott Craner's comment above: Note that the "If-Then" construct doesn't need any corresponding "End If" when you have only one statement on one line following "Then". But when the If construct contains more than one statement, as in your case, the code block must end with "End If". You have a line continuation symbol ( _) immediately after "Then". Try removing this and include an End If statement instead, like this:
Select Case Target.Address(0, 0)
Case Sheets("Wage Run").Range("B7")
If Target.HasFormula = False Then
Sheets("wage run").Range("B7").Copy
Sheets("With Changes").Select
Range(Application.WorksheetFunction.Index(Sheets("With Changes").Range("E2:AB138"), Application.WorksheetFunction.Match(Sheets("Wage Run").Range("D1"), Sheets("With Changes").Range("A2:A138"), 0), 1)).Select
ActiveSheet.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
End Select

In addition to my comments above, here is a very minimal example that seems to do what you are asking for, but without any other context. It works on a workbook with (at least) two sheets. Maybe useful as a starting point.
Sub Macro1()
'
' Macro1 Macro
'
' Paste a value into another sheet if the value is not a formula.
Dim sourcecell As Range
Dim targetcell As Range
Set sourcecell = Sheets(1).Range("D8")
Set targetcell = Sheets(2).Range("D8")
If Not sourcecell.HasFormula Then targetcell = sourcecell
End Sub

Related

Hot to solve: VBA Copy Values only 1004 Error?

I have an excel protected sheet that is being used by several users and I've found that they sometimes Copy&Paste info from a different application that Blocks the pasted cells (while changing other formats as well).
The solution that I've found is using a macro in ThisWorkbook that forces the Copy as Value.
As I don't have that much experience in VBA, I've decided to use some available code in the internet.
The problem is that the marco works just fine in my Excel, but not in an older version of Excel (where I need it to work...).
I'm getting the 1004 Error. From what I've read so far I believe this happens because the info in the Clipboard dissappears when I run the macro.
Does anyone know how to solve this?
Below is the code that I'm using:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim UndoString As String, srce As Range
On Error GoTo err_handler
UndoString = Application.CommandBars("Standard").Controls("&Undo").List(1)
If Left(UndoString, 5) <> "Paste" And UndoString <> "Auto Fill" Then
Exit Sub
End If
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Undo
If UndoString = "Auto Fill" Then
Set srce = Selection
srce.Copy
Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.SendKeys "{ESC}"
Union(Target, srce).Select
Else
Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
err_handler:
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Try this, two lines have been inserted
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim UndoString As String, srce As Range
On Error GoTo err_handler
UndoString = Application.CommandBars("Standard").Controls("&Undo").List(1)
If Left(UndoString, 5) <> "Paste" And UndoString <> "Auto Fill" Then
Exit Sub
End If
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Undo
If UndoString = "Auto Fill" Then
Set srce = Selection
srce.Copy
Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.SendKeys "{ESC}"
Union(Target, srce).Select
Else
Selection.Copy 'This line has been inserted
Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False 'This line has been inserted
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
err_handler:
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
It looks as though that section of the code (Else etc.) only applies if they used Paste but not AutoFill.
I don't see the merit of using the If block to distinguish between UndoString = "Auto Fill" and any other result, which based on an earlier If block can only include a situation where the last undoable action name started with (or was) Paste. Don't you want to do the same thing to both Auto Fill and Paste?

2nd change(ByVal target As Range) doesnt work

I have a small puzzle. Im using cells "K6" and "L6" as imputs (for scaning the barcode). When I select cell "K6" and scan the barcode the cell "O6" is becoming from value "0" to value "1" (by formula "=IF(K6>0,1,0)").
Im using cell "O6" (when is grater then "0") as a triger for "Sub (ByVal target As Range)"
This Sub call marco "Nasklad" which copy and paste "K6" value to sheet2 (IN_OUT) column "A" first blank. After it call macro "Clear" which select sheet1 (Sklad) and clear cell "K6".
Whole process working fine, I scan a barcode, code is copied to another sheet and I can scan another code and so on.
Sub worksheet_change(ByVal target As Range)
Set target = Range("O6")
If target.Value > 0 Then
Call Nasklad
End If
End Sub
Sub Nasklad()
Sheets("Sklad").Select
Range("K6").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("IN_OUT").Select
ActiveSheet.Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Call clear
End Sub
Sub clear()
Sheets("Sklad").Select
Range("K6").Select
Selection.ClearContents
End Sub
Now the catch, when I tried duplicate serie of commands for cell "K6" to work with cell "L6", then nothing happedn.
It looks like "Sub worksheet_change2(ByVal target As Range)" is not working. When I imput something to "L6" value in "P6" become "1" (by formula "=IF(L6>0,1,0)" and this time trigger dont run the Sub and I dont know why.
Sub worksheet_change2(ByVal target As Range)
Set target = Range("P6")
If target.Value = 0 Then
Call Vysklad
End If
End Sub
Sub Vysklad()
Sheets("Sklad").Select
Range("L6").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("IN_OUT").Select
ActiveSheet.Range("B1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Call clear2
End Sub
Sub clear2()
Sheets("Sklad").Select
Range("L6").Select
Selection.ClearContents
End Sub
Did I miss something?
Thanks for helping
Thank you #BigBen and #urdearboy, this help me and I've changed the Sub worksheet_change(ByVal target As Range) to
Sub worksheet_change(ByVal target As Range)
Set target = Range("O6")
Set target2 = Range("P6")
If target.Value > 0 Then
Call Nasklad
End If
If target2.Value > 0 Then
Call Vysklad
End If
End Sub

Iserror not working

I've a problem with my code.
I'm trying to activate a code that take a cell in one sheet and filter the data in another pivot sheets, in case that the value dosen't exist there's a msgbox that show there's an error.
My problem is when the value is true I'd like it to show msgbox "the value dosen't exists in the pivot". when the "if" is false I need to filter the data but it dosen't work.
There's the code:
Sub MM()
Sheets("sheets1").Select
Selection.Copy
Sheets("pivot").Select
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").ClearAllFilters
ActiveSheet.PivotTables("pivottable1").PivotCache.Refresh
If Not IsError(ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").CurrentPage = Range("c1").Value) Then
MsgBox ("the value dosen't exists in the pivot")
Sheets("sheets1").Select
Else
ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").CurrentPage = Range("c1").Value
End If
End Sub
I'll be glad for some help!
Not totally sure if you wanted to filter the pivot according to what is in the selected cell, but here is my suggestion. To point out there is a way to filter pivot with many values but I supposed you wanted the filter to be done only for one value? Also the way to add filter to pivot is to loop trough all the field values and set them to visible or not visible.
Sub testi2()
'Bit waisty way to do it, you could just make a variable to hold the value -
Dim myValue As Variant
myValue = ActiveCell.Value
'Sheets("sheets1").Select
'Selection.Copy
Sheets("pivot").Select
'Range("C1").Select
'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Your choise tough, if you really need to copy the value to the cell C1 then by all
'means do, but you should still send the value to variable for code will be easier
'to be handled and clearer to read.
'Here you could also clear all past filters for the pivot if needed.
'I won't encourage to but if there are other filters present exept
'what is in filterWBS field, the code will run into an error.
Dim pItem As PivotItem
Dim ifFound As Boolean
ifFound = False
'loop trough the pivotfieldvalues to see if match exists, pivot tables have a need for at least one visible value.
For Each pItem In ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").PivotItems
'if data exists then ifFound value will be set to true
If pItem = myValue Then
ifFound = True
End If
Next
'based on the if value found set fields visible or hidden
If ifFound = True Then
For Each pItem In ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").PivotItems
If pItem <> myValue Then
pItem.Visible = False
Else
pItem.Visible = True
End If
Next
'if the value was not present show the message box
Else
MsgBox ("the value doesn't exists in the pivot")
'You could in this case clear the filter
End If
End Sub
I found the solution for my problem.
Sub MM()
Sheets("Sheets1").Select
Selection.Copy
Sheets("Pivot").Select
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").ClearAllFilters
ActiveSheet.PivotTables("pivottable1").PivotCache.Refresh
On Error GoTo msg
ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").CurrentPage = Range("c1").Value
Exit Sub
msg:
MsgBox ("There is no data for this WBS in pivot")
Sheets("sheets1").Select
End Sub

How do I select a cell referenced by a variable on VBA

This is what I have so far. I need a sub to copy a group of cells and paste their values on the next empty cell available. The error I'm getting is in selecting that first available cell. Any thoughts?
Dim workline As Integer
Sub Test()
With ActiveSheet
workline = 11
While .Cells(workline, 2) <> Empty
workline = workline + 1
Wend
End With
Range("B3:CH9").Select
Selection.Copy
range(workline,2) .Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Message = MsgBox("Data copied succesfully", vbInformation + vbOKOnly, "Aecon Mining")
End Sub`
Not tested in Excel, but should work, or at least pointing you to the right direction:
Range("B3:CH9").Copy
Range("B2").end(xlDown).offset(1,0).paste 'first available cell
And stop using those .Select and selection everywhere, they are a total waste of time.

Copy values from one worksheet to another

How can I copy some data from one worksheet to another?
I tried this code, but get an error:
Private Sub CommandButton2_Click()
Sheets("Gas Opt").Select
Range("A1:A3").Select
Selection.Copy
Sheets("ExportToPPServer").Select
Cells(3, AColumn).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
LFound = True
MsgBox "Data coped."
End Sub
Error:
Select method of Range class failed.
Something like this should work:
Private Sub CommandButton2_Click()
Dim copyRng As Range, targetRng As Range
Set copyRng = Worksheets("Gas Opt").Range("A1:A3")
Set targetRng = Worksheets("ExportToPPServer").Cells(3, AColumn)
copyRng.Copy
targetRng.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
LFound = True
MsgBox "Data coped."
End Sub
How does this look?
Sub x()
Sheets("Gas Opt").Select
Range("A1:A3").Select
Selection.Copy
ActiveWorkbook.Sheets("ExportToPPServer").Range("A1:A3").PasteSpecial Paste:=xlValues
End Sub
Edit
Is your Control button on a different sheet than "Gas Opt"? That would explain it. Try this:
Sub x()
Sheets("Sheet2").Range("A1:A3").Copy
ActiveWorkbook.Sheets("Sheet3").Range("A1:A3").PasteSpecial Paste:=xlValues
End Sub
You need to activate the sheet, else you cannot select cells in it.
Sheets("ExportToPPServer").Activate ' Instead of select

Resources