Académique Documents
Professionnel Documents
Culture Documents
When you record a macro to add a chart object to a worksheet, Excel comes up with the following code:
Sub RecordedAddChartObject() ' ' RecordedAddChartObject Macro ' Macro recorded 5/2/02 by Jon Peltier ' Charts.Add ActiveChart.ChartType = xlXYScatterLines ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A3:G14") ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1" End Sub
Excel uses Charts.Add followed later by ActiveChart.Location to create a chart object, and uses ActiveChart.SetSourceData to set all the series data in one shot. The coding is efficient in terms of the small length of the code, but inflexxible in terms of your control over the output. In my examples I use ChartObjects.Add, which also requires (or allows) me to state the position and size of the chart. This example does almost exactly what the recorded macro above does:
Sub AddChartObject() ' With ActiveSheet.ChartObjects.Add _ (Left:=100, Width:=375, Top:=75, Height:=225) .Chart.SetSourceData Source:=Sheets("Sheet1").Range("A3:G14") .Chart.ChartType = xlXYScatterLines End With End Sub
PIVOT
Sub MakeTable() Dim Pt As PivotTable Dim strField As String 'Pass heading to a String variable strField = Selection.Cells(1, 1).Text 'Name the list range Range(Selection, Selection.End(xlDown)).Name = "Items" 'Create the Pivot Table based off our named list range. 'TableDestination:="" will force it onto a new sheet ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _ SourceData:="=Items").CreatePivotTable TableDestination:="", _ TableName:="ItemList" 'Set a Pivot Table variable to our new Pivot Table Set Pt = ActiveSheet.PivotTables("ItemList")
'Place the Pivot Table to Start from A3 on the new sheet ActiveSheet.PivotTableWizard TableDestination:=Cells(3, 1) 'Move the list heading to the Row Field Pt.AddFields RowFields:=strField 'Move the list heading to the Data Field Pt.PivotFields(strField).Orientation = xlDataField End Sub
VLOOKUP LOOKUP
VLOOKUP can find an exact match on unsorted data or an approximate match on sorted data, but LOOKUP does not handle unsorted data, and VLOOKUP always works vertically whereas LOOKUP automatically decides whether to work vertically or horizontally. The V in VLOOKUP stands for 'Vertical,' which implies that it will work only on columns, where LOOKUP can scan columns or rows. Note also that LOOKUP is restricted to a single column or row, not a range, as VLOOKUP (or HLOOKUP for rows) can span.
lLow = 90000 lHigh = 100000 Set rng = Range("K8:K207") rng.FormatConditions.Delete ' delete any pre-existing formatting ' add greater than condition With rng.FormatConditions.Add(Type:=xlCellValue, Operator:=xlGreater, Formula1:="=" & lHigh) .Interior.Color = rgbLimeGreen End With
' add middle condition With rng.FormatConditions.Add(Type:=xlCellValue, Operator:=xlBetween, Formula1:="=" & lLow, Formula2:="=" & lHigh) .Interior.Color = rgbGold End With ' add less than condition With rng.FormatConditions.Add(Type:=xlCellValue, Operator:=xlLess, Formula1:="=" & lLow) .Interior.Color = rgbRed End With
ActiveSheet.Unprotect Chr(i) & Chr(j) & Chr(k) & _ Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _ Chr(i4) & Chr(i5) & Chr(i6) & Chr(n) If ActiveSheet.ProtectContents = False Then MsgBox "One usable password is " & Chr(i) & Chr(j) & _
Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _ Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n) ActiveWorkbook.Sheets(1).Select Range("a1").FormulaR1C1 = Chr(i) & Chr(j) & _ Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _ Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n) Exit Sub End If Next: Next: Next: Next: Next: Next Next: Next: Next: Next: Next: Next End Sub