写一个VBA按列区分,逐行相除得到的结果写到最后一行
时间: 2024-02-09 10:10:09 浏览: 33
这是一个IT类问题,我可以回答。
以下是实现该功能的VBA代码:
```
Sub DivideColumns()
Dim lastRow As Long
Dim lastCol As Long
Dim i As Long
Dim j As Long
Dim result As Double
lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
For j = 1 To lastCol - 1
result = ActiveSheet.Cells(1, j)
For i = 2 To lastRow
If ActiveSheet.Cells(i, j) <> 0 Then
result = result / ActiveSheet.Cells(i, j)
End If
Next i
ActiveSheet.Cells(lastRow + 1, j) = result
Next j
End Sub
```
该代码的作用是按列区分,逐行相除,并将结果写到最后一行。您可以将其复制到 VBA 编辑器中,并在需要的工作表上运行该代码。
相关问题
写一个VBA按列区分,逐行相除得到的和写到最后一行
这是一个IT类问题。
以下是一个示例代码,可以实现按列区分,逐行相除得到的和写到最后一行的功能:
```
Sub DivideByColumn()
Dim lastRow As Long
Dim numCols As Integer
Dim i As Integer, j As Integer
Dim total As Double
Dim cellValue As Double
'确定最后一行和列数
lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
numCols = ActiveSheet.UsedRange.Columns.Count
'逐列进行计算
For i = 1 To numCols
total = 0
'计算当前列的总和
For j = 1 To lastRow
cellValue = Cells(j, i).Value
If IsNumeric(cellValue) Then
total = total + cellValue
End If
Next j
'逐行进行除法运算,并将结果写入当前列的最后一行
For j = 1 To lastRow
cellValue = Cells(j, i).Value
If IsNumeric(cellValue) And total <> 0 Then
Cells(j, i).Value = cellValue / total
End If
'将每行的结果累加到最后一行
If i = numCols Then
Cells(lastRow + 1, 1).Value = Cells(lastRow + 1, 1).Value + Cells(j, i).Value
End If
Next j
Next i
End Sub
```
这个宏可以将当前活动工作表中的数据逐列进行计算,并将每行的结果除以当前列的总和,最后将每行的结果累加到最后一行的第一个单元格中。请注意,此代码仅适用于数字数据,如果数据中包含非数字,则需要相应地进行修改。
使用java写一段两个整数相除结果显示以分数形式显示
好的,以下是Java代码,可以将两个整数相除的结果以分数形式显示:
```
import java.util.Scanner;
public class FractionalDivision {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个整数:");
int num1 = input.nextInt();
System.out.print("请输入第二个整数:");
int num2 = input.nextInt();
int gcd = getGcd(num1, num2);
int numerator = num1 / gcd;
int denominator = num2 / gcd;
System.out.println(num1 + " / " + num2 + " = " + numerator + " / " + denominator);
}
public static int getGcd(int num1, int num2) {
if (num2 == 0) {
return num1;
}
return getGcd(num2, num1 % num2);
}
}
```
这里的主要思路是先求出两个整数的最大公约数,然后将两个整数分别除以最大公约数,得到的就是两个整数的约分结果。最后以分数的形式进行输出。
相关推荐
![](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)
![](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)