结果未计入 excel vba 电子表格

如何解决结果未计入 excel vba 电子表格

(已编辑,问题已部分解决,请参阅评论)我是 VBA 新手,所以这可能很乏味,但是:我编写的代码存在一些问题,试图为发票添加折扣。我有它,如果在“组织列表”中的一个折扣列下的某一行中写入 1,则该折扣号将反映在“QuickCalcs”的成本列中,用于与具有折扣的组织相关联的项目。我已经对其进行了测试,以确保 if 语句正常工作,并且确实如此。计算也是正确的,但由于某些原因,这些值并没有取代通过乘以数量和费率计算出的原始成本。似乎 amount.value = amount.value - dis1 行和其他类似的行不起作用,或者我可能误解了它。当我测试时,我在另一个空单元格中出现了 amount.value 并显示了正确的值,但是由于某种原因将其输入到具有预先存在的数量的单元格中时它似乎不起作用,尽管这可能不是问题。你会建议我如何解决这个问题,以便正确的折扣金额反映在成本列(QuickCalcs,col L)中?我的代码如下。谢谢!!

Sub Discounts()
    Dim orgnameB    As Range
    Dim orgListB    As Range
    Set orgListB = Worksheets("Org List").Range("B3:B23")
    Dim siteTC      As Range
    Dim TCsites     As Range
    Set TCsites = Worksheets("TC Site Report").Range("B2:B23")
    Dim itemDescr   As Range
    Dim quickcalcsI As Range
    Set quickcalcsI = Worksheets("QuickCalcs").Range("I2:I23")
    
    ' Amount Calculation
    Dim amount      As Range
    Dim rateval     As Range
    Dim qtyval      As Range
    Dim total       As Double
    Dim dis1        As Integer
    Dim dis2        As Integer
    Dim dis3        As Integer
    Dim dis4        As Integer
    Dim dis5        As Integer
    Dim dis6        As Integer
    
    For Each orgnameB In orgListB
        If IsEmpty(orgnameB) = TRUE Then
            Exit For
        End If
        For Each siteTC In TCsites
            For Each itemDescr In quickcalcsI
                Set amount =     Worksheets("QuickCalcs").Cells(itemDescr.row,"L")
                Set rateval = Worksheets("QuickCalcs").Cells(itemDescr.row,"K")
                Set qtyval = Worksheets("QuickCalcs").Cells(itemDescr.row,"J")
                amount.Value = rateval.Value * qtyval.Value
                total = amount.Value
                
                If Worksheets("Org List").Cells(orgnameB.row,"I").Value = 1 Or Worksheets("Org List").Cells(orgnameB.row,"K").Value = 1 Or Worksheets("Org List").Cells(orgnameB.row,"J").Value = 1 Then
                    ' If discount : if orgnameB.row,colI,J,or K = 1,apply discount
                    
                    If itemDescr.Value = siteTC.Value Then
                        
                        'If current item in quickcalcs = item in TC Site Report
                        If Worksheets("TC Site Report").Cells(siteTC.row,"D") = orgnameB.Value And Worksheets("QuickCalcs").Cells(itemDescr.row,"H") <> "Annual Fee" Then
                            'If current item in quickcalcs is associated with that org
                            If Worksheets("Org List").Cells(orgnameB.row,"I") = 1 Then
                                percentoff1 = Worksheets("Org List").Cells(2,"I").Value / 100
                                dis1 = total * (percentoff1)
                                amount.Value = amount.Value - dis1
                            End If
                            If Worksheets("Org List").Cells(orgnameB.row,"J") = 1 Then
                                percentoff2 = Worksheets("Org List").Cells(2,"I").Value / 100
                                dis2 = total * (percentoff2)
                                amount.Value = amount.Value - dis2
                            End If
                            If Worksheets("Org List").Cells(orgnameB.row,"K") = 1 Then
                                percentoff3 = Worksheets("Org List").Cells(2,"I").Value / 100
                                dis3 = total * (percentoff3)
                                amount.Value = amount.Value - dis3
                            End If
                        End If
                    End If
                End If
            Next
        Next
    Next
    
End Sub

Ranges Worked with,QuickCalcs Tab

Ranges Worked with,TC Site Report Tab

Ranges Worked with,Org List

解决方法

尝试重新安排循环的嵌套,以便只扫描一次 Worksheets("QuickCalcs")percentoff 是否总是 wsOrg.Cells(2,"I").Value 即使例如 wsOrg.Cells(r2,"J") = 1

Option Explicit

Sub Discounts()

    Dim wsOrg As Worksheet,orgnameB As Range,orgListB As Range
    Set wsOrg = Worksheets("Org List")
    Set orgListB = wsOrg.Range("B3:B23")

    Dim wsSite As Worksheet,siteTC As Range,TCsites As Range
    Set wsSite = Worksheets("TC Site Report")
    Set TCsites = wsSite.Range("B2:B23")

    Dim wsCalc As Worksheet,itemDescr As Range,quickcalcsI As Range
    Set wsCalc = Worksheets("QuickCalcs")
    Set quickcalcsI = wsCalc.Range("I2:I23")

    ' Amount Calculation
    Dim amount As Range,rateval As Range,qtyval As Range
    Dim total,percentoff,dis
    Dim r1 As Long,r2 As Long,r3 As Long

    For Each itemDescr In quickcalcsI
        r1 = itemDescr.Row

        With Worksheets("QuickCalcs")
            Set amount = .Cells(r1,"L")
            Set rateval = .Cells(r1,"K")
            Set qtyval = .Cells(r1,"J")
            amount.Value = rateval.Value * qtyval.Value
            total = amount.Value
        End With

        For Each orgnameB In orgListB
            If IsEmpty(orgnameB) = True Then
                Exit For
            End If
            r2 = orgnameB.Row

            ' If discount : if orgnameB.row,colI,J,or K = 1,apply discount
            If wsOrg.Cells(r2,"I").Value = 1 _
                Or wsOrg.Cells(r2,"K").Value = 1 _
                Or wsOrg.Cells(r2,"J").Value = 1 Then

                For Each siteTC In TCsites
                     
                    If itemDescr.Value = siteTC.Value Then
                        r3 = siteTC.Row
                            'If current item in quickcalcs = item in TC Site Report
                        If wsSite.Cells(r3,"D") = orgnameB.Value _
                           And wsCalc.Cells(r1,"H") <> "Annual Fee" Then

                            'If current item in quickcalcs is associated with that org
                            If wsOrg.Cells(r2,"I") = 1 Then
                                percentoff = wsOrg.Cells(2,"I").Value / 100
                                dis = total * percentoff
                                amount.Value = amount.Value - dis
                            End If
                            If wsOrg.Cells(r2,"J") = 1 Then
                                percentoff = wsOrg.Cells(2,"K") = 1 Then
                                percentoff = wsOrg.Cells(2,"I").Value / 100
                                dis = total * percentoff
                                amount.Value = amount.Value - dis
                            End If
                        End If
                    End If

                Next
            End If
        Next
    Next
    MsgBox "Done"
End Sub
,

使用三重嵌套循环并不是真正的可扩展解决方案,并且很难通过单步调试 (F8)。更好的方法是使用 Find 直接定位其他工作表上的匹配行。例如

Sub Discount2()

    Dim iLastRow As Long
    Dim wsOrg As Worksheet,orgListB As Range
    Set wsOrg = Worksheets("Org List")
    iLastRow = wsOrg.Cells(Rows.Count,"B").End(xlUp).Row
    Set orgListB = wsOrg.Range("B3:B" & iLastRow)

    Dim wsSite As Worksheet,TCsites As Range
    Set wsSite = Worksheets("TC Site Report")
    iLastRow = wsSite.Cells(Rows.Count,"B").End(xlUp).Row
    Set TCsites = wsSite.Range("B2:B" & iLastRow)

    Dim wsCalc As Worksheet,quickcalcsI As Range
    Set wsCalc = Worksheets("QuickCalcs")
    iLastRow = wsCalc.Cells(Rows.Count,"I").End(xlUp).Row
    Set quickcalcsI = wsCalc.Range("I2:I" & iLastRow)

    Dim amount As Range,qtyval As Range
    Dim rng As Range,org As String,dis As Single
    Dim r As Long,n As Long

    ' scan down sheet
    For Each itemDescr In quickcalcsI

        Set amount = Worksheets("QuickCalcs").Cells(itemDescr.Row,"L")
        Set rateval = Worksheets("QuickCalcs").Cells(itemDescr.Row,"K")
        Set qtyval = Worksheets("QuickCalcs").Cells(itemDescr.Row,"J")
        amount.Value = rateval.Value * qtyval.Value
        dis = 0

        ' find org for site
        Set rng = TCsites.Find(itemDescr,LookIn:=xlValues,LookAt:=xlWhole)
        If rng Is Nothing Then
            MsgBox "Site '" & itemDescr & "' not found",vbExclamation,"Row " & itemDescr.Row
        Else
            org = rng.Offset(,2) ' col d
            ' Find discount for org
            Set rng = orgListB.Find(org,LookAt:=xlWhole)
            If rng Is Nothing Then
                MsgBox "Org '" & org & "' not found","Row " & itemDescr.Row
            Else
                r = rng.Row
                If wsOrg.Cells(r,"I") = 1 Then
                    dis = wsOrg.Cells(2,"I")
                ElseIf wsOrg.Cells(r,"J") = 1 Then
                    dis = wsOrg.Cells(2,"J")
                ElseIf wsOrg.Cells(r,"K") = 1 Then
                    dis = wsOrg.Cells(2,"K")
                End If
            End If
        End If

        ' apply discount
        If dis > 0 Then
            amount.Value = amount.Value * (100 - dis) / 100
            'amount.Interior.Color = vbYellow ' use to debug
            n = n + 1
        Else
            'amount.Interior.Color = vbWhite ' use to debug
        End If
    Next
    MsgBox n & " rows  discounted",vbInformation
   
End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res