请参见下表,我要将小计放在最后一行的下方1行。对于C行和D行(使用下面的代码),此方法效果很好,但是其余各列并不总是在每行中都有信息。
$result = mysqli_query($db, "SELECT Distinct a.*,b.* FROM OrderCalculation a right join crm_order b on a.orderid = b.orderno");
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
/*******Start of Formatting for Excel*******/
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character
//start of printing column names as names of MySQL fields
//for ($i = 0; $i < mysql_num_fields($result); $i++) {
//echo mysqli_fetch_field_direct($result,$i) . "\t";
//}
//print("\n");
//end of printing column names
//start while loop to get data
while($row = mysqli_fetch_array($result))
{
$schema_insert = "";
for($j=0; $j<mysqli_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
当我在同一行中都需要它们时,如何在其他小计(以黄色突出显示)的右边插入公式?
答案 0 :(得分:2)
只需使用一列来确定最后一行(此处使用Column A
)
此外,您的总和范围的底部由最后一行(LR
)表示,但是您想让公式进入的位置是下面的2行(因此LR + 2
)
Dim ws as Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") '<-- Update
Dim LR as Long
LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
ws.Range("C" & LR + 2).Formula = "=Sum(C2:C" & LR & ")"
ws.Range("D" & LR + 2).Formula = "=Sum(D2:D" & LR & ")"
答案 1 :(得分:0)
尝试:
Option Explicit
Sub test()
Dim LastRow As Long, LastColumn As Long, Column As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For Column = 3 To 10
.Cells(LastRow + 2, Column) = Application.WorksheetFunction.Sum(.Range(.Cells(2, Column), Cells(LastRow, Column)))
Next Column
End With
End Sub