Converting VBA to Google Apps Script
I have the VBA code below. I don't know how it works, but I want to convert it to Google Sheets.
I'm hoping someone can either:
- Explain to me what the VBA is doing so that I can, perhaps, reason it out enough to work on programming it as Google Apps script,
or
- Show me how the same VBA function would be achieved through Google Sheets.
Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range
Dim lastrow As Long
With ThisWorkbook.Worksheets(targetSheet)
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
col = Application.Match(targetDate, .Range("4:4"), 0)
If col = 0 Then
getData = CVErr(xlErrNA)
Exit Function
End If
Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A"))
For Each cell In names
If cell = targetName Then
res = res + cell.Offset(, col - 1)
End If
Next cell
End With
getData = res
End Function
Here's a link to an example excel file where the function is being used:
https://www.dropbox.com/s/h5vcjv9tlh1vvg7/Resources%20and%20Projects%20Full%20Example.xlsm
javascript excel vba excel-vba google-apps-script
add a comment |
I have the VBA code below. I don't know how it works, but I want to convert it to Google Sheets.
I'm hoping someone can either:
- Explain to me what the VBA is doing so that I can, perhaps, reason it out enough to work on programming it as Google Apps script,
or
- Show me how the same VBA function would be achieved through Google Sheets.
Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range
Dim lastrow As Long
With ThisWorkbook.Worksheets(targetSheet)
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
col = Application.Match(targetDate, .Range("4:4"), 0)
If col = 0 Then
getData = CVErr(xlErrNA)
Exit Function
End If
Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A"))
For Each cell In names
If cell = targetName Then
res = res + cell.Offset(, col - 1)
End If
Next cell
End With
getData = res
End Function
Here's a link to an example excel file where the function is being used:
https://www.dropbox.com/s/h5vcjv9tlh1vvg7/Resources%20and%20Projects%20Full%20Example.xlsm
javascript excel vba excel-vba google-apps-script
How do you think it works? Stack Overflow is a question and answer site for professional and enthusiast programmers. So, be an enthusiast. Have a go at it and come back with any specific issues.
– Jean-François Corbett
Jun 19 '14 at 6:46
add a comment |
I have the VBA code below. I don't know how it works, but I want to convert it to Google Sheets.
I'm hoping someone can either:
- Explain to me what the VBA is doing so that I can, perhaps, reason it out enough to work on programming it as Google Apps script,
or
- Show me how the same VBA function would be achieved through Google Sheets.
Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range
Dim lastrow As Long
With ThisWorkbook.Worksheets(targetSheet)
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
col = Application.Match(targetDate, .Range("4:4"), 0)
If col = 0 Then
getData = CVErr(xlErrNA)
Exit Function
End If
Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A"))
For Each cell In names
If cell = targetName Then
res = res + cell.Offset(, col - 1)
End If
Next cell
End With
getData = res
End Function
Here's a link to an example excel file where the function is being used:
https://www.dropbox.com/s/h5vcjv9tlh1vvg7/Resources%20and%20Projects%20Full%20Example.xlsm
javascript excel vba excel-vba google-apps-script
I have the VBA code below. I don't know how it works, but I want to convert it to Google Sheets.
I'm hoping someone can either:
- Explain to me what the VBA is doing so that I can, perhaps, reason it out enough to work on programming it as Google Apps script,
or
- Show me how the same VBA function would be achieved through Google Sheets.
Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range
Dim lastrow As Long
With ThisWorkbook.Worksheets(targetSheet)
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
col = Application.Match(targetDate, .Range("4:4"), 0)
If col = 0 Then
getData = CVErr(xlErrNA)
Exit Function
End If
Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A"))
For Each cell In names
If cell = targetName Then
res = res + cell.Offset(, col - 1)
End If
Next cell
End With
getData = res
End Function
Here's a link to an example excel file where the function is being used:
https://www.dropbox.com/s/h5vcjv9tlh1vvg7/Resources%20and%20Projects%20Full%20Example.xlsm
javascript excel vba excel-vba google-apps-script
javascript excel vba excel-vba google-apps-script
edited Jun 19 '14 at 7:10
Steve K
asked Jun 19 '14 at 4:21
Steve KSteve K
109420
109420
How do you think it works? Stack Overflow is a question and answer site for professional and enthusiast programmers. So, be an enthusiast. Have a go at it and come back with any specific issues.
– Jean-François Corbett
Jun 19 '14 at 6:46
add a comment |
How do you think it works? Stack Overflow is a question and answer site for professional and enthusiast programmers. So, be an enthusiast. Have a go at it and come back with any specific issues.
– Jean-François Corbett
Jun 19 '14 at 6:46
How do you think it works? Stack Overflow is a question and answer site for professional and enthusiast programmers. So, be an enthusiast. Have a go at it and come back with any specific issues.
– Jean-François Corbett
Jun 19 '14 at 6:46
How do you think it works? Stack Overflow is a question and answer site for professional and enthusiast programmers. So, be an enthusiast. Have a go at it and come back with any specific issues.
– Jean-François Corbett
Jun 19 '14 at 6:46
add a comment |
1 Answer
1
active
oldest
votes
Though I am not familiar with Google Apps scripting, I can help you with the first part.
The point of the function appears to be adding up all values where the name
found in column A matches targetName
passed in as a parameter and the date
found in row 4 matches targetDate
, which is also a parameter. (Row is determined by name
and column is determined by date
.) The total value is then returned as a double
.
Here's line by line comments.
Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True 'I don't see a reason for this line
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range
Dim lastrow As Long
With ThisWorkbook.Worksheets(targetSheet) 'All ranges start with ThisWorkbook.'targetSheet'
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 'Get the last row with data in column A to 'lastrow'
col = Application.Match(targetDate, .Range("4:4"), 0) 'Find 'targetDate' in row 4 and set it to 'col'
If col = 0 Then 'Couldn't find 'targetDate'
getData = CVErr(xlErrNA) 'Function returns the appropriate error
Exit Function 'Exit the function after setting the return value
End If
Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A")) 'Setting the range from A4 to A'lastrow' to 'names'
For Each cell In names 'Looping through every 'cell' in the 'names' range
If cell = targetName Then 'If the 'cell' value matches the 'targetName' passed in as a parameter
res = res + cell.Offset(, col - 1) 'Add the value from the column with the 'targetDate' to 'res'
End If
Next cell
End With
getData = res 'Return the total
End Function
Thanks J_V, this is helpful. I should be able to reason something similar out with JS pseudo-code and give it a try as @Jean-François Corbett suggests. Much-appreciated, all. I'll follow up once I've checked some stuff out.
– Steve K
Jun 19 '14 at 7:09
Glad you found it helpful!
– natancodes
Jun 19 '14 at 7:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24298733%2fconverting-vba-to-google-apps-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Though I am not familiar with Google Apps scripting, I can help you with the first part.
The point of the function appears to be adding up all values where the name
found in column A matches targetName
passed in as a parameter and the date
found in row 4 matches targetDate
, which is also a parameter. (Row is determined by name
and column is determined by date
.) The total value is then returned as a double
.
Here's line by line comments.
Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True 'I don't see a reason for this line
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range
Dim lastrow As Long
With ThisWorkbook.Worksheets(targetSheet) 'All ranges start with ThisWorkbook.'targetSheet'
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 'Get the last row with data in column A to 'lastrow'
col = Application.Match(targetDate, .Range("4:4"), 0) 'Find 'targetDate' in row 4 and set it to 'col'
If col = 0 Then 'Couldn't find 'targetDate'
getData = CVErr(xlErrNA) 'Function returns the appropriate error
Exit Function 'Exit the function after setting the return value
End If
Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A")) 'Setting the range from A4 to A'lastrow' to 'names'
For Each cell In names 'Looping through every 'cell' in the 'names' range
If cell = targetName Then 'If the 'cell' value matches the 'targetName' passed in as a parameter
res = res + cell.Offset(, col - 1) 'Add the value from the column with the 'targetDate' to 'res'
End If
Next cell
End With
getData = res 'Return the total
End Function
Thanks J_V, this is helpful. I should be able to reason something similar out with JS pseudo-code and give it a try as @Jean-François Corbett suggests. Much-appreciated, all. I'll follow up once I've checked some stuff out.
– Steve K
Jun 19 '14 at 7:09
Glad you found it helpful!
– natancodes
Jun 19 '14 at 7:13
add a comment |
Though I am not familiar with Google Apps scripting, I can help you with the first part.
The point of the function appears to be adding up all values where the name
found in column A matches targetName
passed in as a parameter and the date
found in row 4 matches targetDate
, which is also a parameter. (Row is determined by name
and column is determined by date
.) The total value is then returned as a double
.
Here's line by line comments.
Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True 'I don't see a reason for this line
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range
Dim lastrow As Long
With ThisWorkbook.Worksheets(targetSheet) 'All ranges start with ThisWorkbook.'targetSheet'
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 'Get the last row with data in column A to 'lastrow'
col = Application.Match(targetDate, .Range("4:4"), 0) 'Find 'targetDate' in row 4 and set it to 'col'
If col = 0 Then 'Couldn't find 'targetDate'
getData = CVErr(xlErrNA) 'Function returns the appropriate error
Exit Function 'Exit the function after setting the return value
End If
Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A")) 'Setting the range from A4 to A'lastrow' to 'names'
For Each cell In names 'Looping through every 'cell' in the 'names' range
If cell = targetName Then 'If the 'cell' value matches the 'targetName' passed in as a parameter
res = res + cell.Offset(, col - 1) 'Add the value from the column with the 'targetDate' to 'res'
End If
Next cell
End With
getData = res 'Return the total
End Function
Thanks J_V, this is helpful. I should be able to reason something similar out with JS pseudo-code and give it a try as @Jean-François Corbett suggests. Much-appreciated, all. I'll follow up once I've checked some stuff out.
– Steve K
Jun 19 '14 at 7:09
Glad you found it helpful!
– natancodes
Jun 19 '14 at 7:13
add a comment |
Though I am not familiar with Google Apps scripting, I can help you with the first part.
The point of the function appears to be adding up all values where the name
found in column A matches targetName
passed in as a parameter and the date
found in row 4 matches targetDate
, which is also a parameter. (Row is determined by name
and column is determined by date
.) The total value is then returned as a double
.
Here's line by line comments.
Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True 'I don't see a reason for this line
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range
Dim lastrow As Long
With ThisWorkbook.Worksheets(targetSheet) 'All ranges start with ThisWorkbook.'targetSheet'
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 'Get the last row with data in column A to 'lastrow'
col = Application.Match(targetDate, .Range("4:4"), 0) 'Find 'targetDate' in row 4 and set it to 'col'
If col = 0 Then 'Couldn't find 'targetDate'
getData = CVErr(xlErrNA) 'Function returns the appropriate error
Exit Function 'Exit the function after setting the return value
End If
Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A")) 'Setting the range from A4 to A'lastrow' to 'names'
For Each cell In names 'Looping through every 'cell' in the 'names' range
If cell = targetName Then 'If the 'cell' value matches the 'targetName' passed in as a parameter
res = res + cell.Offset(, col - 1) 'Add the value from the column with the 'targetDate' to 'res'
End If
Next cell
End With
getData = res 'Return the total
End Function
Though I am not familiar with Google Apps scripting, I can help you with the first part.
The point of the function appears to be adding up all values where the name
found in column A matches targetName
passed in as a parameter and the date
found in row 4 matches targetDate
, which is also a parameter. (Row is determined by name
and column is determined by date
.) The total value is then returned as a double
.
Here's line by line comments.
Function getData(targetName As String, targetSheet As String, targetDate)
Application.Volatile True 'I don't see a reason for this line
Dim res As Double
Dim col As Integer
Dim cell As Range
Dim names As Range
Dim lastrow As Long
With ThisWorkbook.Worksheets(targetSheet) 'All ranges start with ThisWorkbook.'targetSheet'
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 'Get the last row with data in column A to 'lastrow'
col = Application.Match(targetDate, .Range("4:4"), 0) 'Find 'targetDate' in row 4 and set it to 'col'
If col = 0 Then 'Couldn't find 'targetDate'
getData = CVErr(xlErrNA) 'Function returns the appropriate error
Exit Function 'Exit the function after setting the return value
End If
Set names = .Range(.Cells(4, "A"), .Cells(lastrow, "A")) 'Setting the range from A4 to A'lastrow' to 'names'
For Each cell In names 'Looping through every 'cell' in the 'names' range
If cell = targetName Then 'If the 'cell' value matches the 'targetName' passed in as a parameter
res = res + cell.Offset(, col - 1) 'Add the value from the column with the 'targetDate' to 'res'
End If
Next cell
End With
getData = res 'Return the total
End Function
answered Jun 19 '14 at 5:09
natancodesnatancodes
713710
713710
Thanks J_V, this is helpful. I should be able to reason something similar out with JS pseudo-code and give it a try as @Jean-François Corbett suggests. Much-appreciated, all. I'll follow up once I've checked some stuff out.
– Steve K
Jun 19 '14 at 7:09
Glad you found it helpful!
– natancodes
Jun 19 '14 at 7:13
add a comment |
Thanks J_V, this is helpful. I should be able to reason something similar out with JS pseudo-code and give it a try as @Jean-François Corbett suggests. Much-appreciated, all. I'll follow up once I've checked some stuff out.
– Steve K
Jun 19 '14 at 7:09
Glad you found it helpful!
– natancodes
Jun 19 '14 at 7:13
Thanks J_V, this is helpful. I should be able to reason something similar out with JS pseudo-code and give it a try as @Jean-François Corbett suggests. Much-appreciated, all. I'll follow up once I've checked some stuff out.
– Steve K
Jun 19 '14 at 7:09
Thanks J_V, this is helpful. I should be able to reason something similar out with JS pseudo-code and give it a try as @Jean-François Corbett suggests. Much-appreciated, all. I'll follow up once I've checked some stuff out.
– Steve K
Jun 19 '14 at 7:09
Glad you found it helpful!
– natancodes
Jun 19 '14 at 7:13
Glad you found it helpful!
– natancodes
Jun 19 '14 at 7:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24298733%2fconverting-vba-to-google-apps-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How do you think it works? Stack Overflow is a question and answer site for professional and enthusiast programmers. So, be an enthusiast. Have a go at it and come back with any specific issues.
– Jean-François Corbett
Jun 19 '14 at 6:46