Jim C
Jim C

Reputation: 1

How to prevent Initialize Macro in Excel from clearing data I need to save

Update macro writes data from "Input" worksheet to "Orders" worksheet. I want to initialize data on Input sheet but it also clears data written to Orders sheet. Any help will be appreciated. Update Macro:

Sheets("Orders").Select
Range("B2").Select
ActiveCell.FormulaR1C1 = "=Input!R17C7"
Range("C2").Select
ActiveCell.FormulaR1C1 = "=Input!R17C9" 

Initialize Macro: clears data in both sheets

Sheets("Input").Select
Range("G17").Select
Selection.ClearContents
Range("I17").Select
Selection.ClearContents

Upvotes: 0

Views: 42

Answers (2)

urdearboy
urdearboy

Reputation: 14580

You should avoid the .Select & .Selection method when possible. Please see this link for more information on that.

Notice how you can skip those lines and jump right to the conclusion:

Thisworkbook.Sheets("Orders").Range("B2").FormulaR1C1 = "Input!R17C7"
Thisworkbook.SHeets("Orders").Range("C2").FormulaR1C1 = "Input!R17C9"

&

Thisworkbook.Sheets("Input").Range("G17:I17").ClearContents

Upvotes: 1

Vityata
Vityata

Reputation: 43585

Write only these two lines

Sheets("Input").Range("G17").ClearContents
Sheets("Input").Range("I17").ClearContents

to clear the contents in G17 and I17 of "Input".

Upvotes: 0

Related Questions