New to Macros: Creates in Sheet1 only

Hiker

New Member
Joined
Jun 15, 2011
Messages
33
Office Version
  1. 2013
Platform
  1. Windows
Hi, I'm new to Macros and created one to take certain data from an existing workbook to be placed on a new sheet, which then renames it and moves it to its own workbook. All goes well until I run a similar Macro with different data. Apparently since I'm in the same session, this new sheet will be named Sheet2, an error is returned "subscript out of range". If I close Excel, and reopen I'll be able to run either Macro since Excel is back to Sheet1 again. I have very limited knowledge of VBA but could easily edit the code if it's only the matter of changing Sheet1 to SheetAny, or whatever. Thanks PS Macros were made using the recorder.
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi, I'm new to Macros and created one to take certain data from an existing workbook to be placed on a new sheet, which then renames it and moves it to its own workbook. All goes well until I run a similar Macro with different data. Apparently since I'm in the same session, this new sheet will be named Sheet2, an error is returned "subscript out of range". If I close Excel, and reopen I'll be able to run either Macro since Excel is back to Sheet1 again. I have very limited knowledge of VBA but could easily edit the code if it's only the matter of changing Sheet1 to SheetAny, or whatever. Thanks PS Macros were made using the recorder.

Please post the code or relevant part of it.

You could try a with statement:

Code:
Sub test()

With ActiveSheet
    Range("A1").Value = 1
End With

End Sub

"With ActiveSheet" means that the following lines of code will be executed on the Sheet that is active (open) until the line "End With".

With the above code, whatever sheet is active when the macro is run, will have the number 1 placed in cell A1.
 
Upvote 0
Please post the code or relevant part of it.

Code:
    ActiveWindow.ScrollColumn = 50
    ActiveWindow.ScrollColumn = 51
    Columns("AZ:AZ").Select
    Application.CutCopyMode = False
    Selection.Copy
   [COLOR=Red]Sheets("Sheet1").Select[/COLOR]
    Columns("B:B").Select
    ActiveSheet.Paste
    Sheets("Members").Select
    ActiveWindow.ScrollColumn = 50
    ActiveWindow.ScrollColumn = 49
    ActiveWindow.ScrollColumn = 48
Red font was the highlighted text from the debugger.

I don't want number 1 in A1.

As the macro is run now, it will only complete as intended if when it opens a new sheet to paste to, and that sheet needs to be labeled as sheet1 on the bottom tab. If I try to rerun the macro in the same session without saving, closing and reopening, the macro quits, I assume because it's no longer sheet1, but the new sheet automatically opened by the macro, is numbered sheet 2 or more.

I also assume, rather than scrolling from one column to another when copying and pasting data, there's a better way to get from one column to another. The goto function (F5)?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top