im trying to create a code that can help me to paste my formulas in a range.
the range is always changing as i have to include information on a daily basis at the en of my list
the existing code almost gets the job done but6 instead of recognizing the value on cell a1 (a count of the amount of records to calculate) it pastes the information on range e1:k1
Sub Run_Formulas()
'
' Run_Formulas Macro
'
Dim As Variant
x1 As Variant
x1 = Worksheets("Raw Data").Range("A1")
'
Range("E4:K4").Select
Selection.Copy
Range("E8" & ":" & "K" & "a1").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
End Sub
how can i get the macro to recognize the contents of cell a1 and to use it for my final range/
Related
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
I need to save some data by just clicking a button.
It should be easy but I dont know the syntax for vba.
Range("P2:P47").Copy
Sheets("All Data").Select
Range("J2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I have 2 problem with this code,
1 there is "a lot of" selections that I do not know how to get rid of(I know you should try not to use selections).
2 My real problem is this:
cell J2 in the code should not be a determined cell. I would like to have a "lookup value" and search for that in an array, and then using the mathing cell to paste my values.
So in my workbook cell A1 = "Aug 19" and in row 2 all the months are listed. So cell A2 = "Jan 19", B2 = "Feb 19" etc.
I would like to select cell H2 and then pasting by matching my lookup value "Aug 19" to H2 contains "Aug 19"
So in excel formulas I would basically just write:
=HLOOKUP(A1;'All Data'!$B$2:$AL$2;1;FALSE)
Solved problem 1
Sub savedata()
Range("P2:P47").Copy
Worksheets("All Data").Range("J2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
EDIT on problem 2:
Sub savedata()
Range("P2:P47").Copy
ActiveSheet.Range("C3:K3").Find(What:="Aug 19").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
This work as I need it to! Still som questions tho, I couldn't figure out how to reference cell A1 instead of "Aug 19".
I solved it. I ended up with more selections that I would like but I tried to minimize it but I got a lot of error codes that way.
see code below:
Sub savedata()
G = Range("A1").Value
Range("P2:P47").Copy
Worksheets("All Data").Select
Range("C2:AL2").Find(What:=G).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Worksheets("Add Data").Select
Application.CutCopyMode = False
Range("C4:K34").ClearContents
End Sub
I would like to do like this bu this does not work:
Sub savedata()
G = Range("A1").Value
Range("P2:P47").Copy
Worksheets("All Data").Range("C2:AL2").Find(What:=G).PasteSpecial
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Worksheets("Add Data").Select
Application.CutCopyMode = False
Range("C4:K34").ClearContents
End Sub
I am trying to paste data from a Range into the next empty cell in a Table Row on a different sheet.
I can copy and paste into the table but not to next empty cell, the copied data just keeps overriding existing data. I would prefer to keep the table.
Thanks
Sub ZulilyData()
With Sheets("Orders")
Sheets("Orders").Select
.Range("D2", Range("D" & Rows.Count).End(xlUp)).Copy
Sheets("Source").Select
Range("TableSource[Item '#]").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Sheets("Orders").Select
.Range("F2", Range("F" & Rows.Count).End(xlUp)).Copy
Sheets("Source").Select
Range("TableSource[Quantity]").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
End With
End Sub
I have a macro to copy and paste values into different columns in my spreadsheet. On the target columns I have some formulas that I need to avoid to override when the macro paste the values as I actually want the formula to calculate the new values. I have a code created already, but I would really appreciate you help adding the condition to avoid pasting where there are formulas on the target.
Sub RawDataNew()
'
' RawDataNew Macro
' To move validated to previous week on raw data tab
'
Range("$B$17:$AQ$2572").AutoFilter Field:=6
Range("Z18:AB2572").Select
Selection.Copy
Range("AU18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("H18:H2572").Select
Selection.Copy
Range("AR18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("O18:O2572").Select
Application.CutCopyMode = False
Selection.Copy
Range("AS18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("W18:W2572").Select
Selection.Copy
Range("AX18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("X18:X2572").Select
Application.CutCopyMode = False
Selection.Copy
Range("AY18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("G4").FormulaR1C1 = "=TODAY()"
Range("G4").Value = Date
Range("G5").Value = Environ("username")
'
End Sub
The paste method you're using Paste:=xlPasteValues is only pasting the values; if you want formulas as well as values, you'll need Paste:=xlPasteAll.
If you ONLY want formulas, you'll need Paste:=xlPasteFormulas
Please refer to link for other optional parameters
Another suggestion is using the = method ie.
Range("A1").value = Range("B1").value
As the copy/paste method is very slow, especially when you have as much data as you're using here now. Also, you'll get all the extra formatting from the copied cells, which can be quite annoying to deal with
I am a starting VBA enthusiast and I would like some help on the below formula as I have no idea how to make sure the formula applies to all rows in the book. As you can see, I have started copying the actual code, but as I have to do this for up to 100 rows this will be too manually.
Thanks
Sub Charts()
' Charts Macro
' Run charts
Range("D7").Value = Range("D11")
Range("E7:G7").Select
Selection.Copy
Range("E11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D7").Value = Range("D12")
Range("E7:G7").Select
Selection.Copy
Range("E12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D7").Value = Range("D13")
Range("E7:G7").Select
Selection.Copy
Range("E13").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D7").Value = Range("D14")
Range("E7:G7").Select
Selection.Copy
Range("E14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Is this what you are trying?
Option Explicit
Sub Charts()
Dim i As Long
'~~> Change this to the relevant sheet
With Sheets("Sheet1")
For i = 11 To 14 '<~~ Change 14 to whatever row you want to go to
.Range("D7").Value = .Range("D" & i).Value
.Range("E7:G7").Copy
.Range("E" & i).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Next i
End With
End Sub