ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1100|回复: 11

一个算价格的程序 求教

[复制链接]

TA的精华主题

TA的得分主题

发表于 2021-8-8 15:56 | 显示全部楼层 |阅读模式
想要根据规格尺寸的输入范围,自动输入单价,并算出小计和总价

测试.rar

6.09 KB, 下载次数: 28

TA的精华主题

TA的得分主题

发表于 2021-8-8 16:40 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
是不是这样的。

测试.rar

7.1 KB, 下载次数: 17

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-8-8 16:43 | 显示全部楼层
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row > 1 And Target.Column = 1 Then
   If Cells(Target.Row, 1) <= 100 And Cells(Target.Row, 1) > 0 Then
      Cells(Target.Row, 2) = 20
   ElseIf Cells(Target.Row, 1) <= 200 And Cells(Target.Row, 1) > 100 Then
      Cells(Target.Row, 2) = 25
   End If
End If
If Target.Row > 1 And Target.Column = 3 Then
   If Cells(Target.Row, 3) = "" Then
      Cells(Target.Row, 4) = ""
   ElseIf Cells(Target.Row, 3) <> "" Then
      Cells(Target.Row, 4) = Cells(Target.Row, 2) * Cells(Target.Row, 3)
   End If
End If
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-8-8 16:44 | 显示全部楼层
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row > 1 And Target.Column = 1 Then
   If Cells(Target.Row, 1) <= 100 And Cells(Target.Row, 1) > 0 Then
      Cells(Target.Row, 2) = 20
   ElseIf Cells(Target.Row, 1) <= 200 And Cells(Target.Row, 1) > 100 Then
      Cells(Target.Row, 2) = 25
   End If
End If
If Target.Row > 1 And Target.Column = 3 Then
   If Cells(Target.Row, 3) = "" Then
      Cells(Target.Row, 4) = ""
   ElseIf Cells(Target.Row, 3) <> "" Then
      Cells(Target.Row, 4) = Cells(Target.Row, 2) * Cells(Target.Row, 3)
   End If
End If
End Sub

前面几步我自己试着写了一下 测试没有问题 大神看看这个代码有没有问题

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-8-8 16:47 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

你好非常感谢 实现的效果就是这样 不过我想通过VBA的编程实现Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row > 1 And Target.Column = 1 Then
   If Cells(Target.Row, 1) <= 100 And Cells(Target.Row, 1) > 0 Then
      Cells(Target.Row, 2) = 20
   ElseIf Cells(Target.Row, 1) <= 200 And Cells(Target.Row, 1) > 100 Then
      Cells(Target.Row, 2) = 25
   End If
End If
If Target.Row > 1 And Target.Column = 3 Then
   If Cells(Target.Row, 3) = "" Then
      Cells(Target.Row, 4) = ""
   ElseIf Cells(Target.Row, 3) <> "" Then
      Cells(Target.Row, 4) = Cells(Target.Row, 2) * Cells(Target.Row, 3)
   End If
End If
End Sub

大神 帮我看一下这个代码逻辑或者代码上有没有优化的地方

TA的精华主题

TA的得分主题

发表于 2021-8-8 21:03 | 显示全部楼层
vba小白菜阿 发表于 2021-8-8 16:44
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row > 1 And Target.Column = 1 Then
...

Private Sub Worksheet_Change(ByVal Target As Range)
    Set xTarget = Application.Intersect(Target, [A3:C8])
    If xTarget Is Nothing Then Exit Sub
   
    For Each xT In xTarget
        Application.EnableEvents = False  '停止觸發事件
        r = xT.Row: c = xT.Column
        If c = 1 And Cells(r, 1) > 0 Then
            Cells(r, 2) = Application.Lookup(xT, [{0,"";50,20;101,25;201,28;301,""}])
        ElseIf c = 1 Then
            Cells(r, 2) = ""
        End If
        If Cells(r, 2) > 0 And Cells(r, 3) > 0 Then
            Cells(r, 4) = Cells(r, 2) * Cells(r, 3)
         Else
            Cells(r, 4) = ""
        End If
        [d9] = Application.Sum([d3:d8])
        Application.EnableEvents = True   '啟動觸發事件
    Next
End Sub

Sub 啟動觸發事件()
    Application.EnableEvents = True   '啟動觸發事件
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2021-8-8 21:23 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-8-9 10:54 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
AmoKat 发表于 2021-8-8 21:03
Private Sub Worksheet_Change(ByVal Target As Range)
    Set xTarget = Application.Intersect(Targe ...

大神  首先感谢您的帮助麻烦您再帮我看一下 我把代码放进去,运行不出来结果 到底是什么原因

灯光采购 对应表测试.rar

24.33 KB, 下载次数: 13

ceshi

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-8-9 11:01 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-8-9 11:32 | 显示全部楼层
image.png

大神们帮我看一下这些行代码怎么优化,我测试的是没有问题,每次我试着优化都会提示错误
自学真的是有点吃力了
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-23 22:29 , Processed in 0.056735 second(s), 16 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表