excel vba defining range with specialcells

Lambel
Lambel used Ask the Experts™
on
I am building a procedure that creates a new workbook, looks up excel input files and copies/formats them in the new workbook.  I want make my code dynamic so it can adjust to varying cell ranges to copy and paste.  

In my attached code, line 330, I would like to replace "A834" with a dynamic reference to the cell.
How can I do this?

'copy whole sheet
    Sheets("OH Count Collateral").Select
    Cells.Select
    Selection.Copy
    
    'paste in OH Count sheet
    Sheets("OH Count").Select
    Range("A1").Select
    ActiveSheet.Paste
    
    'figure out where the data ends for the next paste
    Range("A1").Select
    Selection.End(xlDown).Select
    Range("A834").Select
    
    'copy the next sheet
    Sheets("OH Count Pool").Select
    Range("A1").Select
    ' use the SpecialCells method to select only cells with data
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Application.CutCopyMode = False
    Selection.Copy
    
    'paste into OH Count below the data from the first copy/paste
    Sheets("OH Count").Select
    ActiveSheet.Paste

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
VBA Developer
Commented:
Replace this:
    Range("A1").Select
    Selection.End(xlDown).Select
    Range("A834").Select

With this:

  Dim lastRow as long 'Add this to the top of your module

  lastRow = Range("A" & rows.count).end(xlup).row + 1

This will give you the row after the last row with data.  So if you're data ends in A864 and you want to start putting new data in A865, that line above will do that.

So then to use it you would do something like this:

cells(lastRow, "A").value = "New data"

Author

Commented:
Thanks that works first time!
Lynn

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial