Creating functions

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have written the following code in a module: (essentially all it does is
find the next blank line for me to use)

Public Function SR()

Dim row, col As Integer

col = 4
row = 7

While Worksheets("Main").Cells(row, col).Value <> ""
row = row + 1
Wend

End Function

Can someone please tell me:

- is this the right place to put it (it does seem to work)
- how I return the value back to my calling private sub (Button click)

Cheer

Steve
 
Public Function SR()

Dim row, col As Integer

col = 4
row = 7

While Worksheets("Main").Cells(row, col).Value <> ""
row = row + 1
Wend
sr = row
End Function

Public Sub Main()
Dim lastrow as Long
lastrow = Sr
msgbox LastRow
End sub

Depending on how your data is laid out, You might change SR to

Public Function Sr()
Dim rng as Range
set rng = Worksheets("Main").Cells(rows.count,4).End(xlup)(2)
if rng.row < 7 then
set rng = Worksheets("Main").Range("D7")
end if
sr = rng.row
End Function
 
Spot on

Many thanks

Tom Ogilvy said:
Public Function SR()

Dim row, col As Integer

col = 4
row = 7

While Worksheets("Main").Cells(row, col).Value <> ""
row = row + 1
Wend
sr = row
End Function

Public Sub Main()
Dim lastrow as Long
lastrow = Sr
msgbox LastRow
End sub

Depending on how your data is laid out, You might change SR to

Public Function Sr()
Dim rng as Range
set rng = Worksheets("Main").Cells(rows.count,4).End(xlup)(2)
if rng.row < 7 then
set rng = Worksheets("Main").Range("D7")
end if
sr = rng.row
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top