need a macro explaining
I hate Power Programming like the code you supplied. Power programming dates
back to thje 1960's and shouldn't be used. In the 1960's and early 70's
computers were very small and to get programs to run you tried to make the
programs as small as possible.
The idea back then was to to make variable names small and to put all the
code on as few lines as possible. the code was hard to understand.
Good programming practices are to document code so theat it can be
maintiained. Use veriable names that are are descriptive. Break code into
multiple lines so it it easily understood by all people not just the person
who wrote the code. Don't use tricks that people won't understand. Add
error checking like the test below for "If not c is nothing".
Option Explicit
Sub Summary()
Dim ws As Worksheet
Dim ShtCount As Long
Dim ColCount As Long
Dim LastRow As Long
Dim Newrow As Long
Dim Data As Variant
Dim NewData As Variant
Dim c As Variant
'set ws to be the summary sheet
Set ws = Sheets("Summary")
'find last row of data in summary sheet
'Rows.Count is the last row in the worksheet
'Look in column A starting in the last row of worksheet
'search up (xlup) until data is found
LastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
'make New row the the first enpty cell in column A (row after LastRow)
Newrow = LastRow + 1
'search columns 1 t 7 on sheets 1 to 7
For ColCount = 1 To 7
For ShtCount = 1 To 7
'Get data in row 1 from summary sheet
Data = ws.Cells(1, ColCount)
'search for data in all the cells in the sheet specified
Set c = Sheets(ShtCount).Cells.Find(what:=Data, _
LookIn:=xlValues, lookat:=xlWhole)
'test if data is found
If Not c Is Nothing Then
'get the data in the cell directly below the data being search
for
NewData = c.Offset(rowoffset:=1, columnoffset:=0).Value
'Clear the cell where the data is being extracted from
c.Offset(rowoffset:=1, columnoffset:=0).ClearContents
'Put the data extract into the summary sheet
ws.Cells(Newrow, ColCount).Value = NewData
Newrow = Newrow + 1
End If
Next ShtCount
Next ColCount
End Sub
"project manager" wrote:
is it possible for someone to annotate this macro so i can understand what
its doing and try to learn from it.
Option Explicit
Sub Summary()
Dim ws As Worksheet
Dim i As Long, j As Long, rw As Long
Set ws = Sheets("Summary")
For j = 1 To 7
rw = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1
For i = 1 To 7
ws.Cells(rw, i).Value = Sheets(CStr(j)).Cells.Find(ws.Cells(1,
i)).Offset(1).Value
Sheets(CStr(j)).Cells.Find(ws.Cells(1, i)).Offset(1).ClearContents
Next i
Next j
End Sub
|