|
楼主 |
发表于 2023-4-18 10:11
|
显示全部楼层
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Private Sub Worksheet_UpdateData()
Dim lastRow As Long
Dim i As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row '获取最后一行
For i = 1 To lastRow '循环遍历每一行
If InStr(Cells(i, "A").Value, "数据") > 0 Then '如果A列包含“数据”
Cells(i, "H").Value = 30# '将H列赋值为30.00
Cells(i, "I").Value = Cells(i, "G").Value * Cells(i, "H").Value '将G列和H列的乘积放于I列
Cells(i, "N").Value = 40# '将N列赋值为40.00
Cells(i, "O").Value = Cells(i, "M").Value * Cells(i, "N").Value '将M列和N列的乘积放于O列
Cells(i, "T").Value = 40# '将T列赋值为40.00
Cells(i, "U").Value = Cells(i, "S").Value * Cells(i, "T").Value '将S列和T列的乘积放于U列
Cells(i, "Z").Value = 40# '将Z列赋值为40.00
Cells(i, "AA").Value = Cells(i, "Y").Value * Cells(i, "Z").Value '将Y列和Z列的乘积放于AA列
Cells(i, "AC").Value = Cells(i, "I").Value + Cells(i, "O").Value + Cells(i, "U").Value + Cells(i, "AA").Value '将I列、O列、U列和AA列的和放于AC列
Cells(i, "AB").Value = Cells(i, "G").Value + Cells(i, "M").Value + Cells(i, "S").Value + Cells(i, "Y").Value '计算金额合计
Range("B" & i & ":AC" & i).HorizontalAlignment = xlCenter '水平居中对齐
Range("B" & i & ":AC" & i).VerticalAlignment = xlCenter '垂直居中对齐
Range("H" & i & ":I" & i).NumberFormat = "0.00" '将H列和I列设置为两位小数
Range("N" & i & ":O" & i).NumberFormat = "0.00" '将N列和O列设置为两位小数
Range("T" & i & ":U" & i).NumberFormat = "0.00" '将T列和U列设置为两位小数
Range("Z" & i & ":AA" & i).NumberFormat = "0.00" '将Z列和AA列设置为两位小数
End If
If InStr(Cells(i, "A").Value, "班组") > 0 Then '如果A列包含“班组”
Cells(i, "D").Value = "日期" '将D列赋值为"日期"
Cells(i, "E").Value = "时间" '将E列赋值为"时间"
Cells(i, "F").Value = "事由" '将F列赋值为"事由"
Cells(i, "G").Value = "天数" '将G列赋值为"天数"
Cells(i, "H").Value = "报销标准" '将H列赋值为"报销标准"
Cells(i, "I").Value = "金额" '将I列赋值为"金额"
Cells(i, "J").Value = "日期" '将D列赋值为"日期"
Cells(i, "K").Value = "时间" '将E列赋值为"时间"
Cells(i, "L").Value = "事由" '将F列赋值为"事由"
Cells(i, "M").Value = "天数" '将G列赋值为"天数"
Cells(i, "N").Value = "报销标准" '将H列赋值为"报销标准"
Cells(i, "O").Value = "金额" '将I列赋值为"金额"
Cells(i, "P").Value = "日期" '将D列赋值为"日期"
Cells(i, "Q").Value = "时间" '将E列赋值为"时间"
Cells(i, "R").Value = "事由" '将F列赋值为"事由"
Cells(i, "S").Value = "天数" '将G列赋值为"天数"
Cells(i, "T").Value = "报销标准" '将H列赋值为"报销标准"
Cells(i, "U").Value = "金额" '将I列赋值为"金额"
Cells(i, "V").Value = "日期" '将D列赋值为"日期"
Cells(i, "W").Value = "时间" '将E列赋值为"时间"
Cells(i, "X").Value = "事由" '将F列赋值为"事由"
Cells(i, "Y").Value = "天数" '将G列赋值为"天数"
Cells(i, "Z").Value = "报销标准" '将H列赋值为"报销标准"
Cells(i, "AA").Value = "金额" '将I列赋值为"金额"
Range("B" & i & ":AC" & i).HorizontalAlignment = xlCenter '水平居中对齐
Range("B" & i & ":AC" & i).VerticalAlignment = xlCenter '垂直居中对齐
End If
If Range("A" & i).Value = "维护部" Then '判断A列是否为“维护部”
Range("B" & i & ":B" & i + 1).Merge '合并B列此行和下一行
Range("B" & i).Value = "序号"
Range("C" & i & ":C" & i + 1).Merge '合并B列此行和下一行
Range("C" & i).Value = "姓名"
Range("AB" & i & ":AB" & i + 1).Merge '合并AB列此行和下一行
Range("AB" & i).Value = "天数合计"
Range("AC" & i & ":AC" & i + 1).Merge '合并AC列此行和下一行
Range("AC" & i).Value = "金额合计"
Range("D" & i & ":I" & i).Merge '合并D列到I列
Range("D" & i).Value = "一级" '填入“一级”
Range("J" & i & ":O" & i).Merge '合并J列到O列
Range("J" & i).Value = "二级(清淤)" '填入“二级(清淤)”
Range("P" & i & ":U" & i).Merge '合并P列到U列
Range("P" & i).Value = "三级" '填入“三级”
Range("V" & i & ":AA" & i).Merge '合并P列到U列
Range("V" & i).Value = "四级" '填入“四级”
Range("D" & i & ":AC" & i).Font.Bold = True '加粗字体
Range("B" & i & ":AC" & i).HorizontalAlignment = xlCenter '水平居中对齐
Range("B" & i & ":AC" & i).VerticalAlignment = xlCenter '垂直居中对齐
Range("B" & i & ":AC" & i).Borders.LineStyle = xlContinuous '添加边框
Range("B" & i & ":AC" & i).Borders.Weight = xlThin '设置边框粗细
End If
Next i
Application.DisplayAlerts = False
ActiveSheet.Unprotect Password:="122333"
' 复制和粘贴代码
ActiveSheet.Protect Password:="122333"
Application.DisplayAlerts = True
End Sub
这种还是不行呀,还在会报错。
|
|