i dont really understand macros/vba

EXCELlant

New Member
Joined
May 28, 2009
Messages
29
</SPAN>I understand that a Macro is good for completing tasks which will be repeated but I have no idea how to even start with something like this.</SPAN>

I sometimes use the record macro option but this time I need to include a search function.</SPAN>

I downloaded some data at work labeled sheet 1 (see below) which shows both changes to a vendor and newly created vendors, and I would like to split them into two new sheets.</SPAN>

Here is an example of two vendors I would like the macro to look at column E and if grouped together has more than 2 “ *** Created *** </SPAN>” in column J it removes the data and puts it into a new sheet.</SPAN>

If there is 2 or less “*** Created *** “ then it will remain in the current sheet.</SPAN></SPAN>

E</SPAN></SPAN>
F</SPAN></SPAN>
G</SPAN></SPAN>
H</SPAN></SPAN>
I</SPAN></SPAN>
J</SPAN></SPAN>
Vendor</SPAN></SPAN>
Changed By</SPAN></SPAN>
Field Name</SPAN></SPAN>
Company Code</SPAN></SPAN>
Purch. Organization</SPAN></SPAN>
New value</SPAN></SPAN>
300874</SPAN></SPAN>
Confirm.status</SPAN></SPAN>
300874</SPAN></SPAN>
Name</SPAN></SPAN>
300874</SPAN></SPAN>
Bank Details</SPAN></SPAN>
*** Created ***</SPAN></SPAN>
300874</SPAN></SPAN>
Chk double inv.</SPAN></SPAN>
2000</SPAN></SPAN>
300874</SPAN></SPAN>
Confirm.status</SPAN></SPAN>
300874</SPAN></SPAN>
Name</SPAN></SPAN>
300874</SPAN></SPAN>
IBAN</SPAN></SPAN>
*** Created ***</SPAN></SPAN>
303901</SPAN></SPAN>
Confirm.status</SPAN></SPAN>
303901</SPAN></SPAN>
ConfirmSt (CCd)</SPAN></SPAN>
1000</SPAN></SPAN>
303901</SPAN></SPAN>
Bank Details</SPAN></SPAN>
*** Created ***</SPAN></SPAN>
303901</SPAN></SPAN>
Company code data</SPAN></SPAN>
1000</SPAN></SPAN>
*** Created ***</SPAN></SPAN>
303901</SPAN></SPAN>
General data</SPAN></SPAN>
*** Created ***</SPAN></SPAN>
303901</SPAN></SPAN>
Purchasing Data</SPAN></SPAN>
1000</SPAN></SPAN>
*** Created ***</SPAN></SPAN>
303901</SPAN></SPAN>
IBAN</SPAN></SPAN>
*** Created ***</SPAN></SPAN>
303901</SPAN></SPAN>
Cntrl Addr.Admn</SPAN></SPAN>
*** Created ***</SPAN></SPAN>
303901</SPAN></SPAN>
Cntrl Addr.Admn</SPAN></SPAN>
*** Created ***</SPAN></SPAN>
303901</SPAN></SPAN>
Cntrl Addr.Admn</SPAN></SPAN>
*** Created ***</SPAN></SPAN>
303901</SPAN></SPAN>
Cntrl Addr.Admn</SPAN></SPAN>
*** Created ***</SPAN></SPAN>

<TBODY>
</TBODY>

The aim at the end of the task would be to have two sheets.</SPAN>

The one sheet displaying all of the vendors which have had modifications in the month (here highlighted in blue).</SPAN>

With another sheet with all of the new vendors for the period displayed (in the above picture highlighted in green).</SPAN>
Obviously the original data can have hundreds of lines with many new vendors or vendors which have had changes.</SPAN>

Any help, suggestions would be greatly appreciated.</SPAN>
Thank you.</SPAN>
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Excellent,

Sample raw data in worksheet Sheet1 (with the data already grouped/sorted in column E, per your example):


Excel 2007
ABCDEFGHIJ
1Title ATitle BTitle CTitle DVendorChanged ByField NameCompany CodePurch. OrganizationNew value
22222300874Confirm.status
33333300874Name
44444300874Bank Details*** Created ***
55555300874Chk double inv.2000
66666300874Confirm.status
77777300874Name
88888300874IBAN*** Created ***
99999303901Confirm.status
1010101010303901ConfirmSt (CCd)1000
1111111111303901Bank Details*** Created ***
1212121212303901Company code data1000*** Created ***
1313131313303901General data*** Created ***
1414141414303901Purchasing Data1000*** Created ***
1515151515303901IBAN*** Created ***
1616161616303901Cntrl Addr.Admn*** Created ***
1717171717303901Cntrl Addr.Admn*** Created ***
1818181818303901Cntrl Addr.Admn*** Created ***
1919191919303901Cntrl Addr.Admn*** Created ***
20
Sheet1


After the macro in worksheet Sheet1:


Excel 2007
ABCDEFGHIJ
1Title ATitle BTitle CTitle DVendorChanged ByField NameCompany CodePurch. OrganizationNew value
22222300874Confirm.status
33333300874Name
44444300874Bank Details*** Created ***
55555300874Chk double inv.2000
66666300874Confirm.status
77777300874Name
88888300874IBAN*** Created ***
99999303901Confirm.status
1010101010303901ConfirmSt (CCd)1000
11
12
13
14
15
16
17
18
19
20
Sheet1


After the macro in a new worksheet Created:


Excel 2007
ABCDEFGHIJ
1Title ATitle BTitle CTitle DVendorChanged ByField NameCompany CodePurch. OrganizationNew value
211111111303901Bank Details*** Created ***
312121212303901Company code data1000*** Created ***
413131313303901General data*** Created ***
514141414303901Purchasing Data1000*** Created ***
615151515303901IBAN*** Created ***
716161616303901Cntrl Addr.Admn*** Created ***
817171717303901Cntrl Addr.Admn*** Created ***
918181818303901Cntrl Addr.Admn*** Created ***
1019191919303901Cntrl Addr.Admn*** Created ***
11
Created


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub GetCreated()
' hiker95, 09/25/2013
' http://www.mrexcel.com/forum/excel-questions/728755-i-dont-really-understand-macros-visual-basic-applications.html
Dim w1 As Worksheet, wc As Worksheet, rng As Range
Dim r As Long, lr As Long, nr As Long, rr As Long, lc As Long, n As Long, nn As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
lr = w1.Cells(Rows.Count, 5).End(xlUp).Row
lc = w1.Cells(1, Columns.Count).End(xlToLeft).Column
If Not Evaluate("ISREF(Created!A1)") Then Worksheets.Add(After:=w1).Name = "Created"
Set wc = Worksheets("Created")
wc.UsedRange.ClearContents
wc.Cells(1, 1).Resize(, lc).Value = w1.Cells(1, 1).Resize(, lc).Value
For r = 2 To lr
  n = Application.CountIf(w1.Columns(5), w1.Cells(r, 5).Value)
  If n > 2 Then
    Set rng = w1.Range("J" & r & ":J" & r + n - 1)
    nn = Application.CountIf(rng, "*** Created ***")
    If nn > 2 Then
      For rr = r To r + n - 1
        If w1.Cells(rr, 10) = "*** Created ***" Then
          nr = wc.Range("J" & Rows.Count).End(xlUp).Offset(1).Row
          wc.Cells(nr, 1).Resize(, lc).Value = w1.Cells(rr, 1).Resize(, lc).Value
          w1.Cells(rr, 5).ClearContents
        End If
      Next rr
    End If
  End If
  r = r + n - 1
Next r
On Error Resume Next
w1.Range("E2:E" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
With wc
  .Columns.AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetCreated macro.
 
Upvote 0
Excellent,

I understand that a Macro is good for completing tasks which will be repeated but I have no idea how to even start with something like this.


Training / Books / Sites as of 7/13/2013

What Is VBA?
VBA is an acronym for Visual Basic for Applications. VBA should not be confused with VB, which is standard Visual Basic. Visual Basic for Applications is a programming feature designed by Microsoft for use with their Microsoft Office

MrExcel's Products: Books, CDs, Podcasts Discuss topics related to Holy Macro! Products: Learn Excel from MrExcel, VBA and Macros for Microsoft Excel,Holy Macro! It's 2500 VBA Examples CD, Guerilla Data Analysis Using Microsoft Excel and Excel Knowledge Base CD and the MrExcel Podcasts.
MrExcel's Products: Books, CDs, Podcasts

There are over 1800 Excel videos/tutorials here:
ExcelIsFun - YouTube

Getting Started with VBA.
DataPig Technologies

If you are serious about learning VBA try
Macros Made Easy for Microsoft Excel

Excel Tutorials and Tips - VBA - macros - training
Excel Tutorial | Excel Tips | Excel Articles

Microsoft Office training
http://office.microsoft.com/en-us/tr...101782702.aspx

Free VBA Course
Free VBA Course

Excel 2007 VBA materials to learn here:
VBA for Excel 2007 tutorial-VBA programming?

Here's a good primer on the scope of variables.
Understanding Scope

Using Variables in Excel VBA Macro Code
Excel VBA Variables. Using Variables in Excel VBA Macro Code

See David McRitchie's site if you just started with VBA
Getting Started with Macros and User Defined Functions

What is a Visual Basic Module?
What Is A VBA Module and how is a VBA Module Used?

Here is a good introductory tutorial using a VBA Class:
Classes In VBA

Ron de Bruin's intro to macros:
http://www.rondebruin.nl/code.htm

Ron's Excel Tips
Excel for Windows Tips

Ron de Bruin's Mail from Excel and make/mail PDF files (Windows)
Mail from Excel and make/mail PDF files (Windows)

Anthony's Excel VBA Page - Excel Application - Excel Consultant - Excel Consulting (see 3 tutorials in Excel VBA Basic Tutorial Series)

http://www.jlathamsite.com/Teach/VBA...troduction.pdf (95 page "book")

BET: Microsoft Excel Visual Basic

Debugging VBA
Debug Excel VBA Macro Code
TechBookReport - Debugging Excel VBA Code

Start at the beginning...

Creating An XLA Add-In For Excel, Writing User Defined Functions In VBA
Creating An XLA Add In

How do I create a PERSONAL.XLS(B) or Add-in
http://www.rondebruin.nl/personal.htm

Creating custom functions
Creating custom functions - Excel - Office.com

Writing Your First VBA Function in Excel
Writing Your First VBA Function in Excel | Custom Functions

VBA for Excel (Macros)
Excel Macros (VBA) Tutorial

Excel Macros Tutorial
Excel Macros (VBA) Tutorial

Excel Macros & Programming
Learning about EXCEL macros

VBA Lesson 11: VBA Code General Tips and General Vocabulary
VBA for Excel macros language

Excel VBA -- Adding Code to a Workbook
http://www.contextures.com/xlvba01.html

Beyond Excel's recorder

Helpful as a simple concise reference for the basics, and, using the macro recorder is endlessly powerful.
MS Excel: Cells

Learn to debug:
Debugging VBA

How To: Assign a Macro to a Button or Shape
How To: Assign a Macro to a Button or Shape - Peltier Tech Blog

User Form Creation
http://www.contextures.com/xlUserForm01.html

When To Use a UserForm & What to Use a UserForm For
When to use Userform & What To Use Them For. Excel Training VBA 2 lesson 2

Excel Tutorials / Video Tutorials - Functions
http://www.contextures.com/xlFunctions02.html

How to insert Buttons, radio buttons and check boxes in Excel
How to insert Buttons, radio buttons and check boxes in Excel - Bing Videos

INDEX MATCH - Excel Index Function and Excel Match Function
http://www.contextures.com/xlFunctions03.html

Excel Data Validation
http://www.contextures.com/xlDataVal08.html#Larger
http://www.contextures.com/excel-data-validation-add.html

Excel -- Data Validation -- Create Dependent Lists
http://www.contextures.com/xlDataVal02.html

Your Quick Reference to Microsoft Excel Solutions
XL-CENTRAL.COM : For your Microsoft Excel Solutions

New! Excel Recorded Webinars
DataPig Technologies

Fuzzy Matching - new version plus explanation

Programming The VBA Editor - Created by Chip Pearson at Pearson Software Consulting LLC
This page describes how to write code that modifies or reads other VBA code.
Programming In The VBA Editor

VBA and Macros for Microsoft Excel, by Bill Jelen "Mr.Excel" and Tracy Syrstad

Excel Hacks 100 Industrial-Strength Tips & Tools, by David & Traina Hawley

VBA and Macros for Microsoft Excel 2007, by Bill Jelen "Mr.Excel" and Tracy Syrstad

Excel 2007 Book: you can try this...there is a try before you buy ebook available at this link…

by Stephen/ Bovey, Rob/ Green, John Bullen (Paperback - Feb 11, 2005)
Professional Excel Development

by Rob Bovey, Stephen Bullen, John Green, and Robert Rosenberg (Paperback - Sep 26, 2001)
Excel 2002 VBA: Programmers Reference

"Professional Excel Development" by Rob Bovey, Dennis Wallentin, Stephen Bullen, & John Green

DonkeyOte: My Recommended Reading, Volatility
Volatile Excel Functions -Decision Models

Sumproduct
http://www.xldynamic.com/source/xld.SUMPRODUCT.html

Arrays
VBA Array Basics
VBA Arrays
Excel: Introduction to Array Formulas - Xtreme Visual Basic Talk
Excel VBA Chpter 21: Array in Excel VBA

Array Dimensions in Visual Basic - Working with Dimensions (code and graphics)
Array Dimensions in Visual Basic

Shortcut Keys in Excel 2000 through 2007

Pivot Intro
Using Pivot Tables and Pivot Charts in Microsoft Excel
Office 2010 Class #36: Excel PivotTables Pivot Tables 15 examples (Data Analysis) - YouTube
Office 2010 Class #36: Excel PivotTables Pivot Tables 15 examples (Data Analysis) - YouTube
Getting Started with Pivot Tables
http://www.contextures.com/xlPivot01.html#Start
Overview of PivotTable and PivotChart reports
Overview of PivotTable and PivotChart reports - Excel - Office.com
Build a Pivot Table in Excel VBA
Excel VBA - Build A Pivot Table In Excel VBA Tutorials

Email from XL - VBA
Mail from Excel example pages

Outlook VBA
Writing VBA code for Microsoft Outlook

Function Dictionary
http://www.xlfdic.com/

Function Translations
Excel 2007 function name translations - Dictionary Chart Front Page

Dynamic Named Ranges
http://www.contextures.com/xlNames01.html

How to create Excel Dashboards
http://www.mrexcel.com/Excel-dashboards-Xcelsius.html
http://www.contextures.com/excel-dashboards.html
Excel Dashboards - Templates, Tutorials, Downloads and Examples | Chandoo.org - Learn Microsoft Excel Online
Excel Dashboards - Templates, Tutorials, Downloads and Examples | Chandoo.org - Learn Microsoft Excel Online
Free Microsoft Excel Dashboard Widgets to Download
AJP Excel Information - Gauge

Excel Dashboard / Scorecard Ebook
Excel Dashboards and Scorecards Ebook | How to Create Dashboards in Excel

Mike Alexander from Data Pig Technologies
Excel 2007 Dashboards and Reports For Dummies: Michael Alexander: 9780470228142: Amazon.com: Books

Templates
CPearson.com Topic Index
http://www.contextures.com/excel-template-golf-scores.html
Free Microsoft Excel Template Links & Search Engine

Microsoft Excel Cascading Listboxes Tutorial
Microsoft Excel Cascading Listboxes Tutorial - YouTube

Date & Time stamping:
McGimpsey & Associates : Excel : Time and date stamps

Get Formula / Formats thru custom functions:
Show FORMULA or FORMAT of another cell

A nice informative MS article "Improving Performance in Excel 2007"
Improving Performance in Excel 2007

Progress Meters
AJP Excel Information - Progress meters
Website Disabled

How to convert text to numbers in Excel
How to convert text to numbers in Excel

How to parse data from the web - Brett Fret has a video in YouTube
Excel VBA - Easily parse XML data from a file or website using XML Maps - YouTube

And, as your skills increase, try answering posts on sites like:
MrExcel.com | Excel Resources | Excel Seminars | Excel Products
Excel Help Forum
Excel Templates | Excel Add-ins and Excel Help with formulas and VBA Macros
http://www.vbaexpress.com
Excel, Access, PowerPoint and Word VBA Macro Automation Help

If you are willing to spend money for the training, then something here should work for you...
Amazon.com: excel tutorial dvd
Amazon.com: excel tutorial dvd

Advanced Excel Training - Online Excel Course
http://www.udemy.com/advanced-excel/
 
Upvote 0
Dear Hiker,

Thank you for your swift reply,

I think the macro is near spot on, except in the first sheet the last two lines contain Vendor 303901 (which is part of the vendor 303901), although they dont have the required " *** Created *** " next to they belong to the group which has been moved into the new sheet (named Created).

Is it possible to include this in the macro?

Also many thanks for the suggested reading list. I am slightly overwhelmed by it and still a little unsure of where to start!

Thank you!
 
Upvote 0
Excellent,

Also many thanks for the suggested reading list. I am slightly overwhelmed by it and still a little unsure of where to start!

You are very welcome - just take it slow.


I think the macro is near spot on, except in the first sheet the last two lines contain Vendor 303901 (which is part of the vendor 303901), although they dont have the required " *** Created *** " next to they belong to the group which has been moved into the new sheet (named Created).

The screenshots that I posted are per your original post. And, it would appear to me that the macro is working correctly.

In order to continue I will need to see your actual workbook.

You can upload your workbook to Box Net,
sensitive data changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0
Hi Hiker,

Im sorry maybe I didnt explain very well.

What I am hoping for is in your middle screen shot, lines 9 and 10 which are for Vendor 303901 to also be carried over to your third screen shot with the other nine items for vendor 303901.

Does that make more sense?

I want all of the seperate lines for one vendor to remain on the same sheet. i.e you shouldnt have the one vendor on both sheets.
 
Upvote 0
Excellent,

Got it.

Sample raw data in worksheet Sheet1 (with the data already grouped/sorted in column E, per your example):


Excel 2007
ABCDEFGHIJ
1Title ATitle BTitle CTitle DVendorChanged ByField NameCompany CodePurch. OrganizationNew value
22222300874Confirm.status
33333300874Name
44444300874Bank Details*** Created ***
55555300874Chk double inv.2000
66666300874Confirm.status
77777300874Name
88888300874IBAN*** Created ***
99999303901Confirm.status
1010101010303901ConfirmSt (CCd)1000
1111111111303901Bank Details*** Created ***
1212121212303901Company code data1000*** Created ***
1313131313303901General data*** Created ***
1414141414303901Purchasing Data1000*** Created ***
1515151515303901IBAN*** Created ***
1616161616303901Cntrl Addr.Admn*** Created ***
1717171717303901Cntrl Addr.Admn*** Created ***
1818181818303901Cntrl Addr.Admn*** Created ***
1919191919303901Cntrl Addr.Admn*** Created ***
20
Sheet1


After the macro in worksheet Sheet1:


Excel 2007
ABCDEFGHIJ
1Title ATitle BTitle CTitle DVendorChanged ByField NameCompany CodePurch. OrganizationNew value
22222300874Confirm.status
33333300874Name
44444300874Bank Details*** Created ***
55555300874Chk double inv.2000
66666300874Confirm.status
77777300874Name
88888300874IBAN*** Created ***
9
10
11
12
13
14
15
16
17
18
19
20
Sheet1


After the macro in a new worksheet Created:


Excel 2007
ABCDEFGHIJ
1Title ATitle BTitle CTitle DVendorChanged ByField NameCompany CodePurch. OrganizationNew value
29999303901Confirm.status
310101010303901ConfirmSt (CCd)1000
411111111303901Bank Details*** Created ***
512121212303901Company code data1000*** Created ***
613131313303901General data*** Created ***
714141414303901Purchasing Data1000*** Created ***
815151515303901IBAN*** Created ***
916161616303901Cntrl Addr.Admn*** Created ***
1017171717303901Cntrl Addr.Admn*** Created ***
1118181818303901Cntrl Addr.Admn*** Created ***
1219191919303901Cntrl Addr.Admn*** Created ***
13
Created


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub GetCreatedV2()
' hiker95, 09/26/2013
' http://www.mrexcel.com/forum/excel-questions/728755-i-dont-really-understand-macros-visual-basic-applications.html
Dim w1 As Worksheet, wc As Worksheet, rng As Range
Dim r As Long, lr As Long, nr As Long, lc As Long, n As Long, nn As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
lr = w1.Cells(Rows.Count, 5).End(xlUp).Row
lc = w1.Cells(1, Columns.Count).End(xlToLeft).Column
If Not Evaluate("ISREF(Created!A1)") Then Worksheets.Add(After:=w1).Name = "Created"
Set wc = Worksheets("Created")
wc.UsedRange.ClearContents
wc.Cells(1, 1).Resize(, lc).Value = w1.Cells(1, 1).Resize(, lc).Value
For r = 2 To lr
  n = Application.CountIf(w1.Columns(5), w1.Cells(r, 5).Value)
  If n > 2 Then
    Set rng = w1.Range("J" & r & ":J" & r + n - 1)
    nn = Application.CountIf(rng, "*** Created ***")
    If nn > 2 Then
      nr = wc.Range("J" & Rows.Count).End(xlUp).Offset(1).Row
      wc.Cells(nr, 1).Resize(r + n - 1, lc).Value = w1.Cells(r, 1).Resize(r + n - 1, lc).Value
      w1.Cells(r, 1).Resize(r + n - 1, lc).ClearContents
    End If
  End If
  r = r + n - 1
Next r
On Error Resume Next
w1.Range("E2:E" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
With wc
  .Columns.AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetCreatedV2 macro.
 
Upvote 0
Hi Hiker,

Yes thats exactly what Im after! thank you so much for your help and patience!

Thanks again!
 
Upvote 0
Excellent,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,215,212
Messages
6,123,651
Members
449,111
Latest member
ghennedy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top