Selecting a specific range in a worksheet using a recorded macro

Andy0311

Board Regular
Joined
Oct 16, 2019
Messages
118
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello,

Shown below is a an active range that I have to have to select and cut and paste. There are 30 some ranges that I have to do this for, one of each teacher on our staff. Each range can be different in length because the number of units graded are unique for each teacher. In this example, the teacher graded 11 units (see A10), but each range can vary from 1 unit graded to several hundred. Here is my goal: record a macro (using relative referencing) which starts at the teacher name (A1) and labels B1, "Name"; C1, "Course"; D1 "Unit"; E1 "Date Graded"; and F1 Grade and then automatically selects the range, in this case A1:F10. I would copy and paste this into an email, and move down the sheet to the next teacher and run the macro for the next active range, and so forth. I am trying to avoid selecting each range by moving my cursor down the range, which can go several hundred lines. I have tried to record this macro but have not been successful. If what I am trying to do is possible, any help in recommending a way to accomplish this would be gratefully appreciated.

1701279921777.png
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You'd need to post more data and make sure it accurately represents what you're working with (real names are not needed) because 8 rows with one teacher name doesn't give much information. Best to use xl2BB add in (shown on the posting toolbar when you compose a post). If you can't, you can copy and paste from a worksheet. Given what you've shown, my initial questions are,
- is everything blank from A2 to A8?
- does the count have to be there (A9), or could the start of a new range be the name of the next teacher? Total could be in B9 or elsewhere?
- what happens when the next range is selected (because what's the point of populating B1:F1 with those values again)?
- what is the code supposed to do when the range is selected?

If you want to keep it simpler and not bother with those details, you could run code to raise a range input box (it is type 8) and manually select the range. The only way to avoid having to mouse over a hundred rows would be if you started the selection and typed the row/column values into the input box, but you'd have to know what those are.

One last idea might be to have dynamic named ranges, but you'd need one for each teacher. However, if this
Each range can be different in length because the number of units graded are unique for each teacher.
simply means the ranges are different sizes from each other but their size does not change then that's easier. Otherwise you'd need dynamic ranges based on cell values and I've never done that but it seems doable:
 
Upvote 0
Let me try and answer your questions first:

- is everything blank from A2 to A8? Yes. The system generates the report in this format, and I ask for the output to be in Excel.
- does the count have to be there (A9), or could the start of a new range be the name of the next teacher? Total could be in B9 or elsewhere? It could be elsewhere, but it has to be there so the teacher knows the number of units he or she graded.
- what happens when the next range is selected (because what's the point of populating B1:F1 with those values again)? The next range begins when the next teacher's name appears. All ranges between each teachers' data have a blank line.
- what is the code supposed to do when the range is selected? The code is supposed to (1)supply the labels described above, and (2) select the range. Then I can copy the range and paste it into an email and send the teacher her data.

Shown below is the report for 4 teachers. The first teacher, Rhonda, is in A1 and her report ends at A3, where her graded units are shown (in this case,1), and so forth. A4 is blank. A5 begins the second teacher, Brian. The formatting is the same for each teacher, except that Brian's total number graded is 263. Each teacher's data may vary significantly.

I hope what I have provided helps. Using xl2BB would have been better but I am not proficient in using that yet, so I had to cut and paste.


Rhonda
JoanEnglish 1Unit 3
11/11/2023​
A
Rhonda: 1 units
Brian
DavidHistoryUnit 2
11/11/2023​
A
DonaldSociologyUnit 3
11/12/2023​
A
AndrewGovernmentUnit 8
11/13/2023​
B
JoanPsychologyUnit 2
11/14/2023​
B
Brian: 4 units
Amy
ArtEnglish 1Unit 2
11/11/2023​
A
JohnPoetryUnit 3
11/12/2023​
C
SamShort StoriesUnit 8
11/13/2023​
B
JoeEnglish 4Unit 2
11/14/2023​
B
ElaineGrammarUnit 7
11/15/2023​
D
Amy: 5 units
Dan
NickBiologyUnit 2
11/13/2023​
B
GeorgePhys ScienceUnit 3
11/14/2023​
B
AlicePhysicsUnit 8
11/15/2023​
B
MikeChemistryUnit 2
11/16/2023​
C
JulieForensicsUnit 7
11/17/2023​
D
EmilyMed TermUnit 8
11/18/2023​
A
JessyBiology 2Unit 9
11/19/2023​
A
Dan: 7 units

Thanks for helping me.


 
Upvote 0
Taking a break from a job so just a quick review for now.
but it has to be there so the teacher knows the number of units he or she graded.
That depends on how much you want to automate. It's possible have the code do things like create the email & body content, which means that the number could be part of the body text or subject or both rather than in the grid. Like so
1701386343949.png


You can even send a range as a pdf attachment. However, it might be easier to capture the range if it starts and stops with the same name. I would have to think about that. Perhaps you would prefer not to get too complicated and just copy/paste your range and build your own emails. If you automate the email you would need some kind of flag to denote that this was done (be that for a period of time or whatever) otherwise you'd repeatedly send the same person the same info. A sheet column with the date of the sent email would probably suffice.
The code is supposed to (1)supply the labels described above,
This part is still not clear. You didn't show it in your last post so I still don't know if it's supposed to do that once as you explained, or at the beginning of each range.
 
Upvote 0
I don't deal with pasting into an email, but I think this code has the basis of what you are after.
It copies the block for each name in column A and pastes in columns J:O (but hopefully you can paste as required into your email(s) instead.)
The code does not actually 'select' each range but in general code does not need to select cells/ranges to work with them and selecting slows your code.

VBA Code:
Sub Copy_Blocks()
  Dim rA As Range
 
  For Each rA In Range("B1", Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
    rA.Offset(-1, -1).Resize(rA.Rows.Count + 2, 6).Copy Destination:=Range("J" & Rows.Count).End(xlUp).Offset(1)
  Next rA
End Sub

My sample data is in columns A:F and the code produced the results shown in columns J:O

Andy0311.xlsm
ABCDEFGHIJKLMNO
1Rhonda
2JoanEnglish 1Unit 311/11/2023ARhonda
3Rhonda: 1 unitsJoanEnglish 1Unit 311/11/2023A
4Rhonda: 1 units
5BrianBrian
6DavidHistoryUnit 211/11/2023ADavidHistoryUnit 211/11/2023A
7DonaldSociologyUnit 312/11/2023ADonaldSociologyUnit 312/11/2023A
8AndrewGovernmentUnit 813/11/2023BAndrewGovernmentUnit 813/11/2023B
9JoanPsychologyUnit 214/11/2023BJoanPsychologyUnit 214/11/2023B
10Brian: 4 unitsBrian: 4 units
11Amy
12AmyArtEnglish 1Unit 211/11/2023A
13ArtEnglish 1Unit 211/11/2023AJohnPoetryUnit 312/11/2023C
14JohnPoetryUnit 312/11/2023CSamShort StoriesUnit 813/11/2023B
15SamShort StoriesUnit 813/11/2023BJoeEnglish 4Unit 214/11/2023B
16JoeEnglish 4Unit 214/11/2023BElaineGrammarUnit 715/11/2023D
17ElaineGrammarUnit 715/11/2023DAmy: 5 units
18Amy: 5 unitsDan
19NickBiologyUnit 213/11/2023B
20DanGeorgePhys ScienceUnit 314/11/2023B
21NickBiologyUnit 213/11/2023BAlicePhysicsUnit 815/11/2023B
22GeorgePhys ScienceUnit 314/11/2023BMikeChemistryUnit 216/11/2023C
23AlicePhysicsUnit 815/11/2023BJulieForensicsUnit 717/11/2023D
24MikeChemistryUnit 216/11/2023CEmilyMed TermUnit 818/11/2023A
25JulieForensicsUnit 717/11/2023DJessyBiology 2Unit 919/11/2023A
26EmilyMed TermUnit 818/11/2023ADan: 7 units
27JessyBiology 2Unit 919/11/2023A
28Dan: 7 units
Sheet1
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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