'splain the code please :)

M

mike

Hello, thank you for helping in advance..
i have a couple of macros that was i was working on and
was helped ( alot).. i am currently reading a excel step
by step book by reed jacobson and i am learning the term
adn properties as i go along here. Will anyone be willing
to decipher and explain each statement in "idiot" terms
so i can see how it actually worked and understand it? i
find it fascinating to do macros. I would be thrilled if
anyone can break these down.
again thanks you for helping. mike

Sub copy_to_sheet()
copy_to = "Month" & Range("month_val").Value
last_used = Sheets(copy_to).Cells.SpecialCells _
(xlCellTypeLastCell).Address
copy_to_address = Rows(Sheets(copy_to).Range _
(last_used).Row + 2).Columns(1).Address
If copy_to_address = "$A$3" Then copy_to_address = "$A$1"
Sheets("form").Range("form_data").Copy Destination:=Sheets
(copy_to).Range(copy_to_address)


End Sub


and...

Private Sub Worksheet_Change(ByVal Target As Range)

this_month = 1
dd = Range("start_date").Value
Do While dd < Range("ref_date").Value
dd = 1 + dd
If Day(dd) = 25 Then this_month = 1 + this_month
Loop
Range("month_val").Value = this_month
End Sub

**what did the argument in paratheses mean? "(ByVal
Target As Range)" and "this_month = 1 ..What is
this "statement" telling Excel?


thanks!!!
 
N

Nigel

(by Value Target As Range) is a parameter passed to your procedure as part
of the Worksheet Change event.

'this_month' is a variable used by your procedures, its name is decriptive
only as defined by the author of the code.
 
T

Tom Ogilvy

Sub copy_to_sheet()
' construct a string made up of "Month" and concatenate the value in a cell
' named "month_val". the string is held in a variable named copy_to
' this string will represent the name of a sheet
copy_to = "Month" & Range("month_val").Value

' get the address of the last used cell on the worksheets
' place it in a variable last_used. Use the specialcells method
' to get this. The argument to tell specialcells to provide this
' information is the defined constants xlCellTypeLastCell
' special cells can provide other information if provided with
' a different argument. Use of this command is equivalent to
' manually doing Edit=>Goto=>Special and choosing
' last cell
' in code, it returns a cell reference, and we put address
' on the end to get the address of the referenced cell.
' we find this information for the sheet whose name is
' held in the copy_to variable.
last_used = Sheets(copy_to).Cells.SpecialCells _
(xlCellTypeLastCell).Address

'Now build a string that will hold the address we want to
' copy to. We use our copy_to variable determine the
' sheet to copy to. We will use the lastused cell in that
' and go down 2 additional rows, then over to the first column
' so our address would be something like $A$29
copy_to_address = Rows(Sheets(copy_to).Range _
(last_used).Row + 2).Columns(1).Address

' if the address is $A$3, then this means the sheet is empty
' so we want to copy to cell $A$1 instead
If copy_to_address = "$A$3" Then copy_to_address = "$A$1"

'Now we do the copy using the information we have gathered.
'We copy the information found in the Sheet named FORM at
'the range location defined by "form_data" (a defined name)
'and copy to the cell we previously identified.
Sheets("form").Range("form_data").Copy Destination:=Sheets
(copy_to).Range(copy_to_address)


End Sub
 

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

Top