End-Down type function?

smashclash

Board Regular
Joined
Nov 24, 2003
Messages
126
Office Version
  1. 365
Platform
  1. Windows
I want to create a macro for a group of workbooks that will copy data in a cell and then paste that data down in an end down type style. I know how to write a macro to do this. The problem I will run into is when a workbook has only 1 row of data that needs to be changed. If I use the end down function when I have only 1 row of data it will copy a useless formula all the way down to the total at the bottom of the column. Is there a way around using an end-down function to do this?

Below is the code for a sheet with only 1 row: (an end-down function won't work here).

 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You should be able to incorporate this snippet of code into your macro. If there is only one row and it goes all the way to the bottom, if will come back up into the original cell
Code:
'   Capture current cell
    Dim myCurrentCell As Range
    Set myCurrentCell = ActiveCell
    
'   Go to last row
    ActiveCell.End(xlDown).Select
    
'   Check to see if in last row in Excel and go back to first cell, if necessary
    If ActiveCell.Row = 65536 Then myCurrentCell.Select
 
Upvote 0
jmiskey said:
You should be able to incorporate this snippet of code into your macro. If there is only one row and it goes all the way to the bottom, if will come back up into the original cell
Code:
'   Capture current cell
    Dim myCurrentCell As Range
    Set myCurrentCell = ActiveCell
    
'   Go to last row
    ActiveCell.End(xlDown).Select
    
'   Check to see if in last row in Excel and go back to first cell, if necessary
    If ActiveCell.Row = 65536 Then myCurrentCell.Select


I don't think that will work because I have a column total at the bottom of each column. The end down will never go to row 65536. Also, the number of Job Code rows will vary on every template. Some have 1 row, others could have 10.
 
Upvote 0
Very important details! Changes the complexion of the problem.

This following line will replace the previous. What this does is simply check the column in the next row for data. If there is, then it will do the down function. If there isn't, it assumes there is only one row of data, so it won't go anywhere.

Code:
'   Only do down function if the next column has data in it
    If Len(ActiveCell.Offset(1, 0)) > 0 Then ActiveCell.End(xlDown).Select

If my assumptions are incorrect, you will need to lay out the rules for determining what constitutes a new section.
 
Upvote 0
jmiskey said:
Very important details! Changes the complexion of the problem.

This following line will replace the previous. What this does is simply check the column in the next row for data. If there is, then it will do the down function. If there isn't, it assumes there is only one row of data, so it won't go anywhere.

Code:
'   Only do down function if the next column has data in it
    If Len(ActiveCell.Offset(1, 0)) > 0 Then ActiveCell.End(xlDown).Select

If my assumptions are incorrect, you will need to lay out the rules for determining what constitutes a new section.

I think we are on the right track now. However, there is a chance the cell could be 0 or less than 0. is there something I can put there to say "is not null" instead of the > 0?
 
Upvote 0
Code:
I think we are on the right track now. However, there is a chance the cell could be 0 or less than 0. is there something I can put there to say "is not null" instead of the > 0?

Look closely. I am NOT checking the value in the cell, I am checking the length. If the cell is blank, empty, etc. it will have a zero length. If it has the value of 0 in it, its length is 1.

Therefore, it should work as written.
 
Upvote 0
jmiskey said:
Code:
I think we are on the right track now. However, there is a chance the cell could be 0 or less than 0. is there something I can put there to say "is not null" instead of the > 0?

Look closely. I am NOT checking the value in the cell, I am checking the length. If the cell is blank, empty, etc. it will have a zero length. If it has the value of 0 in it, its length is 1.

Therefore, it should work as written.


:oops: my mistake. I'm going to put together a macro now and give it a shot. thanks for your help!
 
Upvote 0
Below is my current code. Everything works great when the worksheet has more than one row. However, I'm getting an error message '1004' when there is only 1 row. I guess I need to tell excel what to do if the cell below is null. Any clue what code i need to add?

Code:
    Range("E14").Select
    ActiveCell.FormulaR1C1 = _
        "=+jan!RC+feb!RC+mar!RC+apr!RC+may!RC+june!RC+july!RC+aug!RC+sept!RC+oct!RC+nov!RC+dec!RC"
    Range("E14").Select
    If Len(ActiveCell.Offset(1, 0)) > 0 Then ActiveCell.Copy
  
     
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Paste
  
  
    Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,756
Members
448,990
Latest member
Buzzlightyear

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top