Solved

Excel: Macro to Find Row Range For Cut/Paste

Posted on 2014-10-23
7
240 Views
Last Modified: 2014-10-27
Hello,

I have the below code. Note my nested comments for the solutions I'm looking for. Essentially I've defined 3 ranges, but I'm not sure how to reference these ranges in the macro formula.

Note that I call upon the range 'sentlistend' twice - and the second time this would be a different row. Not sure if Excel can adjust for this.

Sub mac_addtosentlist()
'
' mac_addtosentlist Macro

'This macro copies the details of the people who will receive the 3 & 6 months emails and adds them on the sent list for future comparison

Dim sentlistend As Long
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sent List")
    sentlistend = ws.Range("A" & Rows.Count).End(xlUp).Row

Dim 3monthrowtally As Long
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("3 Month Letter")
    rowcount = ws.Range("A" & Rows.Count).End(xlUp).Row

Dim 6monthrowtally As Long
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("6 Month Letter")
    rowcount = ws.Range("A" & Rows.Count).End(xlUp).Row

    Sheets("3 Month Letter").Select

'Below I want to select all the rows with data identified by 3monthrowtally
    Activesheet.Rows("3monthrowtally").Select
    Selection.Copy
    Sheets("Sent List").Select

    
'Below I want to select 'A?' where '?' represents the row number identified by sentlistend. This will find me the first blank cell on this sheet, so I can paste the copied data. 

    ActiveCell.Row(sentlistend)+1
    ActiveSheet.Paste

    Sheets("6 Month Letter").Select
'

'Time to do the same, but for a second sheet. Below I want to select all the rows with data identified by 6monthrowtally
    Activesheet.Rows("6monthrowtally").Select
    Selection.Copy
    Sheets("Sent List").Select

    
'Below I want to select 'A?' where '?' represents the row number identified by sentlistend. This will find me the first blank cell on this sheet, so I can paste the copied data.
'Note that because we've pasted data in already, does the sentlistend have to be redefined? 

    ActiveCell.Row(sentlistend)+1
    ActiveSheet.Paste
    Sheets("Paste Here").Select

End Sub

Open in new window


Thanks.
0
Comment
Question by:dabug80
  • 3
  • 3
7 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 40401231
It's not really clear what you want to cut/copy/paste from/to.

Do you want to cut/copy rows from the sheets '3 Month Letter' and '6 Month Letter' and paste to sheet 'Sent List'?

If you do what rows do you want to cut/copy from each sheet and where do you want to paste them?

Also, there are some problems with your variables - you can't start a variable name with a number and you can't declare a variable, eg ws, more than once in the same sub.

The only declarations you should need are these.
Dim ws As Worksheet
Dim sentlistend As Long
Dim monthrowtally3 As Long
Dim monthrowtally6 As Long
Dim RowCount As Long

Open in new window


PS If all you want to do is copy the last rows from '3 Month Letter' and '6 Month Letter' to 'Send List' you could try this.
Sub mac_addtosentlist()
    '
    ' mac_addtosentlist Macro

    'This macro copies the details of the people who will receive the 3 & 6 months emails and adds them on the sent list for future comparison
Dim ws As Worksheet
Dim wsSent As Worksheet

    Set wsSent = ThisWorkbook.Sheets("Sent List")
    
    Set ws = ThisWorkbook.Sheets("3 Month Letter")
    
    ws.Range("A" & Rows.Count).End(xlUp).EntireRow.Copy wsSent.Range("A" & Rows.Count).Offset(1)
    
    Set ws = ThisWorkbook.Sheets("6 Month Letter")
    
    ws.Range("A" & Rows.Count).End(xlUp).EntireRow.Copy wsSent.Range("A" & Rows.Count).Offset(1)
      
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:dabug80
ID: 40405529
Hello,

To clarify, I've added a sample sheet.

Essentially - I'd like to do the following:

1. 3 Month Letter sheet - copy all populated rows from 2 down.
2. Paste these copied rows to the bottom of the blank cells on the 'Sent Emails' sheet

3. 6 Month Letter sheet - copy all populated rows from 2 down.
4. Paste these copied rows to the bottom of the blank cells on the 'Sent Emails' sheet

Thanks
EE-Sample.xlsm
0
 
LVL 12

Accepted Solution

by:
sdwalker earned 500 total points
ID: 40407179
If I understand it correctly, this is what you're looking for.  If not, please let me know.
sdw-EE-Sample.xlsm
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Closing Comment

by:dabug80
ID: 40407435
Excellent solution. Thanks for building on my initial attempt and adding nested comments to explain your working.
0
 
LVL 33

Expert Comment

by:Norie
ID: 40407540
dabug80

Just curious, but did you try the code I posted?

PS Using Select/Activate etc in Excel VBA code isn't good practice.
0
 
LVL 1

Author Comment

by:dabug80
ID: 40407549
Hi imnorie,

To be honest, no - I didn't try your code, as I was a bit put off by your sentence:
"PS If all you want to do is copy the last rows from '3 Month Letter' and '6 Month Letter' to 'Send List' you could try this."

As the definition of 'last rows' was a bit ambiguous to me. So I posted a clarifying post along with a sample spreadsheet.
0
 
LVL 33

Expert Comment

by:Norie
ID: 40407604
No problem.:)

What I meant by 'last rows' was the last populated row(s) in each sheet, but you have a solution and that's the most important thing.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now