Visual Basic

kyle_04

Board Regular
Joined
Sep 26, 2009
Messages
68
Hi,

I am trying to create a macro to insert a new row in a protected sheet,

I am trying to insert a match function into VB to find the last row on the worksheet and came accross ".worksheet function".

Now, I have seemed to get my head around it up until I insert some text (&":"&). The formula works because I have tried it in excel. Formula in VB as follows:

Rows.Application.WorksheetFunction.MATCH("1 end",worksheet(timesheet).range("A:A"),0).value-1&":"&WorksheetFunction.MATCH("1 end",worksheet(timesheet).range("A:A"),0).value-1.Select

On another note is there a easier way of finding a row number than using two matches i.e. so it returns 1:1 (For example), formula in excel as follows =(MATCH("1 end",A:A,0))-1&":"&(MATCH("1 end",A:A,0))-1

Many thanks,

Kyle
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I'm sorry, I must be having a senior moment because I'm not following what you're trying to do. Sorry!

If you want to know how many rows have been used in your worksheet, you could use something like:-
Code:
sheets(1).usedrange.rows.count

You may have to take account of where it starts if there are any blank rows at the top. The top row of the used range is:-
Code:
sheets(1).usedrange.row

The last used cell in column A would be:-
Code:
sheets(1).cells(sheets(1).rows.count,"a").end(xlup).row
 
Upvote 0
Try this:

Code:
Sub Test()
    With Worksheets("timesheet")
        Range("A" & Application.Match("1 end", .Columns(1), False)).EntireRow.Insert
    End With
End Sub
 
Upvote 0
I'm not the expert here, but you would need to use Worksheets("A NAME") rather than Worksheet(A NAME).

Also wouldn't you need spaces ).value-1 &":"&
 
Upvote 0
try:
to select:
Code:
Worksheets("timesheet").Columns(1).Find(What:="1 end", searchdirection:=xlPrevious, lookat:=xlWhole).Offset(-1).EntireRow.Select
or to insert a line too (a guess at where you want to insert):

Code:
Worksheets("timesheet").Columns(1).Find(What:="1 end", searchdirection:=xlPrevious, lookat:=xlWhole).EntireRow.Insert
these don't quite match your formula in that if there are two instance of '1 end' in column A, your version found the upper one, this finds the last one.

To change this to mirror your code, change xlPrevious instances to xlNext.
 
Last edited:
Upvote 0
Apologies my VB skills are not that great, and I seem to have forgotten most of it from college as I hardly use it.

In a nutshell I am trying to insert a copied line when the document is protected so the user cannot make errors etc. Therefore for the line to be inserted I need to find the last row, which in this case is 1 less than a cell containing "1 end" in column A:A.

I'll give you the whole VB to give you an idea of what Im trying to do:

Sub New_Line1()

Sheets("timesheet").Unprotect "password"
'
' New_Line1 Macro
'

'
Range("A1").Select
Cells.Find(What:="1 end", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
Rows.Application.WorksheetFunction.MATCH("1 end",worksheet("timesheet").range("A:A"),0).value-1 &":"&WorksheetFunction.MATCH("1 end",worksheet("timesheet").range("A:A"),0).value-1.Select
Selection.Copy
Rows("30:30").Select
Selection.Insert Shift:=xlDown


Sheets("timesheet").Protect "password"
End Sub


Trevor I have now amended to Worksheets("A NAME") and the inserted the space, this seems to get it to the end of the formula but still shows " compile error"

Regards,

Kyle
 
Upvote 0
I still see small errors here

Rows.Application.WorksheetFunction.MATCH("1 end",worksheet("timesheet").range("A:A"),0).value-1 &":"&WorksheetFunction.MATCH("1 end",worksheet("timesheet").range("A:A"),0).value-1.Select

Would be

Rows.Application.WorksheetFunction.MATCH("1 end",worksheets("timesheet").range("A:A"),0).value-1 &":"&WorksheetFunction.MATCH("1 end",worksheets("timesheet").range("A:A"),0).value-1.Select
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,462
Members
452,915
Latest member
hannnahheileen

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