Copy and Paste value when it changes - excel

I am looking to write a code to :
copy cell A14 into the first row of column H,
move down the entire column (most recent data on top),
and then time stamp in column I when the value changes
But I cannot seem to get it running and working properly.
I am trying to keep real time tracking of these values and create a time series graph.
This needs to execute on its own.
Any thoughts?
Private Sub Gain(ByVal target As Range)
Application.EnableEvents = True
Do While cell("A14") <> cell("H1")
If cell("H1") <> cell("A14") Then
Range("H1:J1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A14").Select
Selection.Copy
Range("H2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A16").Select
Application.CutCopyMode = False
Selection.Copy
Range("I2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B16").Select
Application.CutCopyMode = False
Selection.Copy
Range("J2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("I:I").Select
Application.CutCopyMode = False
Selection.NumberFormat = "[$-F400]h:mm:ss AM/PM"
Columns("J:J").Select
Selection.NumberFormat = "m/d/yyyy"
Columns("H:H").Select
Selection.NumberFormat = "$#,##0.00"
Next
End Sub

This will track any changes and paste the current time in Column I. It looks like your copying and pasting code should be working; if not, please let us know what (if any) errors or results you are getting.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
rng = Range("A:A")
If Intersect(Target, rng) Is Nothing Then
'Do nothing
Else:
If Target.Value <> Now Then
Cells(Target.Row, 9).Value = Now
Cells(Target.Row, 9).NumberFormat = "hh:mm:ss"
Else
End If
End If
End Sub

Related

VBA Macro for Pasting Data In New Row of Table - Excel

I have recorded a macro that is attempting to copy information from cells outside of a table and paste them into a new row in a table on the same sheet. When trying to run the macro I receive "Run-time error '1004': PasteSpecial method of Range class failed." The issue seems to be with the first line stating:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I have a collection of paste special code in this module so I am afraid that this first line might not be the only issue. Below is the code I have so far.
Sub PlaceOrder()
'
' PlaceOrder Macro
'
'
Range("A3").Select
Selection.Copy
Range("Table1[[#Headers],[Balance]]").Select
Selection.End(xlDown).Select
Selection.ListObject.ListRows.Add AlwaysInsert:=False
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 1).Range("A1").Select
Range("B3").Select
Application.CutCopyMode = False
Selection.Copy
Range("B23").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 1).Range("A1").Select
Range("C3:E3").Select
Application.CutCopyMode = False
Selection.Copy
Range("C23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 3).Range("A1").Select
Range("F3").Select
Application.CutCopyMode = False
Selection.Copy
Range("F23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 3).Range("A1").Select
Range("E3").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("D3").Select
Selection.ClearContents
Range("C3").Select
Selection.ClearContents
Range("B3").Select
Selection.ClearContents
Selection.ConvertToLinkedDataType ServiceID:=268435456, LanguageCulture:= _
"en-US"
End Sub
Any help will is greatly appreciated!
Edit:
Worksheet
Attached is screenshot of the worksheet I am working with. I would like to be able to paste the values of A3 & C3-F3, and the formula in B3 into the table seen below. A new row needs to be inserted prior to pasting all of this information.
This should work. It's basically just a clearer version of your code.
Sub PlaceOrder()
Dim tbl As ListObject
Dim LastRow As Long
Set tbl = ActiveSheet.ListObjects("Table1")
LastRow = tbl.Range.Rows.Count 'get # of last row
With ActiveSheet
'copy and paste A3
.Range("A3").Copy
tbl.Range(LastRow, 1).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
'copy and paste B3
.Range("B3").Copy
tbl.Range(LastRow, 2).Offset(1, 0).PasteSpecial Paste:=xlPasteFormulas
'copy and paste C3:F3
.Range("C3:F3").Copy
tbl.Range(LastRow, 3).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
'clear value in B3:F3
.Range("B3:F3").ClearContents
End With
End Sub
Your original macro did not work because the system forgot the copied value after this line:
Selection.ListObject.ListRows.Add AlwaysInsert:=False

VBA error PasteSpecial Method of Range Class failed

I have this error all the time, even though I tried removing selection etc. Before this I update some cells and then the code copies and selects 7 rows (it was supposed to select 10) and before pasting gives the error. Where the code gets stuck is
Range("C4:AN" & Range("B5").Value).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Please help
Sub Refresh()
Call Confirm
Dim F As New FDSOfficeAPI_Server
Application.Iteration = True
Application.ScreenUpdating = False
Sheets("Supply Chain").Select
Range("C1:BA10000").Select
V = F.RefreshSelectedFDSCodes
Range("B1").Select
Application.ScreenUpdating = True
Sheets("Supply Chain").Select
Range("C4:AN4").Copy
Range("C4:AN" & Range("B5").Value).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("BD4:CO4").Select
Application.CutCopyMode = False
Selection.Copy
Range("BD4:CO" & Range("BC5").Value).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("DE4:EO4").Select
Application.CutCopyMode = False
Selection.Copy
Range("DE4:EO" & Range("DD5").Value).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End Sub
Your code would be easier to maintain if you move the repeated parts to a separate sub. Try something more like this:
Sub Tester()
FillDownFormats Range("C4:AN4"), Range("B5").Value
FillDownFormats Range("BD4:CO4"), Range("BC5").Value
FillDownFormats Range("DE4:EO4"), Range("DD5").Value
End Sub
'Fill down the formats from rngSource to fill total of 'numRows' rows
Sub FillDownFormats(rngSource As Range, numRows As Long)
rngSource.Offset(1, 0).Resize(numRows-1).ClearFormats 'clear any existing formatting
rngSource.Copy
rngSource.Resize(numRows).PasteSpecial _
Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub

Cells not being copied to the next available cell

I am trying to record a macro that copies values from 4 cells then pastes them on another sheet that serves as a sort of log. I cannot get the values to paste in a new row though despite using the "Relative References" button when recording the macro. Is there something I can add to the code below to make the values paste in the next available row?
'''
Sub Again()
'
' Again Macro
'
'
Range("B5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
ActiveCell.Offset(0, -3).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Test").Select
Range("C5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Test").Select
Range("D5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Test").Select
Range("E5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
'''
Sub add_value()
Dim wbA As Workbook
Dim wsA As Worksheet
Set wbA = ActiveWorkbook
Set wsA = wbA.Worksheets("Sheet1")
Dim nrow As Long
nrow = 6
Do Until wsA.Range("B" & nrow).Value = ""
wsA.Range("B" & nrow).Value = wsA.Range("B3").Value
wsA.Range("C" & nrow).Value = wsA.Range("C3").Value
Exit Sub
nrow = nrow + 1
Loop
End Sub
This is actually working, now i just have to figure out how to offset it

Looping until cell is empty

I have my macro written but now I need it to run in a loop until cell I2 is empty.
Can anyone help with this?
Sheets("Value Imported Data").Select
Range("I2:Q2").Select
Selection.Copy
Sheets("Good data").Select
Range("I1").Select
Selection.End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Value Imported Data").Select
ActiveCell.Resize(40, 9).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
I assumed it would be something like this but it does not work at all
Sub CandidatesInfo()
Dim r As Range
Dim Cell As Range
Sheets("Value Imported Data").Select
Set r = Range("I2")
For Each Cell In r
If r.Notempty Then
Range("I2:Q2").Select
Selection.Copy
Sheets("Good data").Select
Range("I1").Select
Selection.End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Value Imported Data").Select
ActiveCell.Resize(40, 9).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
End If
Next
End Sub
Basically what I want to do is if I2 is empty do nothing but if it is not empty copy I2 to Q2 into another sheet and once copied go back in the sheet where the information was copied and delete the next 40 information down and 9 to the right and start over again. As I said the top macro works perfectly, now it is just a matter of starting over and over until I2 is empty.
Any help is appreciated.
Thank you very much

Excel macro crashes after running for a few minutes

My code runs for about 5min and the goes into recovery error/message. I have posted 3 Call macros but have 40 in the code.
During the macro the cells need to calculate and I tried a time delay to help but no good.
Private Sub Worksheet_calculate()
If Range("$be8").Value = 1 Then
Application.EnableEvents = True
Call Macro1
Application.EnableEvents = False
End If
If Range("$bf8").Value = 1 Then
Application.EnableEvents = True
Call Macro2
Application.EnableEvents = False
End If
If Range("$bg8").Value = 1 Then
Application.EnableEvents = True
Call Macro3
Application.EnableEvents = False
End If
Sub Macro1()
'
'
Macro1 Macro
'
'
Sheets("Calc. 1").Select
Rows("11:11").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Application.Wait (Now + TimeValue("0:00:05"))
Rows("7:7").Select
Selection.Copy
Rows("11:11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("B2").Select
Sheets("Calc.").Select
Range("A7:Q50002").Select
Selection.Copy
Range("A3").Select
ActiveSheet.Paste
Calculate
Range("AZ3").Select
Selection.Copy
Range("BA3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("B1").Select
Workbooks.Add
DoEvents
End Sub
Sub Macro2()
'
' Macro2 Macro
'
'
Sheets("Calc. 1").Select
Rows("11:11").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Application.Wait (Now + TimeValue("0:00:05"))
Rows("7:7").Select
Selection.Copy
Rows("11:11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("B2").Select
Sheets("Calc.").Select
Range("A8:Q50002").Select
Selection.Copy
Range("A3").Select
ActiveSheet.Paste
Calculate
Range("AZ3").Select
Selection.Copy
Range("BA3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("B1").Select
Workbooks.Add
DoEvents
End Sub
Sub Macro3()
'
' Macro3 Macro
'
'
Sheets("Calc. 1").Select
Rows("11:11").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Application.Wait (Now + TimeValue("0:00:05"))
Rows("7:7").Select
Selection.Copy
Rows("11:11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("B2").Select
Sheets("Calc.").Select
Range("A9:Q50002").Select
Selection.Copy
Range("A3").Select
ActiveSheet.Paste
Calculate
Range("AZ3").Select
Selection.Copy
Range("BA3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("B1").Select
Workbooks.Add
DoEvents
End Sub
First, this is just a big pack of recorded macros, and therefore nearly unreadable, as hinted by Matt Webb in its excellent comment. That being said, I went to do some analysis.
The macros are strictly the same, besides the column of the Range("A7:Q50002").Select, which varies from 7 to 9 (and probably up to 46, if you really have 40 of those). Time to make a proper sub with a parameter :
Private Sub Worksheet_calculate()
If Range("$be8").Value = 1 Then
Application.EnableEvents = True
Call Macro(myRow)
Application.EnableEvents = False
End If
If Range("$bf8").Value = 1 Then
Application.EnableEvents = True
Call Macro(myRow)
Application.EnableEvents = False
End If
If Range("$bg8").Value = 1 Then
Application.EnableEvents = True
Call Macro(myRow)
Application.EnableEvents = False
End If
End Sub
Sub Macro(myRow)
Sheets("Calc. 1").Rows("11:11").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Application.Wait (Now + TimeValue("0:00:05"))
Rows("7:7").Copy
Rows("11:11").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
'Range("B2").Select
Sheets("Calc.").Range("A" & CStr(myRow) & ":Q50002").Copy
Range("A3").Select
ActiveSheet.Paste
Calculate
Range("AZ3").Copy
Range("BA3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
'Range("B1").Select
Workbooks.Add
DoEvents
End Sub
it'still ugly, but now, you just have 1 main procedure instead of 40, and I cleaned a little bit. I also did comment the useless selects.
Next question : what does it do? Seems like it
inserts the row 11, then copies the row 7 into. That one shall not be problematic.
Copies a specific cell from another sheet to the current sheet. Well, why not.
Calculates. Here we have a problem. I have no clue of your calculations, and there very well might be circular things here, or insane amounts of calculations to do. Especially when I see hard-coded values going to the 50000th row... The wolf may hide here. Depends on your worksheet. This is my first suspect.
Copy another cell in the main sheet. No risk here.
Add an empty workbook. Without anything. Hum, are you sure? If your 40 values are positive, you dynamically open 40 workbooks, without naming them, or putting any data in them??? This is my second and last suspect.
Now, you have to troubleshoot yourself to know which suspect is guilty. For that, you can add breakpoints on a few strategic lines of the macro, to see what takes time(my bet on the calculate, but I may be wrong). Especially before & after the calculate & the Workbook.add
But ask yourself is everything I listed is useful, especially the calculate part, and especially the workbook.add part. Your macro might very well flood the computer memory with all those workbooks open in memory that are still useless.

Resources