Excel大神求个VBA代码

查看 86|回复 9
作者:转身有鬼   
[table]
[tr]
[td]家庭[/td]
[td]姓名[/td]
[td]家庭成员类型[/td]
[td]是否成年[/td]
[td]个人花销[/td]
[/tr]
[tr]
[td]家庭一

大神, 代码

Syler   

直接函数试试
列对应你表的顺序
=IF(D2="孩子",0,IF(C2="领导者",SUMIFS(E:E,A:A,A2,D:D,"孩子")+E2,E2))
T4DNA   

[Visual Basic] 纯文本查看 复制代码Sub combineExpenses()
Dim currentRow As Long
Dim currentFamily As String
Dim currentName As String
Dim currentType As String
Dim currentAge As String
Dim currentExpense As Double
Dim leaderRow As Long
Dim leaderExpense As Double
' Start from row 2 (assuming row 1 is the header row)
currentRow = 2
' Loop through all rows until an empty cell is encountered in the "家庭" column
Do Until Cells(currentRow, 1).Value = ""
   
    ' Get the values of the current row
    currentFamily = Cells(currentRow, 1).Value
    currentName = Cells(currentRow, 2).Value
    currentType = Cells(currentRow, 3).Value
    currentAge = Cells(currentRow, 4).Value
    currentExpense = Cells(currentRow, 5).Value
   
    ' Check if the current row is a child
    If currentAge = "孩子" Then
        
        ' Find the row of the leader in the same family
        leaderRow = findLeader(currentFamily)
        
        ' If a leader is found, add the child's expense to the leader's expense
        If leaderRow > 0 Then
            leaderExpense = Cells(leaderRow, 5).Value
            Cells(leaderRow, 5).Value = leaderExpense + currentExpense
            Cells(currentRow, 5).Value = 0
        End If
        
    End If
   
    ' Move to the next row
    currentRow = currentRow + 1
   
Loop
End Sub
Function findLeader(family As String) As Long
Dim currentRow As Long
Dim currentFamily As String
Dim currentType As String
' Start from row 2 (assuming row 1 is the header row)
currentRow = 2
' Loop through all rows until an empty cell is encountered in the "家庭" column
Do Until Cells(currentRow, 1).Value = ""
   
    ' Get the values of the current row
    currentFamily = Cells(currentRow, 1).Value
    currentType = Cells(currentRow, 3).Value
   
    ' If the current row is a leader in the same family, return the row number
    If currentFamily = family And currentType = "领导者" Then
        findLeader = currentRow
        Exit Function
    End If
   
    ' Move to the next row
    currentRow = currentRow + 1
   
Loop
' If no leader is found, return 0
findLeader = 0
End Function
试运行后能够把demo前面的变成第二个表格
T4DNA   

[i]
Syler   


Syler 发表于 2023-3-25 19:53
直接函数试试
列对应你表的顺序

1.判断是否成年
2.判断成员是否是领导者
3,如果是领导者就把该家庭的成员孩子花费求和,然后再加上他的得出总和
clotus   


Syler 发表于 2023-3-25 19:53
直接函数试试
列对应你表的顺序

赞成,直接公式就行了,不需要VBA。
转身有鬼
OP
  


T4DNA 发表于 2023-3-25 19:23
[mw_shl_code=vb,true]Sub combineExpenses()
Dim currentRow As Long

感谢老哥,不过这个结果没有保留原数据,而是直接生成的结果覆盖了第5列。
转身有鬼
OP
  


Syler 发表于 2023-3-25 19:53
直接函数试试
列对应你表的顺序

非常感谢老哥,这个太棒了。想了好久都没有想出来解决方案。
转身有鬼
OP
  


T4DNA 发表于 2023-3-25 19:32

非常感谢大佬。解决方案应该也非常好,不过只能采纳一个,十分抱歉啊。您这个直接运行后也出来了我想要的结果,只是覆盖了原始数据。思路我自己看看你的代码块自己再研究研究,万分感谢了。
转身有鬼
OP
  


clotus 发表于 2023-3-25 21:29
赞成,直接公式就行了,不需要VBA。

确实是,直接出来了我想要的结果。
您需要登录后才可以回帖 登录 | 立即注册

返回顶部