Anda di halaman 1dari 6

MASUKKAN DI "THIS WORKBOOK" Private Sub Workbook_BeforePrint(Cancel As Boolean) Dim wkSht As Worksheet For Each wkSht In Me.

Worksheets With wkSht.PageSetup .CenterFooter = "Copyright 2013 - Instituto da Boa Governacao" End With Next wkSht End Sub ---------------------------------Private Sub Workbook_BeforePrint(Cancel As Boolean) Dim wkSht As Worksheet For Each wkSht In Me.Worksheets With wkSht.PageSetup .CenterFooter = Me.Name 'MENULISKAN NAMA FILE DI FOOTER End With Next wkSht End Sub -------------------------------------Private Sub Workbook_BeforePrint(Cancel As Boolean) Dim ws as WorkSheet Set wkbktodo = ActiveWorkbook For Each ws In wkbktodo.Worksheets ws.PageSetup.LeftFooter = ActiveWorkbook.FullName Next End Sub ---------------------------Private Sub Workbook_BeforePrint(Cancel As Boolean) Dim wkSht As Worksheet For Each wkSht In Me.Worksheets With wkSht.PageSetup .CenterFooter = wksht.range("A1").value End With Next wkSht End Sub ---------------------------This example macro inserts a header/footer in every worksheet in the active work book. It also inserts the complete path to the workbook. Sub InsertHeaderFooter() ' inserts the same header/footer in all worksheets Dim ws As Worksheet Application.ScreenUpdating = False For Each ws In ActiveWorkbook.Worksheets Application.StatusBar = "Changing header/footer in " & ws.Name With ws.PageSetup .LeftHeader = "Company name" .CenterHeader = "Page &P of &N" .RightHeader = "Printed &D &T" .LeftFooter = "Path : " & ActiveWorkbook.Path .CenterFooter = "Workbook name &F" .RightFooter = "Sheet name &A" End With

Next ws Set ws = Nothing Application.StatusBar = False End Sub ------------------------------------Kode dibawah akan aktif dan mengganti warna background ketika sebuah sel atau ra nge dipilih, baik menggunakan klik mouse maupun menggunakan tombol navigasi (arr ow keys) yang terdapat di keyboard. Langkah sederhananya adalah sebagai berikut : 1. Buka jendela Microsoft Visual Basic jika belum 2. Pilih ThisWorkbook yang terdapat di jendela project sebelah kiri 3. Paste kode berikut: Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Ex cel.Range) Static OldRange As Range On Error Resume Next Target.Interior.ColorIndex = 6 ' warna kuning - rubah sesuai selera OldRange.Interior.ColorIndex = xlColorIndexNone Set OldRange = Target End Sub ---------------------------------Dalam tutorial sebelumnya telah dijelaskan cara membuat sebuah ranking atau peri ngkat untuk sebuah database, akan tetapi terdapat sedikit kesalahan dalam formula yang dimasukkan sehingga menampilkan ranking atau peringkat yang sama. =RANK(B2;B$2:B$8)+COUNTIF(B$2:B2;B2)-1 ------------------------------------------------BACKGROUN COLOR SELANG-SELING Tutorial Steps: For help on these steps, refer to the image above. Drag select cells A8 to F11 to highlight them. Click on the Home tab. Click on the Conditional Formatting icon on the ribbon to open the drop down men u. Choose New Rule option to open the New Formatting Rule dialog box. Click on the Use a Formula to determine which cells to format option from the li st at the top of the dialog box. Enter the following formula into the box below the Format values where this valu e is true option in the bottom half of the dialog box. Choose one from the formu la: =MOD(ROW(),2)=1 --> ODD Rows =MOD(ROW(),2)=0 --> EVEN Rows Click the Format button to open the Format Cells dialog box. Click the Fill tab to see the background color options.

Select the green shaded box that is second from the top in the color samples. Click OK twice to close the dialog box and return to the worksheet. Rows A9 to F9 and A11 to F11 should now be shaded with a light green background color.

-------------------------HIDE EMPTY ROWS Sub HideEmpties() Set r = ActiveSheet.UsedRange nLastRow = r.Rows.Count + r.Row - 1 nFirstRow = r.Row For n = nFirstRow To nLastRow If Application.WorksheetFunction.CountA(Rows(n)) = 0 Then Rows(n).EntireRow.Hidden = True End If Next End Sub ---------------------------Sub UnhideRows() Dim r As Range Set r = ActiveSheet.Columns("A:A") r.EntireRow.Hidden = False Set r = Nothing End Sub ---------------------------------------I have a similar question. I am working on a time sheet where the days are in c olumns. The day of the month is in row 1. Columns AE, AF and AG contain days 2 9, 30, and 31. I want to hide those columns base on whether or not they are nee ded for that month. I have a function for the number of days in the month, so t he condition is if the day of the month in the particular column is greater than the number of days in the month, then hide the column. I already have a condit ional format to turn the column black, but it looks horrible. I would rather hi de the column, but that is not an option in the conditional formating for cells. How do I modify the above macros to hide and unhide a colum or multiple columns? I would also need an override so that I can check the columns to see that they have been cleared of any data. If the macro hides the column, can I simply unh ide normally, or will the macro immediately run and close it again? The macro needs to run on various versions of Excel from 2002 to 2010 and on var ious versions of Windows from XP Pro 32 bit to 7 - 64 bit. Put the following Event macro in the worksheet code area: Private Sub Worksheet_Activate() Dim dayss As Integer dayss = Day(DateSerial(Year(Now()), Month(Now() + 1), 0)) Columns("AE:AG").EntireColumn.Hidden = False If dayss = 28 Then Columns("AE:AG").EntireColumn.Hidden = True ElseIf dayss = 29 Then Columns("AF:AG").EntireColumn.Hidden = True

ElseIf dayss = 30 Then Columns("AG:AG").EntireColumn.Hidden = True End If End Sub Whenever you activate the worksheet, the proper columns will be hidden. You can alway un-hide the columns manually to see data and make changes. Because it is worksheet code, it is very easy to install and automatic to use: 1. right-click the tab name near the bottom of the Excel window 2. select View Code - this brings up a VBE window 3. paste the stuff in and close the VBE window If you have any concerns, first try it on a trial worksheet. If you save the workbook, the macro will be saved with it. To remove the macro: 1. bring up the VBE windows as above 2. clear the code out 3. close the VBE window -----------------------------------------AUTO HIDE EMPTY ROWS & COLUMNS '<< Code for worksheet module >> Option Explicit Private Sub Worksheet_Activate() Dim HiddenRow&, RowRange As Range, RowRangeValue& '***************************** '< Set the 1st & last rows to be hidden > Const FirstRow As Long = 4 Const LastRow As Long = 20 '< Set the columns that may contain data > Const FirstCol As String = "B" Const LastCol As String = "G" '***************************** ActiveWindow.DisplayZeros = False Application.ScreenUpdating = False For HiddenRow = FirstRow To LastRow '(we're using columns B to G here) Set RowRange = Range(FirstCol & HiddenRow & _ ":" & LastCol & HiddenRow) 'sums the entries in cells in the RowRange RowRangeValue = Application.Sum(RowRange.Value) If RowRangeValue <> 0 Then 'there's something in this row - don't hide Rows(HiddenRow).EntireRow.Hidden = False Else 'there's nothing in this row yet - hide it Rows(HiddenRow).EntireRow.Hidden = True

End If Next HiddenRow Application.ScreenUpdating = True End Sub HOW TO USE: 1. Open an Excel workbook 2. Select Tools/Macro/Visual Basic Editor 3. In the VBE window, select Tools/Project Explorer 4. Select the worksheet module for the linked sheet 5. Copy and paste the code into this Module 6. Now select File/Close and Return To Microsoft Excel 7. Save your changes... 8. (You can select all and Right-click and then choose Unhide to view all) ---------------------------------------Option Explicit Option Compare Text Dim ws As Worksheet Const MaxUses As Long = 5 '<- change uses Const wsWarningSheet As String = "Splash" Private Sub Workbook_BeforeClose(Cancel As Boolean) 'hide all sheets except warning sheet For Each ws In ThisWorkbook.Sheets If ws.Name = wsWarningSheet Then ws.Visible = True Else ws.Visible = xlVeryHidden End If Next 'record opening in remote cell With Sheets(wsWarningSheet).Cells(Rows.Count, Columns.Count) .Value = .Value + 1 End With End Sub haktanekba981102 -----------------------------Sub advancefilter() ' ' advancefilter Macro ' ' Sheets("Sheet1").Range("A1:D6").AdvancedFilter Action:=xlFilterCopy, _ CriteriaRange:=Range("Sheet1!Criteria"), CopyToRange:=Range("A1"), Uniqu e _ :=False End Sub -------------------------------Sub advfilter2() '

' advfilter2 Macro ' ' Sheets("Sheet1").Range("A1:D6").AdvancedFilter Action:=xlFilterCopy, _ CriteriaRange:=Sheets("Sheet1").Range("H1:H2"), CopyToRange:=Range("F1") , _ Unique:=False End Sub

Anda mungkin juga menyukai