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 in Macros

Relative cell References within a LOOP and IF Statement


I’m new to writing macros for Excel 97
I wish to clear a range of cells on the current line to the left of th
active cell.
I’m having trouble with setting up a “range” with relative cel
values.
The following routine works well to clear an individual cell,
How do I modify it to clear a range of cells.

Any help would be really appreciated


Sub Loop1()
' This loop runs down a column until there is nothing in the column o
the it’s immediate left
‘ Clears a cell 5 columns to left

Do
ActiveCell.Select
If ActiveCell.Offset(0, -1) = 0 Then Clear_Cells
ActiveCell.Offset(1, 0).Select
‘ gets next row
Loop Until IsEmpty(ActiveCell.Offset(0, -1))
End Sub

Sub Clear_Cells()
ActiveCell.Offset(0, -5).Select
Selection.ClearContents
ActiveCell.Offset(0, 5).Select
End Sub

Barry Staples Nottingham Englan

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Relative Cell References in Macros

Range(Cells(activeCell.row,1),ActiveCell.offset(0,-1)).ClearContents

--
Regards
Tom Ogilvy


Barry Staples wrote in
message ...
Relative cell References within a LOOP and IF Statement


I'm new to writing macros for Excel 97
I wish to clear a range of cells on the current line to the left of the
active cell.
I'm having trouble with setting up a "range" with relative cell
values.
The following routine works well to clear an individual cell,
How do I modify it to clear a range of cells.

Any help would be really appreciated


Sub Loop1()
' This loop runs down a column until there is nothing in the column on
the it's immediate left
' Clears a cell 5 columns to left

Do
ActiveCell.Select
If ActiveCell.Offset(0, -1) = 0 Then Clear_Cells
ActiveCell.Offset(1, 0).Select
' gets next row
Loop Until IsEmpty(ActiveCell.Offset(0, -1))
End Sub

Sub Clear_Cells()
ActiveCell.Offset(0, -5).Select
Selection.ClearContents
ActiveCell.Offset(0, 5).Select
End Sub

Barry Staples Nottingham England


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 80
Default Relative Cell References in Macros


Sub Clear_Cells()
ActiveCell.Offset(0, -5).Resize(1,5).Select
Selection.ClearContents
ActiveCell.Offset(0, 5).Select
End Sub

Or, remove the Select:


Sub Clear_Cells()
ActiveCell.Offset(0, -5).Resize(1,5).ClearContents
End Sub


--
Darren
"Barry Staples " wrote in
message ...
Relative cell References within a LOOP and IF Statement


I'm new to writing macros for Excel 97
I wish to clear a range of cells on the current line to the left of the
active cell.
I'm having trouble with setting up a "range" with relative cell
values.
The following routine works well to clear an individual cell,
How do I modify it to clear a range of cells.

Any help would be really appreciated


Sub Loop1()
' This loop runs down a column until there is nothing in the column on
the it's immediate left
' Clears a cell 5 columns to left

Do
ActiveCell.Select
If ActiveCell.Offset(0, -1) = 0 Then Clear_Cells
ActiveCell.Offset(1, 0).Select
' gets next row
Loop Until IsEmpty(ActiveCell.Offset(0, -1))
End Sub

Sub Clear_Cells()
ActiveCell.Offset(0, -5).Select
Selection.ClearContents
ActiveCell.Offset(0, 5).Select
End Sub

Barry Staples Nottingham England


---
Message posted from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Relative Cell References in Macros

Barry,

Sub test()
With ActiveCell
If .Column 1 Then
.Offset(0, -1).Resize(1, Columns.Count - .Column +
2).ClearContents
End If
End With
End Sub


Rob

"Barry Staples " wrote in
message ...
Relative cell References within a LOOP and IF Statement


I’m new to writing macros for Excel 97
I wish to clear a range of cells on the current line to the left of the
active cell.
I’m having trouble with setting up a “range” with relative cell
values.
The following routine works well to clear an individual cell,
How do I modify it to clear a range of cells.

Any help would be really appreciated


Sub Loop1()
' This loop runs down a column until there is nothing in the column on
the it’s immediate left
‘ Clears a cell 5 columns to left

Do
ActiveCell.Select
If ActiveCell.Offset(0, -1) = 0 Then Clear_Cells
ActiveCell.Offset(1, 0).Select
‘ gets next row
Loop Until IsEmpty(ActiveCell.Offset(0, -1))
End Sub

Sub Clear_Cells()
ActiveCell.Offset(0, -5).Select
Selection.ClearContents
ActiveCell.Offset(0, 5).Select
End Sub

Barry Staples Nottingham England


---
Message posted from http://www.ExcelForum.com/



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Relative Cell References in Macros

Thanks Tom,Darren and Rob
With your help I have sorted the problem.
Interesting to see the different solutions


---
Message posted from http://www.ExcelForum.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
Excel 2003 macros set relative cell references JPI Excel Discussion (Misc queries) 7 May 21st 07 05:08 AM
Relative (vs. Absolute) Cell References with macros Claude S. Weiller Excel Discussion (Misc queries) 1 August 3rd 06 08:56 PM
Relative Cell References within VBA code Jandy Excel Discussion (Misc queries) 2 April 21st 05 02:17 AM


All times are GMT +1. The time now is 10:09 PM.

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

About Us

"It's about Microsoft Excel"