Change Row Height When Row is Selected

antifragile

New Member
Joined
Dec 22, 2014
Messages
13
Hi,

I would like a vba code that allows me to select the entire row of the selected cell, doubles the row height(or to certain height), and fills the row background color with, say, turquoise. Also, when I select another cell or row, I would like the previously selected row to go back to its original format, size and fill color.

So far I was able to figure out selecting the entire row of the selected cell, I can also increase the row height but I cannot have it go back to its original formatting when I select another cell or row.
My code is something like below:

Code:
[COLOR=#011993][FONT=Menlo]Private[/FONT][/COLOR][COLOR=#011993][FONT=Menlo]Sub[/FONT][/COLOR][COLOR=#000000][FONT=Menlo] Worksheet_SelectionChange([/FONT][/COLOR][COLOR=#011993][FONT=Menlo]ByVal[/FONT][/COLOR][COLOR=#000000][FONT=Menlo] Target [/FONT][/COLOR][COLOR=#011993][FONT=Menlo]As[/FONT][/COLOR][COLOR=#000000][FONT=Menlo] Range)[/FONT][/COLOR][COLOR=#000000][FONT=Menlo]ActiveCell.EntireRow.Select[/FONT][/COLOR]
[COLOR=#000000][FONT=Menlo]Selection.RowHeight = 32[/FONT][/COLOR]
[COLOR=#011993][FONT=Menlo]EndSub[/FONT][/COLOR]

Any ideas are greatly appreciated.

Thanks,
 
Dear Saba Sabaratnam,

My apologies. You are correct. Apparently I have not copied the whole code when I was trying it on my worksheet. It works as desired. Thank you for your help!
If you don't mind I would like to ask a bit of a clarification as I am trying to enhance my knowledge in VBA. I am sorry if these questions are boring but my knowledge is very limited on vba. Can you please explain in plain text what these rows of commands do?

Code:
[COLOR=#011993][FONT=Menlo]
Public[/FONT][/COLOR][COLOR=#000000][FONT=Menlo] strLastCell [/FONT][/COLOR][COLOR=#011993][FONT=Menlo]As[/FONT][/COLOR][COLOR=#000000][FONT=Menlo] [/FONT][/COLOR][COLOR=#011993][FONT=Menlo]String[/FONT][/COLOR][COLOR=#000000][FONT=Menlo][COLOR=#011993]Public[/COLOR] strActiveCell [COLOR=#011993]As[/COLOR] [COLOR=#011993]String[/COLOR][/FONT][/COLOR]
[COLOR=#000000][FONT=Menlo][COLOR=#011993]Public[/COLOR] intLastCellHeight [COLOR=#011993]As[/COLOR] [COLOR=#011993]Integer
[/COLOR][COLOR=#ff0000]   ' WHY DO WE START WITH "PUBLIC" ELEMENT?
   ' WHAT IS THE PURPORSE OF "STR"?[/COLOR][/FONT][/COLOR]
[COLOR=#ff0000][/COLOR][FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[COLOR=#000000][FONT=Menlo][COLOR=#011993]Private[/COLOR] [COLOR=#011993]Sub[/COLOR] Worksheet_SelectionChange([COLOR=#011993]ByVal[/COLOR] Target [COLOR=#011993]As[/COLOR] Range)[/FONT][/COLOR]
[FONT=Menlo]
[COLOR=#ff0000] 'IF STR ACTIVECELL IS NOTHING, THEN??? WHAT DOES IT TELL EXCEL?[/COLOR][/FONT]
[COLOR=#000000][FONT=Menlo]If strActiveCell = "" Then strActiveCell = ActiveCell.Address
 [COLOR=#ff0000]'WHY REFERRING TO LASTCELL?
[/COLOR][/FONT][/COLOR]
[COLOR=#000000][FONT=Menlo]strLastCell = strActiveCell[/FONT][/COLOR]
[COLOR=#000000][FONT=Menlo]strActiveCell = ActiveCell.Address[/FONT][/COLOR]
[COLOR=#000000][FONT=Menlo]intLastCellHeight = ActiveCell.RowHeight[/FONT][/COLOR]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[COLOR=#000000][FONT=Menlo]Target.RowHeight = 32[/FONT][/COLOR]
[FONT=Menlo]
[/FONT]
[FONT=Menlo][COLOR=#ff0000] 'IF THIS COMMAND DECREASES THE ROW HEGIHT BACK TO ITS ORIGINAL HEIGHT AFTER ANOTHER ROW IS SELECTED HOW DOES IT DO IT?[/COLOR][/FONT]
[COLOR=#000000][FONT=Menlo]Range(strLastCell).RowHeight = intLastCellHeight[/FONT][/COLOR]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[COLOR=#011993][FONT=Menlo]End[COLOR=#000000] [/COLOR]Sub[/FONT][/COLOR]
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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