This is a short step-by-step tutorial for beginners showing how to add VBA code (Visual Basic for Applications code) to your Excel workbook and run this macro to solve your spreadsheet tasks.
Most people like me and you are not real Microsoft Office gurus. So, we may not know all specificities of calling this or that option, and we cannot tell the difference between VBA execution speed in different Excel versions. We use Excel as a tool for processing our applied data.
Suppose you need to change your data in some way. You googled a lot and found a VBA macro that solves your task. However, your knowledge of VBA leaves much to be desired. Feel free to study this step-by-step guide to be able to use the code you found:
Insert VBA code to Excel Workbook
For this example, we are going to use a VBA macro to remove line breaks from the current worksheet.
- Open your workbook in Excel.
- Press Alt + F11 to open Visual Basic Editor (VBE).
- Right-click on your workbook name in the "Project-VBAProject" pane (at the top left corner of the editor window) and select Insert -> Module from the context menu.
- Copy the VBA code (from a web-page etc.) and paste it to the right pane of the VBA editor ("Module1" window).
- Tip: Speed up macro execution
If the code of your VBA macro does not contain the following lines in the beginning:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManualThen add the following lines to get your macro to work faster (see the screenshots above):
- To the very beginning of the code, after all code lines that start with Dim (if there are no "Dim" lines, then add them right after the Sub line):
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual - To the very of the code, before End Sub:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
These lines, as their names suggest, turn off screen refresh and recalculating the workbook's formulas before running the macro.
After the code is executed, everything is turned back on. As a result, the performance is increased from 10% to 500% (aha, the macro works 5 times faster if it continuously manipulates the cells' contents).
- To the very beginning of the code, after all code lines that start with Dim (if there are no "Dim" lines, then add them right after the Sub line):
- Save your workbook as "Excel macro-enabled workbook".
Press Crl + S, then click the "No" button in the "The following features cannot be saved in macro-free workbook" warning dialog.
The "Save as" dialog will open. Choose "Excel macro-enabled workbook" from the "Save as type" drop-down list and click the Save button.
- Press Alt + Q to close the Editor window and switch back to your workbook.
How to run VBA macros in Excel
When you want to run the VBA code that you added as described in the section above: press Alt+F8 to open the "Macro" dialog.
Then select the wanted macro from the "Macro Name" list and click the "Run" button.
256 comments
Hi Team,
I would like to inquire about a specific task. In a particular Excel sheet, there exists a dataset. If the data includes the number 4146, it should be transferred to Sheet1. If the data contains the number 4148, it must be moved to Sheet2. Additionally, if the data features the number 4145, it should be relocated to Sheet3.
How to write formula for this??
Thank you in advanc3
Hello Komala!
For each of your three worksheets, you can extract data that matches a specific condition using the FILTER formula.
You can find the examples and detailed instructions here: Excel FILTER function - dynamic filtering with formulas.
Your instructions for loading and running VBS code was excellent. Thank you so much.
Is there a way to write a Macro that will unfilter a text.
I want to filter out my manager's name, delete the rest of the items, then clear filter to create an entire sheet of just my manager's items.
As it is recorded, the macro will select every other name, but every week the names change. Is there a syntax for "Filter does not equal 'name'?"
Hi! We do not do VBA code creation on request.
To filter data by condition, you can use the FILTER function. You can find the examples and detailed instructions here: Excel FILTER function - dynamic filtering with formulas.
hello dears ,
hope you're all doing well and safe , im very keen to know how can i generate VBA script to make my daily tasks easy please
i have an excel file generated from a specific app ( PPS ) , this excel show us where the shortages of products are ,
so we need to filter manual and do some formulas to know where first shortage is
can someone guide me and explain to me how to create VBA easy please ,
PS : i want to share with you the excel file but i didnt see any uploading button here ,
greetings,
Z.E
Hi! Your request goes beyond the advice we provide on this blog. We do not do VBA code creation on request.
Hi Alex ,
could you please guide me more or provide me an email box to share with you excel file for more clarification & understanding ,
thanks in advance Dear
Thank you
You say this is for beginners, but you use language that is for more experienced users. If it is for beginners, you need to include instructions for how to get to the things you are talking about, such as right click, left click, etc.
Hi! Unfortunately, this tutorial is for beginners to VBA, not for beginners to Excel.
Hi, my code was working previously but now isn't. I am not sure why. It gives me runtime error of 13
Sub Get_Plate_A_Data()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your raw data file to import", FileFilter:="Excel Files (*.xls*), *xls*")
If FileToOpen False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
OpenBook.Sheets(1).Range("C126:AP221").Copy
ThisWorkbook.Worksheets(Sheet1).Range("C39").PasteSpecial xlPasteValues
OpenBook.Close False
End If
Application.ScreenUpdating = True
End Sub
Hi! Your request goes beyond the advice we provide on this blog. We don't help with writing VBA code.
this feature is not available how to solve
Hi! I can't see your workbook, but perhaps these instructions will help you: How to enable and disable macros in Excel.
I’d like to take names from a column and have the entire row copied in a separate sheet.
Data example:
List of names in column D
If the name is “Cody Hall” then I want the entire row copied (not deleted) to a sheet named “Cody”
I’ve attempted to use VBA code to accomplish this but to no avail.
Hi! To get a row from the table based on the value in a column, you can try using the FILTER function. Read more: Excel FILTER function - dynamic filtering with formulas.
I want to delete A2 to A35 column using VB in excel, please help me
i'm a registered user of ablebits excel ultimate suite. is there a way to run a certain ablebits tool [in my case, transpose] from vba or in a recorded macro? if it is not possible, this would be great addition to the ultimate suite
Hi! Unfortunately, the capabilities of VBA do not allow you to do this.
Hey, can you tell me about the basis of VBA apply in Excel. I want to make my future in this field.
Hello, thank you for posting this. I'm currently trying to use it and I have followed the steps as written but when I go to the run the macro I get a syntax error on this line:
Set cellsColorSample = Application.InputBox( _
"Select sample color:", "Select a cell with sample color", _ Application.Selection.Address, Type:=8)
Any ideas what I need to change? I'm sorry but I know little of VB Scripting so I'm just trying to follow directions here. This is what I have:
Sub SumCountByConditionalFormat()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim indRefColor As Long
Dim cellsColorSample As Range
Dim cntRes As Long
Dim sumRes
Dim cntCells As Long
Dim indCurCell As Long
On Error Resume Next
cntRes = 0
sumRes = 0
cntCells = Selection.CountLarge
Set cellsColorSample = Application.InputBox( _
"Select sample color:", "Select a cell with sample color", _ Application.Selection.Address, Type:=8)
If Not (cellsColorSample Is Nothing) Then
indRefColor = cellsColorSample.Cells(1, 1).DisplayFormat.Interior.Color
For indCurCell = 1 To (cntCells)
If indRefColor = Selection(indCurCell).DisplayFormat.Interior.Color Then
cntRes = cntRes + 1
sumRes = WorksheetFunction.Sum(Selection(indCurCell), sumRes)
End If
Next
MsgBox "Count=" & cntRes & vbCrLf & "Sum= " & sumRes & vbCrLf & vbCrLf & _
"Color=" & Left("000000", 6 - Len(Hex(indRefColor))) & _
Hex(indRefColor) & vbCrLf, , "Count & Sum by Conditional Format color"
End If
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
I am trying to sum cells that are conditionally formatted red. Any help you can give would be GREATLY appreciated!!!
Hi! Copy the VBA code carefully from the article, or use the sample file linked at the end of the article.
Hi there, thanks for posting this. I'm currently trying to use it and have followed the steps as outlined but when I go to the run the macro there is nothing to select. Any idea why this might be?
Hi! If you have correctly inserted the macro code and saved the file correctly as described in the article above, try these guidelines: How to enable and disable macros in Excel.
Hello everyone, I would like to ask you for help with my problem. I think it's possible to solve it with VBA, but I'm not sure how to do it.
There is an application named Qtest. I want to link it with excel with the help of VBA macros , so that I can upload data from Excel to Qtest automatically.
Hi! Sorry, your request goes beyond the advice we provide on this blog.
I have an RFID reader putting value in active Excel cell. Then nothing more happenes, i want Excel to automaticly move active cell to the cell below and calculate the sheet :-(
Hi! Unfortunately, I cannot reproduce your problem. Set the cursor to move automatically after entering a value in a cell: Options - Advanced - Editing options. Also check in the menu on the ribbon: Formulas - Calculation options - Automatic.
Add to VBA code the first executable statement: Application.Volatile.
I was able to do this using Office 2003, but after closing and then opening, i get the message that my VB project was removed. Ive spent hrs googling and experimenting but I cant find the solution. I have no problems if I use Office 2013, unfortunately, the old program im using will not work with newer versions of excel. Any ideas?
Unfortunately, code created in Excel 2013 and later does not work in Excel 2003.
Im not using excel 2013, im trying to get excel 2003 to stop removing my macro after closing/opening. I meant my old lintool program will not work with new versions of excel so im stuck using the 2003 excel.
Unfortunately, your old program doesn't work with modern VBA macros.
Thanks so much Alexander! that is very helpful.
Fails at Step 2. When I Press Alt-F11, I get the editor screen, but not the left-hand panel shown in the image. :-(.
Press Ctrl + R
This is so good & helpful for first time user like me - Thanks so much
Can someone help me with the following script? There is something wrong with the last call function of this.
Sub Admin()
' Defines variables
Dim Cell As Range, sRange As Range, Rng As Range
' Defines LastCol as the last column with a header on the Run sheet
LastCol = Sheets("SABER").Cells(1, Columns.Count).End(xlToLeft).Column
' Sets the search range as A1 to the last column with a header on the Run sheet
Set sRange = Sheets("SABER").Range("A1", Cells(1, LastCol))
' With the search range
With sRange
' Set Rng as the cell where "Admin No." is found
Set Rng = .Find(What:="Admin No.", _
After:=.Cells(1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
' If Rng exists then
If Not Rng Is Nothing Then
' Define LastRow as the last row of data under the Due Date header
lastRow = Sheets("SABER").Cells(Rows.Count, Rng.Column).End(xlUp).Row
' Copy from the Systme Consition header down to the last row of that column and paste to A1 of Paste Report Here sheet
Sheets("SABER").Range(Rng, Cells(lastRow, Rng.Column)).Copy _
Destination:=Sheets("Status").Range("A1")
End If
End With
End Sub
What I'm trying to accomplish here is to display the "Admin No." Column from sheet 3 "Saber" (no matte where it is in sheet 3) to a sheet called "Status".
This works really well HOWEVER The issue is when a row is deleted from Saber and I run the Macro in status the row remains where there like I never deleted it.
Any help would be apricated.
We apologize, but we cannot offer assistance beyond the scope of this blog.
Dose anyone one know if the above process can be accomplished without the Marco? ie. search sheet 2 for a certain column regardless of where it is and populate it in sheet 1.