How to Create a Form for Search and Print through VBA in Microsoft Excel?
Tags: tocreateaformforsear, vbamacros
Published: 2021-09-15 02:18:55

---
title: How to Create a Form for Search and Print through VBA in Microsoft Excel?
published: true
date: 2021-09-14 13:30:55 UTC
tags: ToCreateAFormForSear,VBAMacros
canonical_url: https://geekexcel.com/how-to-create-a-form-for-search-and-print-through-vba-in-microsoft-excel/
---

In this tutorial, we will guide you on how to **create a form for search and print** through VBA in Microsoft Excel. Let’s get them below!! Get an official version of MS Excel from the following link: **[https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)**

## Example

- Firstly, you need to **create a data**  **in Sheet 1** with **Agent ID,**  **Name** , **Address** , **City** , **Region** , **Country** and **Postal zip**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Sample-data-10-1024x383.png)<figcaption>Sample data</figcaption>

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- You have to **insert a module** to write the code for **search button** in the VBA.
- Now, you have to  **copy and paste**  the following code.

```

Sub Searchdata()
Dim Lastrow As Long
Dim count As Integer

Lastrow = Sheets("Data").Cells(Rows.count, 1).End(xlUp).Row

    For X = 2 To Lastrow
        If Sheets("Data").Cells(X, 1) = Sheet3.Range("B3") Then
            Sheet3.Range("A11") = Sheets("Data").Cells(X, 1)
            Sheet3.Range("B11") = Sheets("Data").Cells(X, 2)
            Sheet3.Range("C11") = Sheets("Data").Cells(X, 3) & " " & Sheets("data").Cells(X, 4) _
             & " " & Sheets("data").Cells(X, 5) & " " & Sheets("Data").Cells(X, 6)
            Sheet3.Range("D11") = Sheets("Data").Cells(X, 7)
        End If
    Next X
End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-38-1024x414.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://geekexcel.com/wp-content/uploads/2021/09/Run-the-Code-19.png)<figcaption>Run the Code</figcaption>

- Finally, you will get  **output ** as given below in the  **Microsoft Excel**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Output-12.png)<figcaption>Output</figcaption>

**To give the printout, assign the below-mentioned macro:**

- In Excel Worksheet, you need to ** ** click on the  **Insert ** icon and select  **Button ** option.
- Now, you have to write the **macro** in the **same model** where we have written the **macro for Searchdata** , with the **subject name of Printout**.

```

Sub PrintOut()    
    Sheet3.Range("A1:D12").PrintPreview
    Sheet3.Range("A1:D12").PrintOut
End Sub

```

- Assign the macro to button, as we have assigned to search data.

## End of Era

We hope that this tutorial gives you guidelines on how to **create a form for search and print** through VBA in Microsoft Excel **. ** Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Read Next:**

- **[How to use the XNPV function in Excel office 365?](https://geekexcel.com/how-to-use-the-xnpv-function-in-excel-office-365/)**
- **[Excel Formulas to Highlight the Cells that End with Given Value!!](https://geekexcel.com/excel-formulas-to-highlight-cells-end-with/)**
- **[Shortcut for “Select to End of Cell” in the Worksheet!!](https://geekexcel.com/quick-excel-shortcut-for-select-to-end-of-cell-in-a-worksheet/)**
- **[Excel Shortcuts: Delete to the End of Line in the Spreadsheet!!](https://geekexcel.com/shortcuts-for-delete-to-the-end-of-line-in-ms-excel-365/)**
- **[Formulas for Counting Cells that End with certain character or text!!](https://geekexcel.com/excel-formulas-for-counting-cells-that-end-with-certain-character-or-text/)**