Complete beginner to VBA,

Linsie

New Member
Joined
Jul 19, 2012
Messages
29
Office Version
  1. 2021
Platform
  1. Windows
Hi,
I am venturing into VBA, I have seen some videos, but its not clicking with me atm.

I want 3 functions to autorun on each sheet of the 4 sheets. The first 3 sheets all have the same A-C columns. I would like B&C to auto populate when then engineers name is entered in A. The data is can lookup is in the Data Sheet. ( I would usually use for B =IFERROR(LOOKUP([@[Engineer Name *]],Table4[Full],Table4[Manager]),"") and for C =IFERROR(LOOKUP([@[Engineer Name *]],Table4[Full],Table4[Team]),"") for that)

Example Sheet (1) Trainings Taken <2 Yrs
Name of Joe Bloggs 1 is entered into the Engineer Name field (A2), that would then automatically trigger B2 to find the Managers name, and C2 to return the team name from the table found on the Data Sheet and to also auto size each column.

I know for auto size I can use the below, but I don't know how to put it all together.

For Each ws In Worksheets
ws.Columns.AutoFit
Next ws


Certification Data v2.xlsm
ABCDEFG
1Engineer Name *Manager Name (Autofill)Team (Autofill)Date (mm-yyyy) *Course Name *Level *Vendor *
2
3
4
5
6
(1) Trainings Taken <2 Yrs


Certification Data v2.xlsm
ABCD
1FullTeamManagerCountry
2Joe Bloggs 1ODCMODCM MGRNL
3Joe Bloggs 2OSESOSES MGRUK
4Joe Bloggs 3OSSOSES MGRNL
5Joe Bloggs 4GroupDirectorUK
6Joe Bloggs 5Band 6B6 MGRIE
7Joe Bloggs 6ODCMODCM MGRUK
8Joe Bloggs 7PSDOSS MGRUK
9Joe Bloggs 8OSESOSES MGRNL
10Joe Bloggs 9OSSOSS MGRUK
11Joe Bloggs 10OSESOSES MGRDE
12Joe Bloggs 11OSESOSES MGRUK
13Joe Bloggs 12GNO-AGNO-A MGRUK
14Joe Bloggs 13OSSOSS MGRNL
15Joe Bloggs 14ODCMODCM MGRAU
16Joe Bloggs 15OSSOSS MGRUK
Data
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
For your autofit part, try this:

VBA Code:
Sub AutoFitCol_All_Sheets()
Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.UsedRange.Columns.AutoFit
    Next ws
End Sub

Make you copy & paste that code into the "ThisWorkbook" module. Say for your example your excel file is named "Book6.xlsx".

Open up the VBA editor (Alt + F11). Locate Book6 and double-click on the "ThisWorkbook" module as shown in the screenshot. Then copy & paste the code I gave you.

VBE This workbook.png
 
Last edited:
Upvote 0
Solution
One additional comment to make. What you're asking for are 2 different types of events. The first "event" is that you want Excel automatically populate columns B & C, whenever a user types in the engineer's name into column A. That's one.

The second event you're asking for (regarding autofit) is just a 1-time deal. You do not need to run it each time that the 1st event is triggered. You just need to run that once and you're done. Therefore you should be looking for 2 separate macros independent of each other. Make sense?
 
Upvote 0
For your autofit part, try this:

VBA Code:
Sub AutoFitCol_All_Sheets()
Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.UsedRange.Columns.AutoFit
    Next ws
End Sub

Make you copy & paste that code into the "ThisWorkbook" module. Say for your example your excel file is named "Book6.xlsx".

Open up the VBA editor (Alt + F11). Locate Book6 and double-click on the "ThisWorkbook" module as shown in the screenshot. Then copy & paste the code I gave you.

View attachment 111352
Thanks for this and the explanation below.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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