I'm teaching myself to code VBA for Excel and am relatively new to VBA, but not Excel. Today I started a YouTube lesson and the recorder won't pick up any selection of a cell. It will record selection of a range, row or column. It will record everything else, as far as I can tell. I recently switched from Home to Business Excel, but I cannot imagine this is the cause. Any quick suggestions? I've looked through other "simple excel macro" questions, but none is this simple! Thanks, Ellie
CodePudding user response:
Start Macro recording. Select a range of two cells. Click on one of the two cells. Stop recording. Look at the recorded macro. The code for selecting a single cell should be apparent after a small amount of study.
But to be honest, you shouldn't be writing code that operates on 'Selected' ranges or cells. Instead you should be working on a range/cell reference. Much better in the long run.
CodePudding user response:
I cannot explain why your Macro Recorder does not record your range selection, that is abnormal behaviour. Single cell selection, however, is not recorded unless you do something else with that selection.
To select a cell, use ThisWorkbook.Sheets("Sheet1").Range("A1").Select
To select a range of cells, use ThisWorkbook.Sheets("Sheet1").Range("A1:B2").Select
Replace the "Sheet1" with the name of your sheet, and the cell references with the cells you are interested in.
As other users have mentioned in the comments, consider avoid using the .Select method for ranges and cells. While you are learning, it may be a useful tool to clearly show the steps used by the program. However, as you progress into larger and more complicated codes, repeated use of the .Select method will really slow down your programs' performance.
