Vous êtes sur la page 1sur 4

Add a Chart

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.

Next VLOOKUP =INDEX(B:B,MATCH(F6,OFFSET(A1,MATCH(F6,A1:A999,0),0,999,1),0)+M ATCH(F6,A1:A999,0))

The On Error Statement


The heart of error handling in VBA is the On Error statement. This statement instructs VBA what to do when an run time error is encountered. The On Error statement takes three forms. On Error Goto 0 On Error Resume Next On Error Goto <label>:
Conditional Formatting :

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

Crack Sheet Protection Password


This routine provides a password to unprotect your worksheet. However, it may not give you the original password that was used. Open the workbook that has the protected sheet in it. Hit Alt+F11 to view the Visual Basic Editor. Hit Insert-Module and paste this code into the right-hand code window: Sub PasswordBreaker() 'Author unknown but submitted by brettdj of www.experts-exchange.com Dim i As Integer, j As Integer, k As Integer Dim l As Integer, m As Integer, n As Integer Dim i1 As Integer, i2 As Integer, i3 As Integer Dim i4 As Integer, i5 As Integer, i6 As Integer On Error Resume Next For i = 65 To 66: For j = 65 To 66: For k = 65 To 66 For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66 For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66 For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126

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

Vous aimerez peut-être aussi