Using Autosum and recording a Macro

Jaysilva

New Member
Joined
Aug 29, 2015
Messages
3
hi,

Thanks for taking the time to read this. Basically im trying to create a Macro so that i dont waste time formatting statements produced by my in house system at work. The data that is produced by the in house system needs totals in them and borders where appropriate. So far i have created several Macros but the only problem i am having is when the amount of rowss of data varies. When i have more rows of data the borders either stop half way through the date and the autosums either miss bits out of leave hugh amounts of gaps. Any help with this is appreciated i am a Newbie to this.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
hi,

Thanks for taking the time to read this. Basically im trying to create a Macro so that i dont waste time formatting statements produced by my in house system at work. The data that is produced by the in house system needs totals in them and borders where appropriate. So far i have created several Macros but the only problem i am having is when the amount of rowss of data varies. When i have more rows of data the borders either stop half way through the date and the autosums either miss bits out of leave hugh amounts of gaps. Any help with this is appreciated i am a Newbie to this.
You can set a dynamic range to a variable and use that variable in the code to work with the range. There are several ways to do that, but they are all based on defining the last row of data for the range you want to work with. The simple solution is to assume you want your last row for the range to be the last row with anything at all in it, including a formula in a cell that might appear blank. To get that row number:
Code:
Dim lastRow As Long
lastRow = ActiveSheet.Cells.Find("*", , xlFormulas, xlPart, xlByRows, xlPrevious).Row
The above snippet uses the VBA Find function to look for any string value greater than blank, including formulas, beginning at the default top left cell of the used range and working backward from bottom to top, right to left. It will initialize the lastRow variable with an iteger representing the row number where the first value greater than blank is found. You can then use the variable to create your dynamic range:
Code:
Sub t()  
 Dim lastRow As Long
lastRow = ActiveSheet.Cells.Find("*", , xlFormulas, xlPart, xlByRows, xlPrevious).Row      
MsgBox ActiveSheet.Range("A2:D" & lastRow").Address
End Sub
You can try the short procedure on a sheet of data to see what the message box returns. But, be careful, becuase if your code adds or deletes row after you have initialized the variable, the variable may no long reflect the true last row number and could require resetting, depending on how you use it in the code. Yet, it will not require adjustment if rows are manually added or deleted between runs of the procedure.
 
Last edited:
Upvote 0
Please do not attempt to resolve your issues by private email. Any exchange of information or ideas related to this thread should be done by posting to this thread so that all participants have an opportunity to review, and if applicable, to comment.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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