Googlesheet - How to skip empty cell in macro loop and continue filled cell?

tipsytappy

New Member
Joined
Apr 13, 2021
Messages
6
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi, would greatly appreciate if anyone is able to help!

I am working on editing a macro that involves vlookup copy/paste data from Worksheet A (IB) to Worksheet B (Inbound POs). However, it copies empty cell in Worksheet A and overwrites the filled cell in Worksheet B which i do not want. Anyone able to help? Code below:

function onEditDoug(e){
//Variables
var row = e.range.getRow();
var col = e.range.getColumn();

var IBDataEntry = "IB";
var IBDataSheet = e.source.getSheetByName(IBDataEntry);
var IBlastRow = IBDataSheet.getLastRow();

var InboundPO = "Inbound POs";
var InboundPOSheet = e.source.getSheetByName(InboundPO);
var InboundlastRow = InboundPOSheet.getLastRow();
var searchRange = InboundPOSheet.getRange(2,2,InboundlastRow,1);
var rangeValues = searchRange.getValues();

//Data Entry for Date Of Approval

if(col === 22 && row > 2 && e.source.getActiveSheet().getName() === IBDataEntry){

var POList3 = IBSheet.getRange("V3:V").getValues();
var LastRow3 = POList3.filter(String).length + 2;
var Entries3 = LastRow3 - row + 1;

for (var x = 0; x < Entries3 ; x++){
var ApprovalPO = IBDataSheet.getRange(row + x, 22).getValue();
var ApprovalDate = IBDataSheet.getRange(row + x, 23).getValue();

for(var j=1;j<=InboundlastRow;j++){
if(rangeValues[j] == ApprovalPO){
InboundPOSheet.getRange(j+2,11).setValue(ApprovalDate);
}
}
}
} SummarySheet.getRange(36,3).setValue(currentDate);
}

Also, somehow the code stops working after awhile as well.
Appreciate the help, thank you so much!!!
 

Attachments

  • Worksheet A.PNG
    Worksheet A.PNG
    45 KB · Views: 19
  • Worksheet B.PNG
    Worksheet B.PNG
    31.6 KB · Views: 17

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
It appears you do not have this variable declared? Maybe it is IBDataSheet ?
Rich (BB code):
var POList3 = IBSheet.getRange("V3:V").getValues();

Maybe add a condition before the second loop:

Rich (BB code):
if (IBDataSheet.getRange(row + x, 23).getValue() != '') {
for(var j=1;j<=InboundlastRow;j++){
if(rangeValues[j] == ApprovalPO){
InboundPOSheet.getRange(j+2,11).setValue(ApprovalDate);
}
}
}
 
Upvote 0
WOW, thank you! That condition definitely did the trick of skipping the empty cell hence did not overwrite important data in Worksheet B . But can you help me figure out why the loop keep stopping? I have to keep copy/paste row 22/23 in Worksheet A again to 'activate' the code. Really appreciate the help as i am very NEW to macros.

Once again, thank you so much for the help!
 
Upvote 0
I just timed it that every time the macro only runs for 30-35seconds then it will automatically stop running. Anyway to ensure that it runs on and on?

Thank you!
 
Upvote 0
Did you fix that IBSheet variable thing? Did it make any difference?
Since your looping criterion is based on Entries3,

maybe check these lines and do some form of debugging to make sure they are all returning the correct values/rows/length etc.
var POList3 = IBSheet.getRange("V3:V").getValues();
var LastRow3 = POList3.filter(String).length + 2;
var Entries3 = LastRow3 - row + 1;
 
Upvote 0
I realised is restriction by GoogleSheet for the function onEdit(e). Do you have any suggestions for me to reduce the loop duration? Thank you so much!

1618390072441.png
 
Upvote 0
Didn't know there are such restrictions.
I guess you can improve it to not do the first loop at all, or at least only loop those row(s) you have edited.
I don't know what's your process like, or what's the main purpose of this piece of code; do you enter the PO #s 1 by 1? or do you paste a whole chunk in?

I am guessing, Entries3 could just be:
var Entries3 = e.range.getNumRows();

So you restrict the looping to the number of rows of your edited range.
 
Upvote 0
Hi, can you help me again for the below code? How to skip empty cells for the macro loop?

Thank you!!


//Data Entry for Actual Delivery Date - Check for Priority List Addition

if (e.source.getActiveSheet().getName() === 'IB' && editedCol === 4 && editedRowStart > 1) {
var ibSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('IB')
var ibSheetData = ibSheet.getDataRange().getValues()
var updatedPOs = []
var updatedDates = []
for (var i = editedRowStart - 1; i < editedRowEnd; i++) {
updatedPOs.push(ibSheetData[1])
updatedDates.push(ibSheetData[3])
}
var updatedObject = Object.assign(...updatedPOs.map((PO, i) => ({[PO]: updatedDates})))
var inboundPOSheet = e.source.getSheetByName('Inbound POs')
var inboundPOSheetLastRow = inboundPOSheet.getLastRow()
var inboundPOSheetData = inboundPOSheet.getDataRange().getValues()
for (var i = 1; i < inboundPOSheetLastRow; i++) {
var inboundPO = inboundPOSheetData[1]
if (updatedPOs.includes(inboundPOSheetData[1])) {
inboundPOSheet.getRange(i+1, 19).setValue(updatedObject[inboundPO])
}
}
} SummarySheet.getRange(36,3).setValue(currentDate);
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top