Solved

Excel: Macro to Find Row Range For Cut/Paste

Posted on 2014-10-23
7
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 34

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 34

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 34

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

734 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