I have been trying, unsuccessfully, to automatically freeze panes on my worksheet as I scroll the cursor up and down the sheet. Is the following achievable?
The worksheet currently contains three years of data; each year has two header rows, 46 rows of data and then two blanks rows, so each year occupies 50 rows. Col A is a header column, col B to AK contain data. My screen displays 37 rows; I don't want to reduce the font size any further as the data becomes too hard to read.
Currently, in order to work on a particular year of data, I manually freeze panes by selecting either cell B3, or B53, or B103, depending on whether I want to look at 2018, or 2019, or 2020 data. The problem comes when I need to work on several years of data as I'm then having to freeze or unfreeze the panes as I scroll up and down the sheet. By way of example, I initially have the panes frozen based on cell B3. Rows 1 & 2 contain the 2018 header detail, locked in place by having frozen the panes. Rows 3 to 37 show 2018 data. As I scroll down, rows 51 & 52 (the 2019 header rows) come into view. As I scroll down those header rows gradually move up the screen and eventually move off the top of the screen, leaving me with the original 2018 headers (rows 1 & 2) and with 2019 data (rows 53 to 87) on screen - and with the potential for error as a result of the header rows showing a year that doesn't relate to the data.
What I want to achieve is that once the cursor moves out of a given year of data into the next, or the previous, year's data block I want VBA to automatically unfreeze the panes, scroll the sheet so that the required header rows are displayed as the two top rows on screen, then refreeze the panes. So - for clarity - let's say I've been working on 2020 data. Rows 101 & 102 are at the top of the screen, locked in place by having frozen the panes at B103. As soon as I try to drag the cursor above row 101 the panes should unfreeze, rows 51 & 52 are placed at the top of the display, cell B53 is made the active cell and the panes are frozen. If I then continue to drag the cursor upwards to the 2018 data, as soon as I try to drag the cursor above row 51 the panes should unfreeze, rows 1 & 2 are placed at the top of the display, cell B3 is made the active cell and the panes are frozen.
Likewise, if I'm initially working on 2018 data and then move downwards to 2020 data (or beyond), rows 1 & 2 will be at the top of the screen, locked in place by having frozen the panes at B3. As soon as I drag the cursor below row 50 the panes should unfreeze, rows 51 & 52 are placed at the top of the display, cell B53 is made the active cell and the panes are frozen. If I then continue to drag the cursor downwards to the 2020 data, as soon as I try to drag the cursor below row 100 the panes should unfreeze, rows 101 & 102 are placed at the top of the display, cell B103 is made the active cell and the panes are frozen.
Can this be achieved with VBA or am I asking for the impossible? As a last resort I'll create separate sheets for each year but my preference is to keep it as a single worksheet and learn something new about VBA. And as a cautionary note, future years data may exceed 50 rows, so any solution should not be based on 50-row blocks of annual data.
The worksheet currently contains three years of data; each year has two header rows, 46 rows of data and then two blanks rows, so each year occupies 50 rows. Col A is a header column, col B to AK contain data. My screen displays 37 rows; I don't want to reduce the font size any further as the data becomes too hard to read.
Currently, in order to work on a particular year of data, I manually freeze panes by selecting either cell B3, or B53, or B103, depending on whether I want to look at 2018, or 2019, or 2020 data. The problem comes when I need to work on several years of data as I'm then having to freeze or unfreeze the panes as I scroll up and down the sheet. By way of example, I initially have the panes frozen based on cell B3. Rows 1 & 2 contain the 2018 header detail, locked in place by having frozen the panes. Rows 3 to 37 show 2018 data. As I scroll down, rows 51 & 52 (the 2019 header rows) come into view. As I scroll down those header rows gradually move up the screen and eventually move off the top of the screen, leaving me with the original 2018 headers (rows 1 & 2) and with 2019 data (rows 53 to 87) on screen - and with the potential for error as a result of the header rows showing a year that doesn't relate to the data.
What I want to achieve is that once the cursor moves out of a given year of data into the next, or the previous, year's data block I want VBA to automatically unfreeze the panes, scroll the sheet so that the required header rows are displayed as the two top rows on screen, then refreeze the panes. So - for clarity - let's say I've been working on 2020 data. Rows 101 & 102 are at the top of the screen, locked in place by having frozen the panes at B103. As soon as I try to drag the cursor above row 101 the panes should unfreeze, rows 51 & 52 are placed at the top of the display, cell B53 is made the active cell and the panes are frozen. If I then continue to drag the cursor upwards to the 2018 data, as soon as I try to drag the cursor above row 51 the panes should unfreeze, rows 1 & 2 are placed at the top of the display, cell B3 is made the active cell and the panes are frozen.
Likewise, if I'm initially working on 2018 data and then move downwards to 2020 data (or beyond), rows 1 & 2 will be at the top of the screen, locked in place by having frozen the panes at B3. As soon as I drag the cursor below row 50 the panes should unfreeze, rows 51 & 52 are placed at the top of the display, cell B53 is made the active cell and the panes are frozen. If I then continue to drag the cursor downwards to the 2020 data, as soon as I try to drag the cursor below row 100 the panes should unfreeze, rows 101 & 102 are placed at the top of the display, cell B103 is made the active cell and the panes are frozen.
Can this be achieved with VBA or am I asking for the impossible? As a last resort I'll create separate sheets for each year but my preference is to keep it as a single worksheet and learn something new about VBA. And as a cautionary note, future years data may exceed 50 rows, so any solution should not be based on 50-row blocks of annual data.