recording macro question

fuerza

New Member
Joined
Jun 22, 2013
Messages
3
how can i make macro work on different sheets and different cells .

example im going record macro then im going to change the color of cell A1 in sheet 1 then i will stop the macro then i will run the previous macro that i have recorded in sheet 2 but i want to change the color of different cell ,cell B3 sheet 2 with that macro.

but the result is cell A1 is filled not the cell B3

how can i fill cell B3 with the same macro?

thanks
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
If the color of A1 and B3 is the same, you just have to reference both cells in the one macro and it will change the color in both with one run.
 
Upvote 0
Replace all references to, ActiveSheet", with the actual sheet name. For example:
Rich (BB code):
Sub test()
   Sheets("Sheet1").Range("A1").Interior.Color = vbRed
   Sheets("Sheet1").Range("A1").Font.Color = vbWhite
   
   Sheets("Sheet2").Range("B3").Interior.Color = vbBlue
   Sheets("Sheet2").Range("B3").Font.Color = vbYellow
End Sub
 
Upvote 0
thanks got it i use the funtion relative reference


why is that when i record macro and i run it to other sheet sometime some of its parts are in the wrong place sometime it works perfectly? i use excel 2007

also when i try to record macro using filling cells with colors sometimes i got an error which need to debug like run time error 1004 object defined error?

thanks
 
Upvote 0
Consider a simple example. The code generated by the macro recorder produces statements like:
ActiveSheet.Select.

If the Sheet1 tab is selected when you run the code, Sheeet1 is the ActiveSheet, and the code produces expected results.
If the Sheet2 tab is selected when the code is run, Sheet2 is the ActiveSheet, and the code produces unexpected results.

Is the code producing an error?
No! The code is doing as instructed and working on the ActiveSheet.
It is the programmer’s responsibility to be unambiguous about which worksheet the code performs actions on.
That’s why it is advisable to edit the recorder generated code and use specific referencing with regards to sheet names.

Note also in post #3. You don’t need to select a sheet/range/cell in order to use it.

Sheets, ranges and cells are objects. The cause of the, 1004 run time error, may be that the code can’t determine what it is supposed to be working on. But without seeing the code it is difficult to say.
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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