对于 Google 张中的每个循环? VBA 例如
For Each Loop in Google Sheets? VBA For Example
在图1中,绿色单元格表示带有公式的列。我想在第 13 行中求和的唯一列是其中没有公式的列,以及 = "Upfront Costs" 并与第 10 行合并的列。所以在图 1 中,答案是 $97( (A13:F13) - D13 的总和)。
图片1
VBA 图 1 例子
Sub test()
Dim cl As Object, count As Double
For Each cl In Sheets("Sheet1").Range("10:10")
If cl.MergeCells Then
If cl.Offset(3, 0).HasFormula = False And cl.MergeArea.Cells(1, 1).Value = "Upfront Costs" Then
count = count + cl.Offset(3, 0).Value
End If
Else
MsgBox count
Exit Sub
End If
Next cl
End Sub
在图 2 中,如果 "Upfront Costs" 是从 "B10:L10" 合并而来的,则答案为 $289
((B13:L13) - D13 的总和)。
图2
在图 3 的示例中,如果 "Upfront Costs" 是从 "L10:N10" 合并而来的,答案将是 $122(L13:N13 的总和)
图3
对低于合并范围的单元格求和
此函数假定没有其他合并范围与此行相交,并且字符串 "Upfront Costs" 始终位于合并范围的最左侧单元格,必须位于该单元格中才能看到。我们也可以用背景颜色来做到这一点。
function runTwo() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getRange(10,1,1,sh.getLastColumn());
if(rg.getMergedRanges()[0].getValue()=="Upfront Costs") {
var mrg=rg.getMergedRanges()[0];
var vA=sh.getRange(13,mrg.getColumn(),1,mrg.getWidth()).getDisplayValues()[0];
var fA=sh.getRange(13,mrg.getColumn(),1,mrg.getWidth()).getFormulas()[0];
var sum=0;
vA.forEach(function(e,i){if(!fA[i]){sum+=Number(e);}})
SpreadsheetApp.getUi().alert('Range: ' + mrg.getA1Notation() + ' Sum: ' + sum);
return sum;
}else{
SpreadsheetApp.getUi().alert('Does not meet criteria');
}
}
你可以这样做:
function fnct_sum()
{
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var cl , count=0 ;
var yourRange = sheet.getRange("10:10");
for (var i = 1; i < yourRange.getNumColumns()+1; i++)
{
cl=yourRange.getCell(1, i);
if (cl.isPartOfMerge())
{
Logger.log(cl.getMergedRanges()[0].getCell(1, 1).getValue());
if (cl.offset(3, 0).getFormula()=="" && cl.getMergedRanges()[0].getCell(1, 1).getValue()=='Upfront Costs')
{
count = count + cl.offset(3, 0).getValue();
}
else
{
Logger.log (count);
//break;
}
}
}
Logger.log (count);
};
在图1中,绿色单元格表示带有公式的列。我想在第 13 行中求和的唯一列是其中没有公式的列,以及 = "Upfront Costs" 并与第 10 行合并的列。所以在图 1 中,答案是 $97( (A13:F13) - D13 的总和)。
图片1
VBA 图 1 例子
Sub test()
Dim cl As Object, count As Double
For Each cl In Sheets("Sheet1").Range("10:10")
If cl.MergeCells Then
If cl.Offset(3, 0).HasFormula = False And cl.MergeArea.Cells(1, 1).Value = "Upfront Costs" Then
count = count + cl.Offset(3, 0).Value
End If
Else
MsgBox count
Exit Sub
End If
Next cl
End Sub
在图 2 中,如果 "Upfront Costs" 是从 "B10:L10" 合并而来的,则答案为 $289 ((B13:L13) - D13 的总和)。
图2
在图 3 的示例中,如果 "Upfront Costs" 是从 "L10:N10" 合并而来的,答案将是 $122(L13:N13 的总和)
图3
对低于合并范围的单元格求和
此函数假定没有其他合并范围与此行相交,并且字符串 "Upfront Costs" 始终位于合并范围的最左侧单元格,必须位于该单元格中才能看到。我们也可以用背景颜色来做到这一点。
function runTwo() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getRange(10,1,1,sh.getLastColumn());
if(rg.getMergedRanges()[0].getValue()=="Upfront Costs") {
var mrg=rg.getMergedRanges()[0];
var vA=sh.getRange(13,mrg.getColumn(),1,mrg.getWidth()).getDisplayValues()[0];
var fA=sh.getRange(13,mrg.getColumn(),1,mrg.getWidth()).getFormulas()[0];
var sum=0;
vA.forEach(function(e,i){if(!fA[i]){sum+=Number(e);}})
SpreadsheetApp.getUi().alert('Range: ' + mrg.getA1Notation() + ' Sum: ' + sum);
return sum;
}else{
SpreadsheetApp.getUi().alert('Does not meet criteria');
}
}
你可以这样做:
function fnct_sum()
{
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var cl , count=0 ;
var yourRange = sheet.getRange("10:10");
for (var i = 1; i < yourRange.getNumColumns()+1; i++)
{
cl=yourRange.getCell(1, i);
if (cl.isPartOfMerge())
{
Logger.log(cl.getMergedRanges()[0].getCell(1, 1).getValue());
if (cl.offset(3, 0).getFormula()=="" && cl.getMergedRanges()[0].getCell(1, 1).getValue()=='Upfront Costs')
{
count = count + cl.offset(3, 0).getValue();
}
else
{
Logger.log (count);
//break;
}
}
}
Logger.log (count);
};