Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default relative cell references

Hi,

I am fairly new to programming in VBA/Excel, and find myself having
trouble with the most seemingly straightforward things...

I am working on a macro that creates a new worksheet and a new row in an
existing worksheet, and links a cell in the new row to a cell in the new
worksheet.

The relevant code (from a recorded macro) is as follows:

Sub SettInnNy()
Windows("innretninger.xls").Activate
Sheets("innretninger").Select
Range("B108").Select
Selection.EntireRow.Insert
Windows("data.xls").Activate
Sheets("Ny").Copy Befo=Sheets(1)

Windows("innretninger.xls").Activate
Range("H108").Select
ActiveCell.FormulaR1C1 = "='[data.xls]'Sheets(1)!R12C5"
End Sub


It gives the following error message:

<Run-time error '1004': Application-defined or object-defined error


When recorded the last line is

ActiveCell.FormulaR1C1 = "='[data.xls]Ny (2)'!R12C5"

which works fine the first time, but the second and third time keeps on
referring to the same sheet, when it should refer to the newest one. It
is obviously my modification that isn't working
-- any help on how to do this would be greatly appreciated.

Thomas

*** Sent via Developersdex http://www.developersdex.com ***
  #2   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default relative cell references

Hi Thomas,

Try this:

Sub SettInnNy()
With Workbooks("data.xls")
.Sheets("Ny").Copy Befo=.Sheets(1)
wsName = .Sheets(1).Name
End With
With Workbooks("innretninger.xls").Sheets("innretninger ")
.Rows(108).Insert
.Range("H108").FormulaR1C1 = "='[data.xls]" & _
wsName & "'!R12C5"
End With
End Sub


Regards,
KL



"Thomas F" wrote in message
...
Hi,

I am fairly new to programming in VBA/Excel, and find myself having
trouble with the most seemingly straightforward things...

I am working on a macro that creates a new worksheet and a new row in an
existing worksheet, and links a cell in the new row to a cell in the new
worksheet.

The relevant code (from a recorded macro) is as follows:

Sub SettInnNy()
Windows("innretninger.xls").Activate
Sheets("innretninger").Select
Range("B108").Select
Selection.EntireRow.Insert
Windows("data.xls").Activate
Sheets("Ny").Copy Befo=Sheets(1)

Windows("innretninger.xls").Activate
Range("H108").Select
ActiveCell.FormulaR1C1 = "='[data.xls]'Sheets(1)!R12C5"
End Sub


It gives the following error message:

<Run-time error '1004': Application-defined or object-defined error


When recorded the last line is

ActiveCell.FormulaR1C1 = "='[data.xls]Ny (2)'!R12C5"

which works fine the first time, but the second and third time keeps on
referring to the same sheet, when it should refer to the newest one. It
is obviously my modification that isn't working
-- any help on how to do this would be greatly appreciated.

Thomas

*** Sent via Developersdex http://www.developersdex.com ***



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default relative cell references

Thanks a million, KL -- not only did it work, it also gave me some faith
in VBA. Good to see that things can be done more elegantly than what
comes out of the record-macro function.

*** Sent via Developersdex http://www.developersdex.com ***
  #4   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default relative cell references

You're welcome. And yes, the macro recorder is good for quick'n'dirty code,
whcih then has to be tidied up for optimal performance. It is also a great
tool to quickly identify objects, properties, methods, etc. you may need to
use.

KL

"Thomas F" wrote in message
...
Thanks a million, KL -- not only did it work, it also gave me some faith
in VBA. Good to see that things can be done more elegantly than what
comes out of the record-macro function.

*** Sent via Developersdex http://www.developersdex.com ***



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Absolute and Relative Cell References Karen Excel Discussion (Misc queries) 1 April 1st 09 09:41 AM
convert relative cell references to absolute cell references via amacro? Dave F[_2_] Excel Discussion (Misc queries) 1 May 15th 08 04:43 PM
Relative Cell References within VBA code Jandy Excel Discussion (Misc queries) 2 April 21st 05 02:17 AM
Transpose-relative cell references would be useful. carlmanaster Excel Worksheet Functions 7 March 15th 05 01:04 AM
how to use relative cell references with solver you Excel Programming 1 December 31st 04 01:43 AM


All times are GMT +1. The time now is 08:59 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"