Copy/Paste Error Special Paste - vba

I'm trying to rewrite someone's program, but I'm not familiar with VBA. I've tried many different ranges with .End(x1Up) and (x1Down). I understand the Up is the better option, but it just keeps pasting it in the same row every time and doesn't drop down to the next one. Here's my code (many tries have been commented out):
Sub Save_History()
Sheets("Simple Calculation").Select
Range("A10:J10").Select
Selection.Copy
'Sheets("Media Data History").Select
'Range("A65536").End(xlup).Offset(1,0)
'If Range("A1") <> "" Then
'Range("A1").End(xlUp).Offset(1, 0).Select
'End If
' Range("A1").End(xlUp).Select
'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub

This should work but I encourage you to take a look at THIS Post
Sub Save_History()
Sheets("Simple Calculation").Select
Range("A10:J10").Select
Selection.Copy
Sheets("Media Data History").Select
Range("A65536").End(xlup).Offset(1,0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub

Got it!!
Sub Save_History()
Sheets("Simple Calculation").Select
Range("A10:J10").Select
Selection.Copy
Sheets("Media Data History").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub

Related

Copying and Pasting into a new Table Row using VBA

I am trying to figure this out and I am hoping you can help
Basically I have Form and Data Sheet. I am looking to copy the information in the form into a new blank row within Table1 on the data sheet,
I have managed to get as far as the following but this causes the data to be over written each time, (rather than a a new row).
Sub Macro1()
Sheets("Form").Select
Range("G5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[ID]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("D3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Contact Date]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("D4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Channel]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("D5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Agent Name]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Contact ID]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("G3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Scored by]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("G4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Team Leader]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I realise this may seem like a simple question but I am struggling to work this out.
FYI - There will be 29 Columns to this table so If I should be doing something to make this cleaner, please let me know
Here's a more streamlined way to approach this:
EDIT - updated to add "config" array to reduce repetition
Sub Transfer()
Dim config, itm, arr
Dim rw As Range, listCols As ListColumns
Dim shtForm As Worksheet
Set shtForm = Worksheets("Form") '<< data source
With Sheets("Data").ListObjects("Table1")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
'array of strings with pairs of "[colname]<>[range address]"
config = Array("ID<>G5", "Contact Date<>D3", "Channel<>D4")
'loop over each item in the config array and transfer the value to the
' appropriate column
For Each itm In config
arr = Split(itm, "<>") ' split to colname and cell address
rw.Cells(listCols(arr(0)).Index).Value = shtForm.Range(arr(1)).Value
Next itm
End Sub
No copy/paste/select/activate required.

vba copy from defined loop and paste in set cell

I'm working on a business case with a growing number of scenarios, I could do all these in scenario manager but that's not convenient right now.
What I'm trying to do is the following: I've got a list of names for the given scenarios, these should be pasted to an input field which then runs the scenario, the results should then be copied to a location specified for that scenario.
The current code does it without any issues but it feels 'sluggish' to me because I need to make a different block of code for each scenario:
Sheets("Output").Select
Range("G7").Select
ActiveCell.FormulaR1C1 = "All stores"
Sheets("Stuurgroep").Select
Range("N4:N18").Select
Selection.Copy
Range("C4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Output").Select
Range("G7").Select
ActiveCell.FormulaR1C1 = "Quartile 1"
Sheets("Stuurgroep").Select
Range("N4:N18").Select
Selection.Copy
Range("D4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
In which G7 is the place for the scenario, "all stores" in this case is the first scenario, N4:N18 are the results of that scenario and C4 is the place for those results, with the results of the next scenario being pasted into D4.
I've tried the following code to create the list to paste into G7 but it doesn't work:
Set ListScenarios = ActiveWorkbook.Sheets("RefTables").Range("B3:B11")
For Each cell In ListScenarios
Application.CutCopyMode = False
Selection.Copy
Sheets("Output").Select
Range("G7").Select
ActiveSheet.Paste
Sheets("Stuurgroep").Select
Range("N4:N18").Select
Selection.Copy
Call Paste
Next cell
Sub Paste()
Set Destination = ActiveWorkbook.Sheets("Stuurgroep").Range("C4:K4")
For Each cell In Destination
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next cell
Help would be much appreciated!

Excel Macro Possible Bug

I have made a Macro that performs some repetitive tasks (updates certain data connections and transfers this information to another Excel). The code works perfectly, however, sometimes, it changes the formatting of certain cells, in worksheets, that are not even called in the Macro.
This sounds as a bug. I have previously had the same problem, that was solved by removing the On Error Resume Next line.
Could you please read my code, and see if there is something that could be causing this bug? I really cannot allow to lose my formatting since I am using these sheets for important company reports.
Before running the Macro:
After running the Macro:
Here goes my code:
Sub TRANSFER_INPUT()
'
' TRANSFER_INPUT Macro
'
Dim MWWorkBook As Workbook
Set MWWorkBook = ActiveWorkbook
Sheets("PAR").Select
Dim Pateka As String
Worksheets("PAR").Activate
Pateka = Range("E5").Value
Dim Datum1 As String
Worksheets("PAR").Activate
Datum1 = Range("E6").Value
Dim InputExcel As Workbook
Workbooks.Open Filename:=Pateka & "INPUT" & Datum1 & ".xlsx", UpdateLinks:=3
Set InputExcel = ActiveWorkbook
'###
'MAIN WORKBOOK / PREFRLANJE FAJLOVI
'###
'INPUTBILANS
MWWorkBook.Activate
Sheets("INPUTBILANS").Select
Range("F11:M1000").Select
Selection.Copy
Range("Y11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'REFRESH BILANS VO INPUT
MWWorkBook.Activate
Sheets("INPUTBILANS").Select
Range("F10").Select
Selection.QueryTable.Refresh BackgroundQuery:=False
'###
'###
'PREFRLI VO INPUT
'###
'###
'Kopiraj Bilans_1
Range("F11:V1000").Select
Selection.Copy
'Pastiraj Bilans_1 VO INPUT / Bilans_1 vo Bilans_2
InputExcel.Activate
Sheets("BILANS_1").Select
Range("B8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("B8:R1000").Select
Selection.Copy
Sheets("BILANS_2").Select
Range("B8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
''' PREFRLI I KOPIRAJ COSTS
MWWorkBook.Activate
Sheets("COSTS").Select
Range("A4").Select
Selection.QueryTable.Refresh BackgroundQuery:=False
Range("A5:AV312").Select
Selection.Copy
InputExcel.Activate
Sheets("COSTS").Activate
Range("A5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'Kopiraj OPER.COST
MWWorkBook.Activate
Sheets("OPER.COST | NONOP | PRIHODI").Select
Range("D7").Select
Selection.QueryTable.Refresh BackgroundQuery:=False
Range("G16:P63").Select
Selection.Copy
InputExcel.Activate
Sheets("OPER.COST").Select
Range("Z4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'Kopiraj PRIHODI
MWWorkBook.Activate
Sheets("OPER.COST | NONOP | PRIHODI").Select
Range("D65:F204").Select
Selection.Copy
InputExcel.Activate
Sheets("PRIHODI").Select
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'Kopiraj NONOP
MWWorkBook.Activate
Sheets("OPER.COST | NONOP | PRIHODI").Select
Range("F8:F14").Select
Selection.Copy
InputExcel.Activate
Sheets("NONOP").Select
Range("D5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
Thank you so much!

Error when pasting values using macro

Why canĀ“t my code below paste values only?
The result is pastes the formula instead of the value.
Can anyone help?
Please check my code as follow :
Sheets("Invoice Print").Activate
Range("F21:F27").Select
Selection.SpecialCells(xlCellTypeFormulas, 1).Select
Selection.Copy
Sheets("Outgoing Goods").Select
Cells(Rows.Count, 1).Range("K1").End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Paste
Application.CutCopyMode = False
Are there any mistakes?
PS: I'm using Excel 2013.
Please advise.
Thank you.
You overwrite the pasted values right in the next line with ActiveSheet.Paste
Also you should not use .Select and Selection..
Sheets("Invoice Print").Range("F21:F27").SpecialCells(xlCellTypeFormulas, 1).Copy
With Sheets("Outgoing Goods")
.Cells(.Rows.Count, 1).Range("K1").End(xlUp).Offset(1, 0).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'This line would overwrite the pasted values with not explecit values only
'ActiveSheet.Paste
Application.CutCopyMode = False
End With
Hope I could help.

Calling a sub within a loop

was trying to call a sub(trialMacro) from inside another sub(Macro2) but somehow it does not get activated. Is there a reason why? The call function works fine if its not within the loop. I.e. if I just do a plain call function under another sub. Is my coding right? I'm not sure if the loop disrupts the execution
' trial Macro
Sub trialMacro()
Dim PrevCell As Range
Set PrevCell = ActiveCell
SolverOk SetCell:=ActiveCell, MaxMinVal:=2, ValueOf:="0", ByChange:= _
"$M$2,$M$3,$M$5,$M$7"
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
'Copy in sample and out of sample error
PrevCell.Resize(1, 3).Copy
'Paste Values of in sample and out of sample errors
PrevCell.Offset(0, 4).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Copy Co-efficient
Range("M2:M7").Select
Application.CutCopyMode = False
Selection.Copy
'Select paste destination
PrevCell.Offset(0, 7).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
'Copy Paste Following months data
PrevCell.Offset(1, -1).Resize(12, 1).Copy
'Select target destination
PrevCell.Offset(0, 13).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
PrevCell.Offset(1, 0).Select
End Sub
Sub Macro2()
'
' Macro2 Macro
Dim i, j As Integer
For i = 50 To 162
For j = 0 To 113
Sheets("Model v2 DUBDAT >0").Cells(i, 17).Select
Call trialMacro
Range("P50:BA50").Offset(j, 0).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Reference Sheet").Select
Range("D6").Offset(j, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Model v2 DUBDAT >0").Select
Next
Next
End Sub
Give this a read...
https://msdn.microsoft.com/en-us/library/office/gg251432.aspx
Remove call from this line
Call trialMacro
Should be..
trialMacro
If you use the call statement the subname must be followed by parentheses.