We help IT Professionals succeed at work.
Get Started

excel vba defining range with specialcells

Lambel
Lambel asked
on
683 Views
Last Modified: 2012-05-11
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
VBA Developer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE