Writing Excel Macros with GPT - Automating Without VBA Using AI
Don't know VBA coding? That's okay! AI writes macro code to automate repetitive tasks for you.
Auto-Writing IF Statements and VLOOKUP
IF statements and VLOOKUPโjust hearing the names gives you a headache, right? They're the most commonly used Excel functions, but also the most confusing.
IF statements are fine with one condition, but when you nest multiple conditions, you lose track of where parentheses close and errors occur frequently. VLOOKUP is confusing with argument order, and you never remember when to use FALSE and TRUE.
With AI, you can easily create even these complex functions. Just naturally describe the conditions you want, and AI creates a perfect IF statement. For VLOOKUP, just say "find this from here and get that" and you're done.
In this post, we'll learn how to auto-generate IF statements and VLOOKUP with AI. From simple conditional statements to complex nested IF statements and error handling, we'll cover it all.
1. Auto-Generating Simple IF Statements
Basic IF Statement
AI Prompt:
"If score is 60 or above, display 'Pass', otherwise 'Fail'."
AI Generated Formula:
=IF(B2>=60, "Pass", "Fail")
Multi-Condition IF Statement
AI Prompt:
"Grade by score:
- 90 or above: A
- 80 or above: B
- 70 or above: C
- 60 or above: D
- Others: F"
AI Generated Formula:
=IF(B2>=90, "A",
IF(B2>=80, "B",
IF(B2>=70, "C",
IF(B2>=60, "D", "F"))))
// Or using IFS function (Excel 2016 or later)
=IFS(
B2>=90, "A",
B2>=80, "B",
B2>=70, "C",
B2>=60, "D",
TRUE, "F"
)
2. Complex Conditional IF Statements
AND Conditions
AI Prompt:
"If attendance rate is 80% or above AND score is 70 or above, display 'Complete',
if either is below, display 'Incomplete'."
AI Generated Formula:
=IF(AND(B2>=80, C2>=70), "Complete", "Incomplete")
// Or
=IF((B2>=80)*(C2>=70), "Complete", "Incomplete")
OR Conditions
AI Prompt:
"If payment method is 'Credit Card' OR 'Debit Card', classify as 'Card Payment',
otherwise 'Cash Payment'."
AI Generated Formula:
=IF(OR(D2="Credit Card", D2="Debit Card"), "Card Payment", "Cash Payment")
// Or
=IF((D2="Credit Card")+(D2="Debit Card"), "Card Payment", "Cash Payment")
3. Basic VLOOKUP
Simple VLOOKUP
AI Prompt:
"Look up employee name from 'Employee Info' sheet using employee ID in Column A.
If not found, display 'Not Registered'."
AI Generated Formula:
=IFERROR(
VLOOKUP(A2, EmployeeInfo!$A:$D, 2, FALSE),
"Not Registered"
)
// Or using IFNA
=IFNA(
VLOOKUP(A2, EmployeeInfo!$A:$D, 2, FALSE),
"Not Registered"
)
Referencing Multiple Columns
AI Prompt:
"Retrieve product name (column 2), unit price (column 3), and stock (column 4) at once using product code."
AI Generated Formulas:
// Column B: Product name
=VLOOKUP($A2, ProductDB!$A:$E, 2, FALSE)
// Column C: Unit price
=VLOOKUP($A2, ProductDB!$A:$E, 3, FALSE)
// Column D: Stock
=VLOOKUP($A2, ProductDB!$A:$E, 4, FALSE)
// Or as one array formula (Ctrl+Shift+Enter)
=VLOOKUP($A2, ProductDB!$A:$E, {2,3,4}, FALSE)
4. Advanced VLOOKUP Usage
Approximate Match Lookup
AI Prompt:
"Find commission rate by sales amount:
- 0~10 million won: 5%
- 10~50 million won: 3%
- 50~100 million won: 2%
- 100 million won or more: 1%"
AI Generated Formula:
// Commission table sheet:
A B
0 5%
10000000 3%
50000000 2%
100000000 1%
// Formula (TRUE: approximate match)
=VLOOKUP(B2, CommissionTable!$A:$B, 2, TRUE)
// Or direct array
=VLOOKUP(B2, {0,0.05;10000000,0.03;50000000,0.02;100000000,0.01}, 2, TRUE)
Left Column Reference (INDEX-MATCH)
AI Prompt:
"VLOOKUP can't reference left columns, but
I want to find product code (Column A) using product name (Column C)."
AI Generated Formula:
// Use INDEX-MATCH combination
=INDEX(ProductDB!$A:$A, MATCH(C2, ProductDB!$C:$C, 0))
// Or XLOOKUP (Microsoft 365)
=XLOOKUP(C2, ProductDB!$C:$C, ProductDB!$A:$A, "None")
5. Auto-Generating Complex Conditional Statements with VBA
IF Statement Auto-Generator
AI Prompt:
"Create a tool that automatically generates IF statements
based on user-entered conditions."
AI Generated VBA:
Sub IFStatementGenerator()
Dim conditionCount As Integer
Dim i As Integer
Dim formula As String
Dim condition As String
Dim result As String
' Input number of conditions
conditionCount = InputBox("How many conditions?", "Condition Count", 3)
formula = "=IF("
For i = 1 To conditionCount
condition = InputBox("Enter condition " & i & "." & vbCrLf & _
"Example: A1>=90", "Condition Input")
result = InputBox("Enter result when condition " & i & " is true.", "Result Input")
If i = 1 Then
formula = formula & condition & ',"' & result & '",'
Else
formula = formula & "IF(" & condition & ',"' & result & '",'
End If
Next i
' Last false condition
result = InputBox("Enter result when all conditions are false.", "Default Value Input")
formula = formula & '"' & result & '"'
' Close parentheses
For i = 1 To conditionCount - 1
formula = formula & ")"
Next i
formula = formula & ")"
' Display result
MsgBox "Generated formula:" & vbCrLf & vbCrLf & formula, vbInformation
' Copy to clipboard
Dim dataObj As Object
Set dataObj = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
dataObj.SetText formula
dataObj.PutInClipboard
MsgBox "Formula copied to clipboard.", vbInformation
End Sub
Conclusion
Automating IF statements and VLOOKUP with AI:
- โ Generate complex conditional statements in natural language
- โ Error-free formulas with VLOOKUP builder
- โ Automate repetitive tasks with VBA
- โ Improve reusability with formula library
In the next post, we'll learn how to find meaningful patterns in large datasets.
๐๊ฐ์ ์๋ฆฌ์ฆ
Getting Excel Function Explanations from ChatGPT - Learning Excel Basics with AI
Don't know what VLOOKUP is? AI explains complex Excel functions in simple terms and shows you practical examples.
Auto-Generating Excel Formulas with GPT - First Steps to AI Excel Automation
No more struggling with complex formulas! Just describe what you want to ChatGPT and get ready-to-use formulas instantly.
Organizing Excel Data with ChatGPT - AI Data Cleaning Know-How
Stressed about messy data! Learn how AI can quickly organize everything from removing duplicates to standardizing formats.