Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Ant
 
Posts: n/a
Default Defining an Activecell

Each month I add new data to an existing sheet. I then want to run a macro to
insert a new column and a formula. Trouble is this data increases rows each
month and I need to be able to identify the final row each time so I can copy
the formula down. ie in Jan the data finished at row 100. In Feb it finishes
at row 200. I need the macro to be able to know to copy the formula down to
row 200 in Feb for example.
  #2   Report Post  
Ron de Bruin
 
Posts: n/a
Default

Hi Ant

You can use this function fir finding the last row with data on thye worksheet

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function

Use this in your code then

Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1


If you can check one column then use this
Lr = Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Each month I add new data to an existing sheet. I then want to run a macro to
insert a new column and a formula. Trouble is this data increases rows each
month and I need to be able to identify the final row each time so I can copy
the formula down. ie in Jan the data finished at row 100. In Feb it finishes
at row 200. I need the macro to be able to know to copy the formula down to
row 200 in Feb for example.



  #3   Report Post  
Ant
 
Posts: n/a
Default

Thanks Ron. I copied this into VBA however it debugged stating that it
expected an end sub after the Sub Macro()??

Also, could I do something a bit more simple like using xldown then that
cell = activecell. Then my formula can copy down to range(activecell)?

"Ron de Bruin" wrote:

Hi Ant

You can use this function fir finding the last row with data on thye worksheet

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function

Use this in your code then

Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1


If you can check one column then use this
Lr = Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Each month I add new data to an existing sheet. I then want to run a macro to
insert a new column and a formula. Trouble is this data increases rows each
month and I need to be able to identify the final row each time so I can copy
the formula down. ie in Jan the data finished at row 100. In Feb it finishes
at row 200. I need the macro to be able to know to copy the formula down to
row 200 in Feb for example.




  #4   Report Post  
Ron de Bruin
 
Posts: n/a
Default

Copy the in a normal module
Run the sub test then

Sub test()
Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1
MsgBox Lr
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function


Or without the function

Sub test2()
Dim Lr As Long
Lr = Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
MsgBox Lr
End Sub


You can use Cells now like this to build a range
Cells(Lr, "B") for example



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Thanks Ron. I copied this into VBA however it debugged stating that it
expected an end sub after the Sub Macro()??

Also, could I do something a bit more simple like using xldown then that
cell = activecell. Then my formula can copy down to range(activecell)?

"Ron de Bruin" wrote:

Hi Ant

You can use this function fir finding the last row with data on thye worksheet

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function

Use this in your code then

Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1


If you can check one column then use this
Lr = Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Each month I add new data to an existing sheet. I then want to run a macro to
insert a new column and a formula. Trouble is this data increases rows each
month and I need to be able to identify the final row each time so I can copy
the formula down. ie in Jan the data finished at row 100. In Feb it finishes
at row 200. I need the macro to be able to know to copy the formula down to
row 200 in Feb for example.






  #5   Report Post  
Ant
 
Posts: n/a
Default

Thanks again Ron. Very useful. In the end I used:

Sub EndCell()
Dim Lr As Long
Lr = Sheets("SAP (2)").Range("A" & Rows.Count).End(xlUp).Offset(0, 0).Row
Range("C2").Select
Selection.Copy
Cells(Lr, "C").Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False

End Sub

This copied the formula in C2 from the last row containing data up to C2 as
hoped.

Thanks again.

"Ron de Bruin" wrote:

Copy the in a normal module
Run the sub test then

Sub test()
Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1
MsgBox Lr
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function


Or without the function

Sub test2()
Dim Lr As Long
Lr = Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
MsgBox Lr
End Sub


You can use Cells now like this to build a range
Cells(Lr, "B") for example



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Thanks Ron. I copied this into VBA however it debugged stating that it
expected an end sub after the Sub Macro()??

Also, could I do something a bit more simple like using xldown then that
cell = activecell. Then my formula can copy down to range(activecell)?

"Ron de Bruin" wrote:

Hi Ant

You can use this function fir finding the last row with data on thye worksheet

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function

Use this in your code then

Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1


If you can check one column then use this
Lr = Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Each month I add new data to an existing sheet. I then want to run a macro to
insert a new column and a formula. Trouble is this data increases rows each
month and I need to be able to identify the final row each time so I can copy
the formula down. ie in Jan the data finished at row 100. In Feb it finishes
at row 200. I need the macro to be able to know to copy the formula down to
row 200 in Feb for example.








  #6   Report Post  
Ron de Bruin
 
Posts: n/a
Default

Another way without selecting

Sub test()
Dim LastRow As Long
With Sheets("SAP (2)")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("C2").AutoFill Destination:=.Range("C2:C" & LastRow) _
, Type:=xlFillDefault
End With
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Thanks again Ron. Very useful. In the end I used:

Sub EndCell()
Dim Lr As Long
Lr = Sheets("SAP (2)").Range("A" & Rows.Count).End(xlUp).Offset(0, 0).Row
Range("C2").Select
Selection.Copy
Cells(Lr, "C").Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False

End Sub

This copied the formula in C2 from the last row containing data up to C2 as
hoped.

Thanks again.

"Ron de Bruin" wrote:

Copy the in a normal module
Run the sub test then

Sub test()
Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1
MsgBox Lr
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function


Or without the function

Sub test2()
Dim Lr As Long
Lr = Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
MsgBox Lr
End Sub


You can use Cells now like this to build a range
Cells(Lr, "B") for example



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Thanks Ron. I copied this into VBA however it debugged stating that it
expected an end sub after the Sub Macro()??

Also, could I do something a bit more simple like using xldown then that
cell = activecell. Then my formula can copy down to range(activecell)?

"Ron de Bruin" wrote:

Hi Ant

You can use this function fir finding the last row with data on thye worksheet

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function

Use this in your code then

Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1


If you can check one column then use this
Lr = Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Each month I add new data to an existing sheet. I then want to run a macro to
insert a new column and a formula. Trouble is this data increases rows each
month and I need to be able to identify the final row each time so I can copy
the formula down. ie in Jan the data finished at row 100. In Feb it finishes
at row 200. I need the macro to be able to know to copy the formula down to
row 200 in Feb for example.








  #7   Report Post  
Ant
 
Posts: n/a
Default

That's even better. Exactly what I was after. Many thanks.

"Ron de Bruin" wrote:

Another way without selecting

Sub test()
Dim LastRow As Long
With Sheets("SAP (2)")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("C2").AutoFill Destination:=.Range("C2:C" & LastRow) _
, Type:=xlFillDefault
End With
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Thanks again Ron. Very useful. In the end I used:

Sub EndCell()
Dim Lr As Long
Lr = Sheets("SAP (2)").Range("A" & Rows.Count).End(xlUp).Offset(0, 0).Row
Range("C2").Select
Selection.Copy
Cells(Lr, "C").Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False

End Sub

This copied the formula in C2 from the last row containing data up to C2 as
hoped.

Thanks again.

"Ron de Bruin" wrote:

Copy the in a normal module
Run the sub test then

Sub test()
Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1
MsgBox Lr
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function


Or without the function

Sub test2()
Dim Lr As Long
Lr = Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
MsgBox Lr
End Sub


You can use Cells now like this to build a range
Cells(Lr, "B") for example



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Thanks Ron. I copied this into VBA however it debugged stating that it
expected an end sub after the Sub Macro()??

Also, could I do something a bit more simple like using xldown then that
cell = activecell. Then my formula can copy down to range(activecell)?

"Ron de Bruin" wrote:

Hi Ant

You can use this function fir finding the last row with data on thye worksheet

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function

Use this in your code then

Dim Lr As Long
Lr = LastRow(Sheets("Sheet2")) + 1


If you can check one column then use this
Lr = Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Ant" wrote in message ...
Each month I add new data to an existing sheet. I then want to run a macro to
insert a new column and a formula. Trouble is this data increases rows each
month and I need to be able to identify the final row each time so I can copy
the formula down. ie in Jan the data finished at row 100. In Feb it finishes
at row 200. I need the macro to be able to know to copy the formula down to
row 200 in Feb for example.









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
ActiveCell giving blank value itstome New Users to Excel 1 August 30th 05 11:01 PM
ActiveCell giving blank value in vbscript itstome Excel Worksheet Functions 1 August 30th 05 04:24 PM
Defining Matt Excel Discussion (Misc queries) 1 July 26th 05 08:13 PM
Defining a cell to name file TimlmiT Excel Discussion (Misc queries) 1 May 24th 05 04:30 PM
Defining a number in a cell by text then subtracting it by the tex Crowraine Excel Worksheet Functions 1 December 16th 04 07:49 AM


All times are GMT +1. The time now is 05:34 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"