Sorry guys but I am stuck again. In the middle of a macro and need to be able to place the cursor at the end of a range which will not be constant - any help greatly received. Poss PX accounting advice
- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Excel Experts - Stuck Again
Collapse
X
-
Excel Experts - Stuck Again
-
Have a look at "ActiveCell" you can set it as well as read it.
You can use the Row and Column property of a Range object. For
example, the following code will print the results to the Immediate
Window.
'This is for the ActiveCell
Debug.Print ActiveCell.Row
Debug.Print ActiveCell.Column
'This is for a specified range location
Debug.Print Range("AA5").Row
Debug.Print Range("AA5").ColumnAlways forgive your enemies; nothing annoys them so much. -
When you say a range, do you mean a named range on the sheet, or the last row/col of the current used range?
If it's the named range you could trap the row and column values using
Row = Range("Range Name").rows.count + range("Range Name").row - 1
Same for column
If its the last used row/col of the sheet then
activesheet.usedrange.rows.count should do the trick.
If all you are doing is trying to find the last row I tend to use
Cells.Find("*",SearchOrder:=xlByRows,SearchDirecti on:=xlPrevious).RowRule Number 1 - Assuming that you have a valid contract in place always try to get your poo onto your timesheet, provided that the timesheet is valid for your current contract and covers the period of time that you are billing for.
I preferred version 1!Comment
-
Not an expert but try
Range("A" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).Selectmerely at clientco for the entertainmentComment
-
Thanks for coming back so quickly guys - think a bit more info will help. Two reports are run each week which will have the same number of columns and a different number of rows - both reports are then exported into Excel and fiddled about with until they are in the format that we need.I then need to amalgamate the data on both reports so that the whole lot can be sorted. I want to do all this with a macro as it is a regular excercise and time consuming. Trouble is I can't find a way of automatically directing the cursor to the last row of the first report so that I can copy and paste the data from the second report directly beneath it so that it can be sorted.
Although I am happy to record macros my knowledge of the code is slightly less than would fit on the average size postage stamp but if I have understood correctly the solutions so far would identify the last cell but not take the cursor thereComment
-
Comment
-
Do this
set a variable for your row number
dim lLastRow as long
use this to determine the last used row
lLastRow = Cells.Find("*",SearchOrder:=xlByRows,SearchDirecti on:=xlPrevious).Row
You can then use this value in any range selection thing
eg range(cells(2,1),cells(llastrow,5)).select
would select cells(2,1) = A_2 to cells(llastrow,5) = e_LastRowRule Number 1 - Assuming that you have a valid contract in place always try to get your poo onto your timesheet, provided that the timesheet is valid for your current contract and covers the period of time that you are billing for.
I preferred version 1!Comment
-
Thanks Tony - this is really useful. Think I am getting there but am having to write the code rather than recording now and it is tricky I have the macro placing the cursor in the correct cell and I have highlighted the area I need from the second worksheet. Trouble is I now don't know how to write the code that will paste it in the area which starts 1 cell down from the last active cell.Comment
-
It gets difficult selecting the cells outside the range as logic is slightly backwards.
From memory its
Range("A" & Cells(Cells.Rows.Count, "A").End(xlUp).Row +1).Select
but its probably better to do something like this as its clearer
NewCell= Cells(Cells.Rows.Count, "A").End(xlUp).Row +1
Range("A" & NewCell).Select
as that is clearer. mind you vba is as clear as mud most of the time.
ActiveSheet.Paste would then paste the contents of the clipboard into the spreadsheet.merely at clientco for the entertainmentComment
-
Hi Eek
Thanks for that - we are now ending up in the right place Trouble is the paste is not working - possibly as the data is on a different worksheet. Macro as it stands at the moment is:
Sub Macro1()
'
' Macro1 Macro
'
'
Columns("F:F").Select
Selection.Cut Destination:=Columns("N:N")
Range("F1").Select
ActiveCell.FormulaR1C1 = "=RIGHT(RC[8],LEN(RC[8])-2)"
Range("F1").Select
Selection.AutoFill Destination:=Range("F1:F1015"), Type:=xlFillDefault
Range("F1:F1015").Select
Range("A1").Select
Sheets("Daybook Invoices").Select
Range("A" & Cells(Cells.Rows.Count, "A").End(xlUp).Row).Select
NewCell = Cells(Cells.Rows.Count, "A").End(xlUp).Row + 1
Range("A" & NewCell).Select
Sheets("Daybook Credits").Select
Range("A1:M1015").Select
ActiveSheet.Paste
End Sub
Comes up with a runtime error 1004 - Paste method of Worksheet class failed. Tricky little blighter!
Any suggestions gratefully received.Comment
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- Five tax return mistakes contractors will make any day now… Today 09:27
- Experts you can trust to deliver UK and global solutions tailored to your needs! Yesterday 15:10
- Business & Personal Protection for Contractors Yesterday 13:58
- ‘Four interest rate cuts in 2025’ not echoed by contractor advisers Yesterday 08:24
- ‘Why Should We Hire You?’ How to answer as an IT contractor Jan 7 09:30
- Even IT contractors connect with 'New Year, New Job.' But… Jan 6 09:28
- Which IT contractor skills will be top five in 2025? Jan 2 09:08
- Secondary NI threshold sinking to £5,000: a limited company director’s explainer Dec 24 09:51
- Reeves sets Spring Statement 2025 for March 26th Dec 23 09:18
- Spot the hidden contractor Dec 20 10:43
Comment