Google sheets macro for autofilling values - excel

I'm working on a spreadsheet which without macros would require a couple of million cells containing formulas, I'd like a macro which would take the formula in the top row of a selection, and then copy/autofill formula in to rows further down the sheet, and then replace those rows with values once the formulas have calculated.
As there are about 20,000 rows with ~20 columns, and several sheets which are like this, even if I ran a macro which would copy the formulas down through every sheet and then replace them with values, it would still be very laggy, so I'd like it to only copy the formulas in to rows where a cell in a specific column isn't empty (there is an input cell in each row), as well as only copying to rows where the cells to be filled are empty.
If we take the example of a simple setup of column 1 being for input, and column 2 being a formula based on it, and I had already run this after entering inputs up to the second row, and then entered stuff into the 3rd and 4th row too, with something that looked like this. I'd want the macro when run to copy the formulas in to just row 3 and 4 cells, then replace them with values.
I'm also uncertain how I could get formulas in non adjacent columns to be copied down through the same rows, as they can't be shown in one range.
Right now I have 2 basic macros, one which would copy formulas down through a set range, and one which would replace it with values, as when I had them in the same macro it seemed that the replacement would occur before the formulas had loaded, so it would be replaced with nothing.
If what I'm aiming for isn't possible with google sheets macros, but is possible with VBA in excel, I'm not ruling out the possibility I'll have to try and switch to excel, but would much prefer doing it in google sheets.
Thanks in advance for any help.
Edit: This is the very basic setup I have right now:
function Autofillformulas() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A4:C5000').activate();
spreadsheet.getRange('A4:C4').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
function Autofillvalues2() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A5:C5000').activate();
spreadsheet.getRange('A5:C5000').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
Edit 2: Thanks to comments I've now got it in to one macro:
function Autofillformulas() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A4:C5000').activate();
spreadsheet.getRange('A4:C4').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
SpreadsheetApp.flush()
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A5:C5000').activate();
spreadsheet.getRange('A5:C5000').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};

Related

Find discrepancy between specific columns in 2 worksheets

I am working with excel, let's say worksheet ABC with columns C3:O102 contains "-" while worksheet DEF with columns C3:O102 contains "yes". I want the cells with discrepancy to return "Discrepancy". I tried excel formulas, I couldn't figure it out so I tried with VBA and it didn't run.
Sub IF_Then()
If Worksheets("Sheet15").Range("C3:O102").Value = "-" And Worksheets("Sheet30").Range("C3:O102").Value = "yes" Then
Worksheets("Sheet30").Range("AJ3:AJ103").Value = "Discrepancy"
End If
End Sub
This can easily be done, using the IF() worksheet function, as in my example: I have created two sheets (Blad1 and Blad2), I have filled in cells "A1:A5", and in a third sheet, I've added the following formula in cells "A1:A5" (starting in cell "A1" and dragging down):
=IF(Blad1!A1 = Blad2!A1;Blad1!A1;"Discrepancy")
(Keep out, my regional settings require semicolons inside a formula, some require commas.)
I ended up using it without VBA. I got accurate results with this excel formula. The formula was really long because I was working with a lot of sheets and kind of like dissimilar data.
=IF(AND(INDEX(MATCH),(MATCH)))
I appreciate the contribution

How to only run a formula if the cell is blank

I am writing a formula to run a matching analysis on some elements of a spreadsheet. The column containing the formula already has values filled in, but I need to run the formula on the blank cells. The formula is working but overwriting the current info I have within the filled cells.
I have tried a VBA code which got too complicated and did not work. With VBA I tried inserting the formula into only the blank spaces but was having some issues. I have also tried a basic excel formula.
=IF(COUNTIFS(A:A,A2,BU:BU,"CLOUD")=0,"Not Cloud",IF(COUNTIFS(A:A,A2,BU:BU,"NOT CLOUD")=0,"Cloud","Hybrid"))
VBA Code I tried:
If (IsEmpty(Bucket) Or Bucket = "" Or Bucket = vbNullString) And _
(GetCellValue(.Cells(i, "BU")) = "CLOUD") Then
Range("CC:CC").Formula = "=IF(COUNTIFS(A:A,A2,BU:BU,""CLOUD"")=0,"Not Cloud", _
IF(COUNTIFS(A:A,A2,BU:BU,"NOT CLOUD")=0,"Cloud"","Hybrid"))"
A reminder this is a high-level version
I want that formula to only run in the cells that do not have a value already in place. It should not overwrite the data from the VBA script that I previously ran before doing the formula. The disposition column should be completely full of values. The formula needs to run in only the blank cells.
You could achieve this with an additional column.
In the column to the right of Disposition (insert one if necessary) write the following formula (example for row 2, assuming Disposition is Column C):
=IF(C2=""; [Your formula here]; C2)
This will provide you with a column full of values, anytime you put a value into column C it will override the formula.
Edit: You can then copy this formula down the entire column to achieve the desired effect.

Macro is not reading the correct value from a cell which contains a math formula

I have a Excel Workbook that is a template and is populated using macros.
In a several cells, I have a formula =abs(sum(H20:P20)).
In my macro, I am trying to hide the row based on the value in that cell.
I reference those cells from a named range using an offset that does not change and then iterate over 8 lines to decide whether to hide them or not.
I have tried various 'Evaluate' and 'Calculate' functions on the cells in question thinking that maybe it was not evaluating the formula in the cell.
For s = 1 To 8
If Worksheets(sheet1).Range("Spend").Offset(s, 15).Value = 0 Then
Worksheets(sheet1).Range("Spend").Offset(s, 15).EntireRow.Hidden = True
End If
Next s
I expect it to hide several of the rows but it does not do anything. After all macros are done in the file; and the rows have not been hidden, I run that same macro again. On this second run, it hides the rows correctly.

Using Excel to conditionally concatenate multiple rows

I have a large data file that has some broken rows from the extraction process. I'm trying to clean it up in excel and am running into an issue with multiple row checking. I could nest a set amount of IF statements in my formula but that runs the risk of missing values. A Sample of the data looks like this (The strings are pasted into Column A):
'ID_Value','last_name','first_name','','dob','gender'comment
comment
comment
comment'
'ID_Value','last_name','first_name','','dob','gender'comment'
'ID_Value','last_name','first_name','','dob','gender'comment'
'ID_Value','last_name','first_name','','dob','gender'comment
comment
comment
comment
comment'
I need to roll the comment rows into the normal row preceding them. Currently I can identify all the rows and can make it work when there are two comment rows but going beyond that I am at a loss.
Formula in Column B: =IF(LEFT(A1, 1) = "'", "IGNORE", "FLAG")
Formula in Column C: =(IF(B2 = "FLAG", IF(B1 = "FLAG", "MOVE")))
Formula in Column D: =IF(B2 = "FLAG", IF(B1 = "FLAG", IF(C2 = "MOVE", CONCATENATE(A1, A2))))
Any recommendations are greatly appreciated.
Here you go this should do it.
Download the spreadsheet here.
Spreadsheet Example
Paste your data into Sheet 1 Cell A1 and click the run_filter button.
Alternatively you can first run it with the dummy data that already present in Sheet1 and check to see that the results work as expected.
A couple of caveats.
The macro will stop When it encounter a blank cell in Column A. This is how I determine that the macro should end.
Comments should contain no more than 5 commas otherwise they'll be interpreted as real data.
A space character is added to comments.
The first row should not contain a comment.
A new worksheet called Filtered_Result will be created with the required result.
If you want to view the code click on the developer tab.
If it's not present See here for How to add it tab
Then Click the Macro Button.
Click the edit button on the macro combine_comments to see the code.
Give it a try and let me know how it goes.
Peter H

How to merge data from different sheets that use the same template?

I have one spreadsheet which has 5 sheets
All the sheets share the same template, so they are alike. The only difference is the data
I would like to make another sheet, whichs gets all the data from column A3 (from row 3 to the end) in sheet 1,2,3,4,5, and puts it in 1 column in my new sheet.
How is this possible?
I am using Google Docs, but i guess Excel and Google Docs are much alike.
Thanks
=CONCATENATE(Sheet1!A:A,",",Sheet2!A:A,",",Sheet3!A:A,",",Sheet4!A:A,",",sheet5!(A:A))
This will concatenate A1,A1,A1,A1,A1 for sheets 1-5 on sheet 6. Drag it down to concatenate by cells (A2 then A3). You don't need to define the ranges A:A but you can if you want.
By far the best answer I've found to this question is found here: http://www.jessespevack.com/systems-leadership/2015/4/22/pulling-spreadsheet-data-no-scripts-required. In essence use ImportRange to pull in data from several other sheets. Then wrap those in an ArrayFormula so that they appear one after the other on the sheet. Lastly wrap the ArrayFormula in a Sort so that the blank lines are all left at the end.
Say you have Sheet1, Sheet2 and Sheet3 and you would like to combine columns A through E on MergeSheet. Put the column headings in the cells MergeSheet!A1:E1
Then in cell A2 enter a formula like this:
=SORT(
ARRAYFORMULA({
IMPORTRANGE("https://docs.google.com/spreadsheets/d/UniqueKey","Sheet1!A2:E");
IMPORTRANGE("https://docs.google.com/spreadsheets/d/UniqueKey","Sheet2!A2:E");
IMPORTRANGE("https://docs.google.com/spreadsheets/d/UniqueKey","Sheet3!A2:E")
,1,TRUE}))
The URL is the url of the spreadsheet, and can be copied from the address bar of the browser.
It's best to test that the IMPORTRANGE function works for each range individually before combining them into the one long function.
I don't think that you have a built-in function for such functionality.
You can easily write an Apps Script that performs this
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var newData = new Array();
for (var i = 1; i <= 5; i++) {
var s = sheets[i];
var r = s.getRange("A:A");
// Go over the values and fill up a new range in sheet 6...
var data = r.getValues();
for(i in data){
var row = data[i];
newData.push(row);
}
}
This is the simpliest solution to merge multiple sheets of multiple google spreadsheets (with the same columns)
=SORT(
{
IMPORTRANGE("unique_spreadsheet_key1";"'Your first sheet'!A2:G");
IMPORTRANGE("unique_spreadsheet_key2";"'Your second sheet'!A2:G");
IMPORTRANGE("unique_spreadsheet_key2";"'Your third sheet'!A2:G")
};
3; TRUE
)
Based on dcb solution, but corrected (the SORT part) and without ARRAYFORMULA!
You can change 3 to sort your merged sheet by the column of your need !
unique_spreadsheet_key : just the key, doesn't neet complete url

Resources