用vba在excel表中从第二个工作簿开始,依次在每一个sheet的H列第三行的单元格开始往下计算出G列中对应行的值相对上一行的值的增长率,以此类推
时间: 2024-02-27 11:59:23 浏览: 26
非常感谢您的问题!根据您的需求,我提供以下 VBA 代码:
```
Sub calculateGrowthRate()
Dim currentSheet As Worksheet
Dim lastRow As Long
Dim currentRow As Long
Dim currentGValue As Double
Dim lastGValue As Double
'从第二个工作簿开始循环
For Each currentSheet In ThisWorkbook.Worksheets
If currentSheet.Index > 1 Then
lastRow = currentSheet.Cells(Rows.Count, "H").End(xlUp).Row
'从第三行开始循环
For currentRow = 3 To lastRow
currentGValue = currentSheet.Cells(currentRow, "G").Value
lastGValue = currentSheet.Cells(currentRow - 1, "G").Value
'计算增长率并写入对应单元格
If lastGValue <> 0 Then
currentSheet.Cells(currentRow, "H").Value = (currentGValue - lastGValue) / lastGValue
Else
currentSheet.Cells(currentRow, "H").Value = ""
End If
Next currentRow
End If
Next currentSheet
End Sub
```
这段代码会遍历您 Excel 表中的每个工作表,从第二个工作表开始,然后循环该工作表的 H 列第三行开始的每一行,计算 G 列中对应行的值相对于上一行的值的增长率,并将结果写入对应的 H 列单元格。如果上一行的值为 0,则增长率为空。
希望这个代码能够帮到您!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)