Display data from data table into another sheet/layout without long spaghetti formulas
Автор: ExcelSynth
Загружено: 2025-10-18
Просмотров: 194
Описание:
1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
This is a worksheet event — it runs automatically every time the user selects a different cell on this sheet.
Target represents the cell (or range) the user just clicked.
You don’t need to call this macro manually — Excel fires it for you whenever selection changes.
2. Dim ws As Worksheet, licence_plate As String, Make As Variant, Model As Variant, Year As Variant
Declares variables:
ws: will later hold a reference to the data sheet.
licence_plate: stores the selected cell’s value (converted to text).
Make, Model, and Year: store lookup results from the “Data” sheet.
They’re declared as Variant because VLOOKUP can return either text or an error.
3. If Intersect(Target, Me.Range("B2:T52")) Is Nothing Then
Checks where the user clicked.
If they clicked outside the car grid (B2:T52), the macro shouldn’t do anything.
4. Me.Range("V4").Value = ""
Clears the display cell V4 whenever the user clicks outside the grid.
Keeps the interface clean — no leftover info from the last selection.
5. Exit Sub
Stops the macro immediately — prevents the rest of the code from running when selection is irrelevant.
6. licence_plate = CStr(Target.Value)
Stores the clicked cell’s value as a string (important because numeric IDs could otherwise misbehave in lookups).
7. If licence_plate = "" Then Me.Range("V4").Value = "": Exit Sub
If the clicked cell is empty — again, just clear the display cell and stop.
Avoids triggering lookups for blanks.
8. Set ws = ThisWorkbook.Sheets("Data")
Points ws to the “Data” sheet — so you can easily call lookups from it without hardcoding the sheet name every time.
🔹 Make = Application.VLookup(licence_plate, ws.Range("C:L"), 2, False)
🔹 Model = Application.VLookup(licence_plate, ws.Range("C:L"), 3, False)
🔹 Year = Application.VLookup(licence_plate, ws.Range("C:L"), 4, False)
These three lines search column C on the Data sheet for the licence plate and return values from columns D, E, and F respectively.
We're effectively doing:
“Find this licence plate in column C, and give me Make, Model, and Year.”
The False at the end forces an exact match — no partials, no guessing.
9. If Not IsError(Make) Then
If the lookup succeeds (no error), display the data.
If it fails (not found), show “no match found”.
10. Me.Range("V4").Value = licence_plate & " - " & Make & " " & Model & ", " & Year
Builds a neat, human-readable output string in V4, e.g.
“28-GBS-4 – BMW 320i, 2019”
11. Else
Handles the “not found” case.
12. Me.Range("V4").Value = licence_plate & " - no match found"
If the car isn’t in the data sheet, give feedback instead of leaving the user wondering.
13. End If / End Sub
Cleanly close the conditional and procedure.
🎵 Original beat by DJ Synth
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: