Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views8 pages

Pib Easy To Learn Excel Class

The document provides instructions for creating a VBA macro in Excel to filter payment data by 'Cash', sum the visible amounts, and format the result. It also includes a second macro to reset the view by clearing filters and formatting. Additionally, it outlines steps to analyze the same data using a Pivot Table, including creating the table, inserting a chart, and sorting/filtering the data.

Uploaded by

paulikpeofficial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Pib Easy To Learn Excel Class

The document provides instructions for creating a VBA macro in Excel to filter payment data by 'Cash', sum the visible amounts, and format the result. It also includes a second macro to reset the view by clearing filters and formatting. Additionally, it outlines steps to analyze the same data using a Pivot Table, including creating the table, inserting a chart, and sorting/filtering the data.

Uploaded by

paulikpeofficial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PIB EASY TO LEARN EXCEL CLASS

(LESSON 5607)
CLIENT: BENIM DRINKS
THURSDAY 15TH MAY 2025

QUERY: Create Solution Using VBA Macro


I have an excel workbook with a sheet called Sheet1 that contains payments made by customers and
their payment methods. From column D row 3 till row 2000 is the amount paid, same in column E
which is the names of customers, column F is the payment mode.

There are 3 types of payment mode in the list

1. Cash

2. Advance

3. Bank Transfer

Create a macro button that after been clicked it sorts data based on payment mode cash and hides
every other data from column A to I

Then sum the visible rows in column D that contains only numerical values, put the summed-up
answer in column J row 1

The answer in j3 should be displayed in white colour bold font size 16 with a red background

ANSWER: Using VBA Macro


What the Macro below does?
1. Filters the data in Sheet1 based on "Cash" in column F.

2. Hides all other rows that don't match.

3. Sums the visible values in column D

4. Places the result in cell J1.

5. Formats J1 with:

o White font
o Bold

o Font size 16

o Red background

Sub FilterCashAndSum()

Dim ws As Worksheet

Dim lastRow As Long

Dim sumTotal As Double

Dim cell As Range

Set ws = ThisWorkbook.Sheets("Sheet1")

' Clear previous filters

If ws.AutoFilterMode Then ws.AutoFilterMode = False

' Find last row

lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row

' Apply filter for "Cash" in column F

With ws.Range("A2:I" & lastRow)

.AutoFilter Field:=6, Criteria1:="Cash"

End With

' Sum visible (filtered) values in column D (Amount Paid)

sumTotal = 0

For Each cell In ws.Range("D3:D" & lastRow).SpecialCells(xlCellTypeVisible)

If IsNumeric(cell.Value) Then

sumTotal = sumTotal + cell.Value

End If

Next cell
' Output result to J1

With ws.Range("J1")

.Value = sumTotal

.Font.Bold = True

.Font.Size = 16

.Font.Color = RGB(255, 255, 255) ' White

.Interior.Color = RGB(255, 0, 0) ' Red

End With

End Sub

How to Add the Macro to a Button


1. Press Alt + F11 to open the VBA Editor.

2. Insert a new module (Right-click VBAProject > Insert > Module).

3. Paste the code above.

4. Close the editor.

5. Go to Sheet1, insert a Button (Form Control) via:

o Developer tab > Insert > Button (Form Control)

6. Assign the macro FilterCashAndSum to that button.

CREATE A MACRO THAT UNDOS ALL


FORMULA & FORMATTING
What This Macro Does:
• Clears all filters on Sheet1.

• Resets row visibility (unhides all hidden rows).

• Clears formatting in column J1 (where we had applied the bold white font and red
background).
Sub ResetView()

Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Sheet1")

' Clear AutoFilter if it's active

If ws.AutoFilterMode Then ws.AutoFilterMode = False

' Unhide all rows

ws.Rows.Hidden = False

' Clear formatting in J1

With ws.Range("J1")

.ClearContents

.Font.Bold = False

.Font.Size = 11

.Font.ColorIndex = xlAutomatic

.Interior.ColorIndex = xlNone

End With

End Sub

EXPLAIN THE FORMULA


Here's a line-by-line explanation of the macro in clear, simple terms:

Macro Name
Sub FilterCashAndSum()

• Starts the macro. The name is FilterCashAndSum.


Declare Variables
Dim ws As Worksheet

Dim lastRow As Long

Dim sumTotal As Double

Dim cell As Range

• ws: Will store the worksheet you're working with.

• lastRow: Will hold the number of the last row with data in column D.

• sumTotal: Will hold the total sum of the visible values.

• cell: Used to loop through each cell in the range.

Set Target Worksheet


Set ws = ThisWorkbook.Sheets("Sheet1")

• Tells Excel to work on the sheet named "Sheet1".

Remove Any Existing Filters


If ws.AutoFilterMode Then ws.AutoFilterMode = False

• If filtering is already applied, it turns it off to start fresh.

Find the Last Used Row in Column D


lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row

• Finds the last row that contains data in column D by going up from the bottom.

Apply Filter to Show Only "Cash" Payments


With ws.Range("A2:I" & lastRow)

.AutoFilter Field:=6, Criteria1:="Cash"

End With

• Filters the range from A2 to column I (up to the last row).

• Only shows rows where column F (Field 6) contains "Cash".

Loop Through Visible Rows in Column D and Sum Them


sumTotal = 0

For Each cell In ws.Range("D3:D" & lastRow).SpecialCells(xlCellTypeVisible)

If IsNumeric(cell.Value) Then

sumTotal = sumTotal + cell.Value

End If

Next cell

• Starts total at zero.

• Loops through only visible (filtered) cells in column D.

• If the cell has a number, it adds it to sumTotal.

Put the Result in Cell J1 and Style It


With ws.Range("J1")

.Value = sumTotal ' Put the total in J1

.Font.Bold = True ' Make the font bold

.Font.Size = 16 ' Set font size to 16

.Font.Color = RGB(255, 255, 255) ' Set text color to white

.Interior.Color = RGB(255, 0, 0) ' Set background color to red

End With

Reference Search: Excel VBA Color & RGB pink code

End the Macro


End Sub

• Marks the end of the macro.


ANSWER: Using Pivot Table

Here's a step-by-step guide on how to analyse the same data using pivot table

1. Create a Pivot Table

2. Add a Pivot Chart

3. Sort and Filter the Data

1. Convert Your List to a Table

1. Click any cell inside your list.

2. Press Ctrl + T to create a table.

3. Make sure “My table has headers” is checked.

4. Click OK.

2. Insert a Pivot Table

1. Click any cell in your table.

2. Go to the Insert tab on the ribbon.

3. Click PivotTable.

4. In the dialog box:

o Make sure the correct table is selected.

o Choose where to place the PivotTable (new or existing sheet).

5. Click OK.

3. Build Your Pivot Table

1. A field list will appear on the right.

2. Drag fields (column names) into:

o Rows to group by that item (e.g., Customer Name).

o Values to summarize data (e.g., Amount).

o Columns if needed to break data across columns.

o Filters to filter the whole Pivot Table.


4. Insert a Pivot Chart

1. Click anywhere inside the Pivot Table.

2. Go to the PivotTable Analyze tab (or just "Analyze").

3. Click PivotChart.

4. Choose a chart type (e.g., Column, Bar, Pie).

5. Click OK.

5. Sort and Filter Data

• To Sort:

1. Click the dropdown arrow next to a row label in the PivotTable.

2. Choose Sort A to Z or Z to A.

• To Filter:

1. Use the dropdown arrows on row/column labels or filters area.

2. Check or uncheck the items you want to display.

You might also like