VBA 根据单元格改变的值改变对应单元格的值

2024-09-06 00:12
文章标签 对应 改变 vba 单元格

本文主要是介绍VBA 根据单元格改变的值改变对应单元格的值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

VBA 根据单元格改变的值改变对应单元格的值


Private Sub Worksheet_Change(ByVal Target As Range)10    On Error GoTo er:20      Application.EnableEvents = False30        If Intersect(Target, Range("I6")) Is Nothing = False Then' Range("I6").Formula = "=IF(SUM(INDIRECT(""$I$16:$I$2015""),INDIRECT(""$G$16:$G$2015""))=0,"""",SUM(INDIRECT(""$I$16:$I$2015""),INDIRECT(""$G$16:$G$2015"")))"
40            Range("I6").Formula = "=IF(SUM(INDIRECT(""$G$16:$G$2015""))=0,"""",SUM(INDIRECT(""$G$16:$G$2015"")))"50        End If60         If Intersect(Target, Range("I9")) Is Nothing = False Then
70           Range("I9").Formula = "=IF(SUM(INDIRECT(""$I$6""),INDIRECT(""$I$7""))*INDIRECT(""$I$8"")=0,"""",SUM(INDIRECT(""$I$6""),INDIRECT(""$I$7""))*INDIRECT(""$I$8""))"
80        End If90        If Intersect(Target, Range("I10")) Is Nothing = False Then
100          Range("I10").Formula = "=IF(sum($I$6,$I$7,$I$9)=0,"""",sum($I$6,$I$7,$I$9))"
110       End If120       If Intersect(Target, Range("N10")) Is Nothing = False Then
130          Range("N10").Formula = "=IF(sum($I$10,0)=0,"""",ROUND($I$10,2)*ROUND($M$10,2))"
140       End If150       If Intersect(Target, Range("N15")) Is Nothing = False Then
160          Range("N15").Formula = "=IF(SUM(INDIRECT(""$N$16:$N$2015""))=0,"""",SUM(INDIRECT(""$N$16:$N$2015"")))"
170       End If'    If Intersect(Target, Range("Q15")) Is Nothing = False Then'       Range("Q15").Formula = "=IF(SUM(INDIRECT(""$Q$16:$Q$2015""))=0,"""",SUM(INDIRECT(""$Q$16:$Q$2015"")))"'    End If''    If Intersect(Target, Range("R15")) Is Nothing = False Then'       Range("R15").Formula = "=IF(SUM(INDIRECT(""$R$16:$R$2015""))=0,"""",SUM(INDIRECT(""$R$16:$R$2015"")))"'    End If'180     Application.EnableEvents = True190    If Intersect(Target, Range("D16:D2015,F16:G2015,L16:N2015")) Is Nothing = False And ckEnableEvents = True ThenDim strFormulaCLPrice As StringDim intResultCLPrice  As DoubleDim strFormulaCLAmount As StringDim intResultCLAmount  As DoubleDim strFormulaCLLR As StringDim intResultCLLR  As Double200          strFormulaCLPrice = "{材料成本}+{材料加价}"     '//材料单价
210          strFormulaCLAmount = "{材料数量}*{材料单价}"    '//材料金额
220          strFormulaCLLR = "{材料数量}*{材料加价}"       '//材料利润230          Application.EnableEvents = False'------材料-----------
240          strFormulaCLPrice = Replace(strFormulaCLPrice, "{材料成本}", Val(Range("L" & Target.Row).Value))
250          strFormulaCLPrice = Replace(strFormulaCLPrice, "{材料加价}", Val(Range("M" & Target.Row).Value))
260          intResultCLPrice = Application.Evaluate(strFormulaCLPrice)
270          Range("F" & Target.Row).Value = IIf(intResultCLPrice = 0, "", intResultCLPrice)280          strFormulaCLAmount = Replace(strFormulaCLAmount, "{材料数量}", Val(Range("D" & Target.Row).Value))
290          strFormulaCLAmount = Replace(strFormulaCLAmount, "{材料单价}", Val(Range("F" & Target.Row).Value))
300          intResultCLAmount = Application.Evaluate(strFormulaCLAmount)
310          Range("G" & Target.Row).Value = IIf(intResultCLAmount = 0, "", intResultCLAmount)320          strFormulaCLLR = Replace(strFormulaCLLR, "{材料数量}", Val(Range("D" & Target.Row).Value))
330          strFormulaCLLR = Replace(strFormulaCLLR, "{材料加价}", Val(Range("M" & Target.Row).Value))
340          intResultCLLR = Application.Evaluate(strFormulaCLLR)
350          Range("N" & Target.Row).Value = IIf(intResultCLLR = 0, "", intResultCLLR)360          Application.EnableEvents = True370    End If' If Intersect(Target, Range("O16:O2015")) Is Nothing = False Then'''       Application.EnableEvents = False''        Dim imgFile As String'        Dim pic As Picture'        Dim strFileAsFileNameFullPath As String'        Dim rng As Range'        Dim bl As Double''        Dim ws As Worksheet'        Set ws = ActiveSheet'''         imgFile = GetPicFileName(Target)''            If Trim(imgFile) <> "" Then''             '--方法一,链接图片---(这里需要保留方法一,来计算方法二填充图片的尺寸)----------'             Set pic = ws.Pictures.Insert(imgFile)''              ' opic_产品编码_01'              picName = GetPicName(Target, False, True)''              '--方法二,填充图片-------------'              Dim shMB As Shape'              Set shMB = Sh_picMB.Shapes("PIC_MB")''              Dim shNew As Shape'              shMB.Copy'              ActiveSheet.Paste'              Set shNew = Selection.ShapeRange(1)''              shNew.Fill.UserPicture (imgFile)''               Set rng = Target.Offset(0, -4)'              ' On Error Resume Next''               Call ShapePicDel(Target)''                 Dim intMin As Double''                 intMin = Application.WorksheetFunction.Min(rng.MergeArea.Height, rng.MergeArea.Width) - 3'                 'ws.Hyperlinks.Add pic.ShapeRange(1), imgFile, , "点击查看图片"'                 ws.Hyperlinks.Add shNew, imgFile, , "点击查看图片"''                 With shNew 'pic'                 .Height = pic.Height'                 .Width = pic.Width''                      .Name = picName'                      '.Placement = xlMoveAndSize '这个属性很关键'                     ' If .ShapeRange.Rotation = 0 Then'                      If .Rotation = 0 Then'                        If .Height >= .Width Then'                            .Height = intMin'                            bl = pic.Height / intMin'                            .Width = pic.Width / bl'                        Else'                            .Width = intMin'                            bl = pic.Width / intMin'                            .Height = pic.Height / bl'                        End If'                      End If'                      .top = rng.top + (rng.MergeArea.Height - .Height) / 2'                      .Left = rng.Left + (rng.MergeArea.Width - .Width) / 2'               End With''               pic.Delete''            End If''       Range(GetSetVal("ckBill_customerRng")).Select''       Application.EnableEvents = True' End If380    Exit Sub'----------------------------------
er:
390     MsgBox err.Description
400     Application.EnableEvents = TrueEnd SubSub ShapePicDel(rng As Range)Dim ws As WorksheetDim sh As ShapeSet ws = rng.WorksheetFor Each sh In ws.ShapesIf InStr(sh.Name, Replace(rng.Address, "$", "")) > 0 Thensh.DeleteEnd IfNext sh
End SubPrivate Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Intersect(Target, Range("G3")) Is Nothing = False ThenCancel = TrueDim strPwInput As StringDim strPw As StringstrPwInput = InputBox("请输入密码", "提示")If Trim(strPwInput) = "" ThenExit SubEnd IfstrPw = getws("数据字典").Range("C2").ValueIf Trim(strPw) = "" ThenExit SubEnd IfIf Trim(strPw) = Trim(strPwInput) ThenColumns("L:N").EntireColumn.Hidden = Not Columns("L:N").EntireColumn.HiddenEnd IfEnd IfIf Intersect(Target, Range(GetSetVal("ckBill_customerRng"))) Is Nothing = False ThenCancel = TrueSet frmSearchCustomer.rngResult = TargetfrmSearchCustomer.utype = "CK"frmSearchCustomer.DblClickClose = TruefrmSearchCustomer.Caption = "选择客户"frmSearchCustomer.Width = 500frmSearchCustomer.listInfo.Width = frmSearchCustomer.Width - 20Dim p As POINTAPIGetCursorPos pfrmSearchCustomer.StartUpPosition = 0 '手动frmSearchCustomer.Left = Target.Offset(0, -1).Width + p.X / 1.3333frmSearchCustomer.top = Target.Offset(0, -1).Height + p.Y / 1.3333frmSearchCustomer.Show 0frmSearchCustomer.initfrmSearchCustomer.Height = frmSearchCustomer.listInfo.Height + frmSearchCustomer.listInfo.top + 38End IfIf Intersect(Target, Range(GetSetVal("ckBill_selectGoodsRng"))) Is Nothing = False ThenCancel = TrueIf CheckLimited = False Then Exit SubckEnableEvents = TrueWith frmSelectInfoSet .rngResult = Target'Set frmSelectInfo.rngDetail = Range(strBillGoodsRng).BillType = "CK".DblClickClose = False.Caption = "选择产品信息".Width = GetSetVal("goods_formWidth").listInfo.Width = .Width - 20.init.Show 0End WithEnd IfIf Intersect(Target, Range(GetSetVal("ckBill_billDateRng"))) Is Nothing = False ThenCancel = TruefrmCalendar.Show vbModalIf IsNull(rtnDate) = False ThenRange(GetSetVal("ckBill_billDateRng")).Value = rtnDatestrTempVal = GetSetVal("ckBill_billNoRng")If Trim(strTempVal) <> "" ThenRange(strTempVal).Value = GetBillNo("BJ", Range(GetSetVal("ckBill_billDateRng")).Value, False) ' "自动生成"End IfEnd IfEnd IfIf Intersect(Target, Range("C5")) Is Nothing = False ThenCancel = TrueSet frmSelectPara.rngResult = TargetfrmSelectPara.BillType = ""frmSelectPara.DblClickClose = TruefrmSelectPara.Caption = "选择提交公司"frmSelectPara.Width = 280frmSelectPara.listInfo.Width = frmSelectPara.Width - 20frmSelectPara.Show 0frmSelectPara.init (GetSetVal("gsmc_list_dataRng"))End IfIf Intersect(Target, Range("A16:A2015")) Is Nothing = False ThenCancel = TrueSet frmSelectPara.rngResult = TargetfrmSelectPara.BillType = "bj"frmSelectPara.moveCol = 1frmSelectPara.DblClickClose = TruefrmSelectPara.Caption = "选择项目类别"frmSelectPara.Width = 280frmSelectPara.listInfo.Width = frmSelectPara.Width - 20frmSelectPara.Show 0frmSelectPara.init (GetSetVal("xmtype_list_dataRng"))End IfEnd SubPrivate Sub Worksheet_SelectionChange(ByVal Target As Range)Set frmSelectInfo.rngResult = Target
End Sub

这篇关于VBA 根据单元格改变的值改变对应单元格的值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1140457

相关文章

react笔记 8-18 事件 方法 定义方法 获取/改变数据 传值

1、定义方法并绑定 class News extends React.Component {constructor(props) {super(props)this.state = {msg:'home组件'}}run(){alert("我是一个run") //方法写在类中}render() {return (<div><h2>{this.state.msg}</h2><button onCli

如何在Excel中根据单元格内容作MSnbsp;…

上篇文章,我们介绍了INDEX+SMALL+IF+ROW的数组公式组合,也就是说只要在IF中通过条件的构造,基本上就可以想提取什么条件的数据都可以,数据查询肯定得心应手。 但是,我们一起强调函数公式不是万能的,尤其是数组公式在海量数据面前,既是软肋也是硬伤,而且构造这个函数组合还需要你要具备或者能理解简单数组公式逻辑,对于在函数公式方面没有深究的人,自然是一头雾水。当然,就像“数据透视表”一样,

jqgrid设置单元格可编辑

1 在单元格的属性列设置为editable。 2 点击编辑按钮的时候,触发某一行设置为edit的状态。 jQuery("#rowed4").jqGrid({url:'server.php?q=2',datatype: "json",colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],colModel

定位cpu占用过高的线程和对应的方法

如何定位cpu占用过高的线程和对应的方法? 主要是通过线程id找到对应的方法。 1 查询某个用户cpu占用最高的进程号 top -u 用户名 2 查询这个进程中占用cpu最高的线程号 top –p 进程号-H    3 查询到进程id后把进程相关的代码打印到jstack文件 jstack -l pid > jstack.txt 4 在jstack文件中通过16进制的线程id搜索到

Weibull概率分布纸(EXCEL VBA实现)

在学习Weibull分布理论的时候,希望有一张Weibull概率纸,用来学习图解法。但是在度娘上没有找到的Weibull概率纸的电子版。在书上看到的Weibull概率纸,只能复印下来使用。于是萌生了自己制作Weibull概率纸的想法,帮助自己更好地学习。 本人擅长使用各种计算机语言,C,C++,Matlab,Scilab等等,但是始终钟爱与VBA,认为VBA可以实现一切你想要的东西,由于在企业里不

一台电脑对应一个IP地址吗?‌探讨两台电脑共用IP的可能性

在当今数字化时代,‌IP地址作为网络世界中的“门牌号”,‌扮演着至关重要的角色。‌它负责在网络上唯一标识每一台设备,‌使得数据能够在庞大的互联网中准确无误地传输。‌然而,‌对于IP地址与电脑之间的对应关系,‌许多人可能存有疑惑:‌一台电脑是否必须对应一个IP地址?‌两台电脑又是否可以共用一个IP地址呢?‌本文将深入探讨这些问题,‌带您一窥IP地址背后的奥秘。‌ 一台电脑对应一个IP地址吗?‌

【前端】animation动画以及利用vue制作简单的透明度改变动画,包含vue生命周期实现

一. 问题描述 想做一个文字透明度从1到0然后再从0到1的css动画。 二. 代码写法 2.1 animation写法 2.1.1 animation属性key 2.1.2 代码展示 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=de

【Markdown】如何在Markdown中合并单元格

Markdown语法本身不包含复杂表格的插入,但是可以使用html语法来实现。 水平单元格的合并:基于colspan属性,即使一个单元格占多列的空间纵向单元格的合并:基于rowspan属性,即使一个单元格占多行的空间 要想MarkDown中插入复杂表格时,可以先在word或excel中把表格写好,然后在如下网站进行转化为标记对形式: http://pressbin.com/tools/exc

git如何灵活切换本地账号对应远程github的两个账号

git如何灵活切换本地账号对应远程github的两个账号 问题: 有时候我们会同时维护两个github的账号里面的仓库内容,这时候本地git需要频繁的切换ssh,以方便灵活的与两个账号的仓库可以通信。这篇日记将阐述我是怎么解决这个问题的。1. 第一个账户 生成本地SSH2. 注意 我们要设置第二个账户的 本地 SSH 时3. 两个账号来回切换 问题: 有时候我们会同时维护两个git