- VBA-to-JavaScript-Translator
- Conversion from VBA to Javascript
- 1 Answer 1
- How to convert VBA macro to Javascript
- >Solution :
- Saved searches
- Use saved searches to filter your results more quickly
- mha105/VBA-to-JavaScript-Translator
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
VBA-to-JavaScript-Translator
This translator is intended to be used as an educational tool to help VBA developers get familiar with JavaScript.
The code for this tool is based on regex examples found from multiple sources in forums and online. Sorry to say. I did not keep a record of all the sources I leveraged. Just know that people smarter than I am provided the basis for most of the code found here. I simply adjusted what I needed and slapped it all together into a single utility.
As with most translation tools, this tool will NOT perform a 100% complete translation. It is designed to cover common constructs in VBA (enough to get you started).
The idea is to enter a basic block of VBA code (be sure to include your Function/Sub wrappers) and see how the syntax would look in JavaScript. It’s best if you first try something simple then progress into more advanced IF statements, loops, comparison operators, Select Case switches, etc.
Currently, this translator works with:
- Both Functions and Sub Procedures
- Variable Declarations
- Basic IF THEN Statements
- SELECT CASE Statements
- Most Comparison Operators
- Basic FOR x TO y Loops
- Most variations of DO LOOPS
- Basic MSGBOX calls
Known Issues: This tool currently does NOT accurately translate
- Built in VBA functions
- References to Office Objects
- WITH Statements
- STEP Constructs in FOR Loops
- FOR EACH Loops (in fact these cause the tool to return nothing at all. I’m still working out why that is)
I offer the source code up to anyone interested in helping make enhancements to this tool in order to develop a more robust utility for the VBA community.
Open Source Agenda is not affiliated with «VBA To JavaScript Translator» Project. README Source: mha105/VBA-to-JavaScript-Translator
Conversion from VBA to Javascript
I am trying to convert some VBA excel code to JS, but even I was sure of me, the code does not work properly. Here are the snippets which cause me problems :
VBA: Tri1 = Tri1 + 2 ^ (2 - Tour) JS: tri1 = tri1 + Math.pow(2, (2 - tour));
VBA: If (y And (2 ^ (5 - Tour))) <> (Tri2 And 2 ^ (5 - Tour)) Then JS: if ((y && Math.pow(2, 5 - tour)) != (tri2 && Math.pow(2, 5 - tour)))
VBA: If (Tri1 And 2 ^ (2 - Tour)) = 0 Then JS: if ((tri1 && Math.pow(2, 2 - tour)) == 0)
tri1, tri2 and y are all integers. I have good understanding of JS, but none in VBA. So I googled a lot before already. Thanks !
Can you clarify what exactly is not working? What are the inputs, what you expect, what you actually get?
I expect the same results that the VBA ones. I can’t clarify much, cause I don’t know the outputs in VBA line by line. The inputs are for example : y = 0 to 7; tour = 1; tri1 = 0 (first iteration); tri = 0 (first iteration); But is my «translation» is correct ?
1 Answer 1
This is not a complete answer, but I think that information could get you to the goal. Because you are just trying to translate the words from vba to js. But js is not vba and js has some special things.
First: Floating numbers
A floating number in js is not always that like it looks like. That question here will give you more information about that topic: How to deal with floating point number precision in JavaScript?
Because of the code snipets I don’t know if one of your variables is a floating number. But if, you should know about that problem.
Second: Comparison
The way JS is handling comparisons is a litle bit different to some other languages. Some helpful links about that topic:
Specially the comparisons in your code are not very clear. For example:
var a = 10; var b = 0; document.write((a && b) == 0); // is true document.write(a && b) // is 0 var a = 10; var b = 11; document.write((a && b) == 0); // is false document.write(a && b) // is 11
Is that really the behaviour you want to have? So try to simplify your comparisons and your code could work.
How to convert VBA macro to Javascript
I want to use an Excel VBA macro on google sheet but I don’t know how to convert it to Javascript as google spreadsheet macros work on Javascript. Code basically pushes a row on different sheet based on the value of a cell.
Link for better understanding
https://www.extendoffice.com/documents/excel/3723-excel-move-row-to-another-sheet-based-on-cell-value.html
Can someone help me out in converting the following code.
P.S I have tried macro converter but don’t have an enterprise account to access it.
Sub moverows() Dim xRg As Range Dim xCell As Range Dim I As Long Dim J As Long Dim K As Long I = Worksheets("Sheet1").UsedRange.Rows.Count J = Worksheets("Sheet2").UsedRange.Rows.Count If J = 1 Then If Application.WorksheetFunction.CountA(Worksheets("Sheet2").UsedRange) = 0 Then J = 0 End If Set xRg = Worksheets("Sheet1").Range("C1:C" & I) On Error Resume Next Application.ScreenUpdating = False For K = 1 To xRg.Count If CStr(xRg(K).Value) = "Done" Then xRg(K).EntireRow.Copy Destination:=Worksheets("Sheet2").Range("A" & J + 1) xRg(K).EntireRow.Delete If CStr(xRg(K).Value) = "Done" Then K = K - 1 End If J = J + 1 End If Next Application.ScreenUpdating = True End Sub
>Solution :
function moverows() < var sh1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); var sh2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2"); if (sh1.getLastRow()==1)var data = sh1.getRange(2,1,sh1.getLastRow()-1,sh1.getLastColumn()).getValues() var archiveData = [] var lignes = [] var ligne = 1 var col = sh1.getLastColumn()-1 try < data.forEach(function (row) < ligne++ if (row[col]=='done') < archiveData.push(row) lignes.push(ligne) >>) sh2.getRange(sh2.getLastRow() + 1, 1, archiveData.length, archiveData[0].length).setValues(archiveData) lignes.reverse().forEach(x => sh1.deleteRow(x)); > catch (e) < >>
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
VBA to JavaScript Translator
mha105/VBA-to-JavaScript-Translator
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
This translator is intended to be used as an educational tool to help VBA developers get familiar with JavaScript.
The code for this tool is based on regex examples found from multiple sources in forums and online. Sorry to say. I did not keep a record of all the sources I leveraged. Just know that people smarter than I am provided the basis for most of the code found here. I simply adjusted what I needed and slapped it all together into a single utility.
As with most translation tools, this tool will NOT perform a 100% complete translation. It is designed to cover common constructs in VBA (enough to get you started).
The idea is to enter a basic block of VBA code (be sure to include your Function/Sub wrappers) and see how the syntax would look in JavaScript. It’s best if you first try something simple then progress into more advanced IF statements, loops, comparison operators, Select Case switches, etc.
Currently, this translator works with:
- Both Functions and Sub Procedures
- Variable Declarations
- Basic IF THEN Statements
- SELECT CASE Statements
- Most Comparison Operators
- Basic FOR x TO y Loops
- Most variations of DO LOOPS
- Basic MSGBOX calls
Known Issues: This tool currently does NOT accurately translate
- Built in VBA functions
- References to Office Objects
- WITH Statements
- STEP Constructs in FOR Loops
- FOR EACH Loops (in fact these cause the tool to return nothing at all. I’m still working out why that is)
I offer the source code up to anyone interested in helping make enhancements to this tool in order to develop a more robust utility for the VBA community.