0

I am currently having a problem compiling a Macro that I am trying to make. I am either trying to use a Named Range or the actual range on a different sheet and copy and paste that data into the current sheet. I want the data to post to the current sheet so I can run this Macro on every day of the month. Here is what I have compiled but for some reason I cannot use the ActiveSheet. Attached is my code.

Sub Macro1()

' Daily Route Sheet

    Sheets("2").Select
    ActiveCell.Range("A1:H44").Select
    Selection.Copy
    Sheets("ActiveSheet").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub
trincot
  • 317,000
  • 35
  • 244
  • 286
  • I believe you mean to have `Sheets("Sheet1").Select` but best would be to do this without selecting anything. – Matt Cremeens Jun 22 '16 at 17:58
  • No, I am trying to grab information from sheet 2 - I named it "2" so our inspectors can quickly click on it. So what I am trying to do is our contractors like to copy and paste but they do not use special paste so it ruins our named ranges. I hid the ranges and I am trying to write something that copies from lets say Day 2 and can be put into Day 23 for example. I would like to copy the data from a different day and post it in the current sheet they are working in – hipster_pete Jun 22 '16 at 18:03
  • And you have a sheet with name "ActiveSheet", if you don't have only need erase the line `Sheets("ActiveSheet").Select` – Juanpablomo Jun 22 '16 at 18:06
  • Take a look [here](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) for avoiding `Select` in your macros. – TheEngineer Jun 22 '16 at 19:22
  • what is the benefit of `activesheet.select` ?! if it is already active, why would you re-active it ? – Patrick Lepelletier Jun 23 '16 at 09:10

4 Answers4

3

Instead of selecting, cutting and pasting, consider

Sub Macro1()
    Worksheets("Sheet1").Range("A1:H44").Value = Worksheets("Sheet2").Range("A1:H44").Value
End Sub
Matt Cremeens
  • 4,951
  • 7
  • 38
  • 67
0

Change

Sheets("ActiveSheet").Select

to

ActiveSheet.Select

However, I agree with Matt on the point that you should try to stay away from .Select

0

"ActiveSheet" is a string. Activesheet is a Worksheet Object.

Activesheet.range("A1").value is equivalent to Sheets(Activesheet.name).value or to Sheets(Activesheet.index).value.

That is because Sheets (or Worksheets, wich are the same) , can be referenced in braquets () trough either their names (being strings), or their Index (number, wich is the sheet's position in the Workbook).

Worksheetsis a collection (by collection , i mean a kind of array) of your workbook's sheets.

Activesheet is a VBA easy way to access a sheet wich is already active, in the same way that ActiveWorkbook' for workbooks, orActiveCell` for cells. They are all Objects , not Strings.

Patrick Lepelletier
  • 1,596
  • 2
  • 17
  • 24
-3

Sub Macro1() ActiveSheet.Range("A1:H44").Value = Worksheets("2").Range("A1:H44").Value End Sub

Kudos to Matt for the answer!