5 ms·
For fun, I took this formula and asked ChatGPT to convert it to a VBA Function (with comments): Function extractText(cell As Range) As String ' Dec
by jmkni 4y ago
For fun, I took this formula and asked ChatGPT to convert it to a VBA Function (with comments):
Function extractText(cell As Range) As String
' Declare an array of search terms
Dim searchTerms As Variant
searchTerms = Array("Banner", "EBL2", "Movie Art", "Use as is", "TTT", "Generic")
' Initialize start and end positions to 0
Dim startPos As Long
startPos = 0
Dim endPos As Long
endPos = 0
' Loop through search terms and find the first occurrence of any of them in the cell
For Each searchTerm In searchTerms
startPos = WorksheetFunction.Search(searchTerm, cell)
If startPos > 0 Then
Exit For
End If
Next searchTerm
' If none of the search terms were found, try finding "Customs" or "Custom"
If startPos = 0 Then
startPos = WorksheetFunction.Search("Customs", cell)
If startPos = 0 Then
startPos = WorksheetFunction.Search("Custom", cell)
End If
End If
' If none of the above were found, try finding "Generic"
If startPos = 0 Then
startPos = WorksheetFunction.Search("Generic", cell)
End If
' If none of the search terms were found, return an empty string
If startPos = 0 Then
extractText = ""
Else
' Loop through search terms and find the next occurrence of any of them after the start position
For Each searchTerm In searchTerms
endPos = WorksheetFunction.Search(searchTerm, cell, startPos + 1)
If endPos > 0 Then
Exit For
End If
Next searchTerm
' If none of the search terms were found after the start position, set the end position to the end of the cell
If endPos = 0 Then
endPos = Len(cell) + 1
End If
' Extract the text between the start and end positions
extractText = Mid(cell, startPos, endPos - startPos)
End If
End Function
Edit: Sadly this doesn't work at all lol, and after half an hour of prompting ChatGPT can't figure out why, it just gets stuck in a loop :-(
- capableweb 4y agoDid you actually try running the code vs the formula and compared it works the same way in the expected cases? The times I've tried using ChatGPT, it has mostly giving me code that seems like it'd work but doesn't.
- dmurray 4y ago"ChatGPT, can you give me some test cases for the code?"
- omgomgomgomg 4y agoThis is interesting, would ChatGPT know to wrap everything in am iferror and can it create new sheets and book and save them as csv on any os? This took me a full week to make it work and got me a raise, but it is so hacky, I rather not want to maintain that. Can it run cron jobs, handle api responses etc?
- logicallee 4y ago(not OP) over on reddit people are saying it is fantastic for giving them excel formulas they ask for. I guess it means the formulas work!
- hermitcrab 4y agoThat is one of the big issues with AIs like ChatGPT though. It gives you a plausible sounding answer, but that answer might be total BS. It doesn't tell you what degree of confidence it has in the answer. Maybe the formulas it gives are right 100%, 99% or 0% of the time.
- Sharlin 4y agoYes, and I think that in order for helper AIs like ChatGPT to really break through, people (especially in marketing) will have to learn to embrace confidence intervals, or at least some quantitative concept of confidence. The AI already knows how confident it is in its prediction, it's just a UI question. If plain numbers are too nerdy or Spock-y, at least tell me something like "I'm fairly sure" or "I'm not sure, but..."
- otabdeveloper4 4y agoGood lord, it doesn't have any confidence at all. It's just a database query with a lossy compression step. What's the confidence level of copy-pasting random code snippets from Stackoverflow?
- russfink 4y agoDid it write the comments, too?
- jmkni 4y agoYes, you can give it any snippet of code and ask it to add comments (the accuracy of the comments can be hit or miss though)