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
Related
I am a newbie for a VBA coding.I am building custom Stock screener. For that I want to print a stock prices at particular times to different cells for my strategy.I developed a code somehow.But I don't know how to make it work in a particular sheet(sheet 1) irrespective of active sheets.
Following is the code which i pasted in a thisworkbook module.
Thanks in advance
Option Explicit
Private Sub Workbook_Open()
Call ScheduleTask
End Sub
Public Sub ScheduleTask()
Application.OnTime TimeValue("14:46:00"), "ThisWorkbook.Execute"
End Sub
Public Sub Execute()
Debug.Print "Executing task", Now
Range("D8:D57").Copy
Range("T8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks:=False, Transpose:=False
Call ScheduleTask1
End Sub
Public Sub ScheduleTask1()
Application.OnTime TimeValue("14:47:00"),"ThisWorkbook.Execute1"
End Sub
Public Sub Execute1()
Debug.Print "Executing task", Now
Range("D8:D57").Copy
Range("U8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks:=False, Transpose:=False
Call ScheduleTask2
End Sub
Public Sub ScheduleTask2()
Application.OnTime TimeValue("14:47:00"), "ThisWorkbook.Execute2"
End Sub
Public Sub Execute2()
Debug.Print "Executing task", Now
Range("D8:D57").Copy
Range("V8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks:=False, Transpose:=False
Call ScheduleTask
End Sub`enter code here`
Specify a worksheet for every Range object. If you don't do that then VBA will use the ActiveSheet.
For example
Range("D8:D57").Copy
will copy from the ActiveSheet but
ThisWorkbook.Worksheets("Sheet1").Range("D8:D57").Copy
will copy from Sheet1 of the worbkook this code is in (represented by ThisWorkbook).
Also You can work with
dim wb as Workbook
dim SheetOne as Worksheet
dim RangeOne as Range
set wb = ThisWorkbook
set SheetOne = wb.Sheets("Sheet1")
set RangeOne = SheetOne.Range("D8:D57")
RangeOne.Copy SheetOne.Range("T8")
Specify the worksheet to be used for every Range statement like so:
Private Sub Workbook_Open()
Call ScheduleTask
End Sub
Public Sub ScheduleTask()
Application.OnTime TimeValue("14:46:00"), "ThisWorkbook.Dostuff"
End Sub
Public Sub Dostuff()
Dim Mysheet As Worksheet
Set Mysheet = ThisWorkbook.Worksheets("Enter sheet name here")
Debug.Print "Executing task", Now
Mysheet.Range("D8:D57").Copy
Mysheet.Range("T8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Call ScheduleTask1
End Sub
Public Sub ScheduleTask1()
Application.OnTime TimeValue("14:47:00"), "ThisWorkbook.Dostuff1"
End Sub
Public Sub Dostuff1()
Dim Mysheet As Worksheet
Set Mysheet = ThisWorkbook.Worksheets("Enter sheet name here")
Debug.Print "Executing task", Now
Mysheet.Range("D8:D57").Copy
Mysheet.Range("U8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Call ScheduleTask2
End Sub
Public Sub ScheduleTask2()
Application.OnTime TimeValue("14:47:00"), "ThisWorkbook.Dostuff2"
End Sub
Public Sub Dostuff2()
Dim Mysheet As Worksheet
Set Mysheet = ThisWorkbook.Worksheets("Enter sheet name here")
Debug.Print "Executing task", Now
Mysheet.Range("D8:D57").Copy
Mysheet.Range("V8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Call ScheduleTask
End Sub
Also do not use Execute as a sub name as it is also a command and will cause clashes.
Edited with #Pᴇʜ 's suggestions (twice)
I have an If statement that runs on the change of a cell. This part works fine. However, when it runs the macro, for some reason it adds is about 40 extra lines. I'v used the breakpoint and discovered that the lines are added in after the the paste special. Can anyone tell me why?
Thanks in advance.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$AG$4" Then
Call CapEx_Copy_Paste_Delete
End If
End Sub
Sub CapEx_Copy_Paste_Delete()
'
' CapEx_Copy_Paste_Delete Macro
'
'
Rows("11:11").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B4:AG4").Select
Selection.Copy
Range("B11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("AG4").Select
Selection.ClearContents
Range("B4:E4").Select
Selection.ClearContents
Range("H4:I4").Select
Selection.ClearContents
Range("L4:M4").Select
Selection.ClearContents
Range("P4:Q4").Select
Selection.ClearContents
Range("T4:U4").Select
Selection.ClearContents
Range("X4:Y4").Select
Selection.ClearContents
Range("Z4").Select
Selection.ClearContents
Range("AA4").Select
Selection.ClearContents
Range("AC4").Select
Selection.ClearContents
Range("AD4").Select
Selection.ClearContents
Range("B4").Select
End Sub
Does this work any better??:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$AG$4" Then
Application.EnableEvents = False
Call CapEx_Copy_Paste_Delete
Application.EnableEvents = True
End If
End Sub
Here is the neater version of your code. It's likely all that use of Select isn't helping your problems:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$AG$4" Then
Application.EnableEvents = False
Call CapEx_Copy_Paste_Delete
Application.EnableEvents = True
End If
End Sub
Sub CapEx_Copy_Paste_Delete()
Dim ws As Worksheet
Dim arrRanges As Variant, v As Variant
'set this as the worksheet you want to update
Set ws = ThisWorkbook.Worksheets("Sheet1")
'set this as the ranges you want to clear
arrRanges = Array("AG4", "B4:E4", "H4:I4", "L4:M4", "P4:Q4", "T4:U4", "X4:Y4", "Z4")
With ws
.Rows("11:11").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
.Range("B4:AG4").Copy
.Range("B11").PasteSpecial Paste:=xlPasteValues
For Each v In arrRanges
.Range(v).ClearContents
Next v
End With
End Sub
Updated to include Gary's Student's suggestion - all credit goes to him for suggesting you disable events in your first sub
To be short and sweet with my requirement, I need a code to do the conditions below.
Select from range A2:G5
Then check if a sheet named with current date i:e 29-02-2016
If yes,
then copy paste the range in A1 leave 3 rows below for the next data to be pasted below that.
If no,
create a new sheet and name it with current date and then copy paste the range in A1 leave 3 rows below for the next data to be pasted below that.
I tried the below code but it give me error once the current date sheet is created.
Sub Macro1()
Sheets("Sheet1").Select
Range("D3:G12").Select
Selection.Copy
sheets = "todaysdate".select
Dim todaysdate As String
todaysdate = Format(Date, "dd-mm-yyyy")
AddNew:
Sheets.Add , Worksheets(Worksheets.Count)
ActiveSheet.Name = todaysdate
On Error GoTo AddNew
Sheets(todaysdate).Select
Range("A1048576").Select
Selection.End(xlUp).Select
ActiveCell.Offset(3, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End Sub
Try these modifications.
Sub Macro1()
Dim todaysdate As String
With Worksheets("Sheet1")
.Range("D3:G12").Copy
End With
todaysdate = Format(Date, "dd-mm-yyyy")
On Error GoTo AddNew
With Worksheets(todaysdate)
On Error GoTo 0
With .Cells(Rows.Count, "A").End(xlUp).Offset(3, 0)
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
End With
End With
Exit Sub
AddNew:
With Worksheets.Add(after:=Sheets(Sheets.Count))
.Name = todaysdate
With .Cells(Rows.Count, "A").End(xlUp)
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
End With
End With
End Sub
Step through the modified procedure with the [F8] key to watch how it handles the thrown error and continues on to exit or processes the paste with a three row offset.
Could you please help me out with below formula? It gives object defined or app defined error. Thanks a lot.
Sub cellstovalues()
Sheets("Parsing").Select
Columns("B:B").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
As an alternative, you can simply assign the values to the Value2 (or Value) property of the range:
Sub cellstovalues()
With Sheets("Parsing")
With Intersect(.Range("B:B"), .UsedRange)
.Value2 = .Value2
End With
End With
End Sub
or for a specific range:
Sub cellstovalues()
With Sheets("Parsing").Range("B1:C10")
.Value2 = .Value2
End With
End Sub
Your code is having issues from you selecting the entire column B. Try using this. This should find the last used cell in your column, then copy and paste to convert the formulas to values like you want.
Sub cellstovalues()
Dim ws As Worksheet
Dim LastRow As Integer
Dim rng As Range
Set ws = ThisWorkbook.Sheets("Parsing")
With ws
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
Set rng = .Range(.Cells(1, "B"), .Cells(LastRow, "B"))
End With
rng.Copy
rng.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
I'm trying to get the sourcedata from a range of cells on an "input sheet" for data entry to copy this information to 2 different sheets. After this copy, I will use one sheet to leave for all data, and manipulate the other sheet for tracking.
This is what I've gotten so far, but I get an error that an object is required.
Sub CopySource()
Dim NextRow As Range
With Sheets(Array("MasterData", "MainData")).Select
Set NextRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
End With
Range("SourceData").Copy
NextRow.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
Just do each copy individually, then:
Sub CopySource()
Range("SourceData").Copy
Sheets("MasterData").Cells(Sheets("MasterData").Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial _
Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
Sheets("MainData").Cells(Sheets("MainData").Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial _
Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
Application.CutCopyMode = False
End Sub
Possible fix for the original code:
Sub CopySource()
Dim NextRow() As Range 'This makes an array of ranges
With Sheets(Array("MasterData", "MainData")).Select
'not sure if this will now populate multiple elements of NextRow() with multiple
'ranges, one from each sheet, above
Set NextRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
End With
Range("SourceData").Copy
NextRow(0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
NextRow(1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
Not sure if that'll work, but it might be worth a try