Excel Macro that will copy formula 'X' cells

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

jjacobi

Thread Starter
Joined
Jul 18, 2005
Messages
2
Scenario:
Excel spreadsheet 1 with 'X' number of lines in it.

Goal:
Create a macro in sheet 2 that will copy a formula from line 2 (in this same spreadsheet) to line 'X'. I am simply doing a reformat in sheet 1 using some simple formulas so I can get this into a format to import.

Example:
First, determine number of rows in spreadsheet 1. I think this can be done with the counta function. I am trying to set a variable up that holds this value. This is what I have in the macro currently to do this:
Dim x
x = "=COUNTA(CSEXPORT!C:C)-1"

I am hoping that this set x = 10.

Second, I need to copy line 2 in spreadsheet 2 down 'x' rows.

I have tried relative positioning thinking that would work, but the macro still specifies specific cells. I thought I could use EndDown, EndUp to do what I needed, but it still puts in specific cell values.

The only thing that I need to know is how to use the variable in the VBA script. I don't know how to tell it to go to cell A(x). I have attached a spreadsheet with a rough example. The macro within this sheet is not completed.

Thanks
 

Attachments

OBP

Joined
Mar 8, 2005
Messages
19,895
Try this t find x -

Dim x As Integer
Range("a1").Select
Selection.CurrentRegion.Select
x = Selection.Rows.Count + 1
MsgBox x
The plus 1 gives you 10, the actual number of rows you have is 9.
To paste x rows use

dim y
for y =1 to x
activesheet.paste
activecell.offset(1,0)
next y
 

jjacobi

Thread Starter
Joined
Jul 18, 2005
Messages
2
Setting of the variable worked as you said.

The For loop did not work exactly right, but it did get me headed down the path I needed to get a solution that works.

Thanks for the help.
 
Joined
Sep 4, 2003
Messages
4,915
The standard way of selecting a cell using a variable row is
Code:
Range("C" & x).Select
where "x" equals the row variable and "C" equals the column.

You can also use
Code:
Cells(x,y).Select
where "x" equals the row index number and "y" equals the column index number. So, if you use the statement Cells(2,2).select you will be taken to column B row 2.

Rollin
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Top