简体   繁体   English

我在VBA代码中的操作顺序有什么问题?

[英]What is wrong with my order of operations in VBA code?

I have this Excel spreadsheet that I am trying to create a working macro (in VBA) that when a cell containing a date (dates in order by a row in the B column), and this cell is in a specific color, and this cell is active, and the user clicks on a button, the macro searches for all the dates equal to the date in the active cell and to its color. 我有这个Excel电子表格,我试图创建一个工作宏(在VBA中),当一个包含日期的单元格(按B列中的一行按日期排序)且该单元格具有特定的颜色时,该单元格如果处于活动状态,并且用户单击按钮,则宏会搜索所有与活动单元格中的日期及其颜色相等的日期。 Then in column H, the number value of the respective rows to the found dates are added up and stored into a variable called totalValue Then afterwards, the date, description, and the totalValue are copied over to another sheet and pasted in the next available predefined row. 然后在H列中,将找到日期的各行的数值相加并存储到名为totalValue的变量中,然后将日期,描述和totalValue复制到另一张纸上,并粘贴到下一个可用的预定义中行。

I know that the color sort works for one color, I am using more than one color layout. 我知道颜色排序适用于一种颜色,我使用了不止一种颜色布局。 The problem is when I run the macro, it seems to add all the number values in Column H within the date and it does not filter out the colors. 问题是当我运行宏时,似乎在日期之内在H列中添加了所有数字值,并且没有滤除颜色。 But, when I take out block of code for "if color equals this, then do math" in lines 52 & 53 ( ElseIf rFound.Style.Name = "Shipping" Then totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet" ) then the color value for the code above that in lines 49 & 50 works ( ElseIf rFound.Style.Name = "Office" Then totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet" ), but not the code above that lines 46 & 47 unless I take out the code in lines 49 & 50 as well, otherwise it would still add all the values in Column E. 但是,当我在第52和53行中取出“如果颜色等于此,然后进行数学运算”的代码块时( ElseIf rFound.Style.Name = "Shipping" Then totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet"进入ElseIf rFound.Style.Name = "Shipping" Then totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet" ),然后上面49和50行中的代码的颜色值起作用( ElseIf rFound.Style.Name = "Office" Then totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet"将到达ElseIf rFound.Style.Name = "Office" Then totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet" ),但第46和47行上方的代码除外,除非我也将第49&50行中的代码取出,否则它仍将添加所有值E栏

What am I doing wrong? 我究竟做错了什么? How can I fix it so that it can find the dates in a set color and be able to have several set colors available for use without this addition problem? 我该如何解决它,以便它可以以一种设置的颜色查找日期,并且能够使用几种设置的颜色而不会出现这种添加问题?

The code in question starts at 'BEGINNING OF HELP SEGMENT and ends at 'END OF HELP SEGMENT . 有问题的代码从'BEGINNING OF HELP SEGMENT开始'BEGINNING OF HELP SEGMENT'BEGINNING OF HELP SEGMENT结束'END OF HELP SEGMENT The code above, between 'BEGINNING of Search function for HELP SEGMENT and 'ENG of Search function for HELP SEGMENT is the gathering of search parameters. 上面的代码在'BEGINNING of Search function for HELP SEGMENT'ENG of Search function for HELP SEGMENT 'BEGINNING of Search function for HELP SEGMENT 'ENG of Search function for HELP SEGMENT是收集搜索参数的代码。

Here is my code: 这是我的代码:

Sub Copy_and_Move_Jul()
'
' Copy_and_Move From July Payable Ledger to Jul Summary Macro
'

'BEGINNING of Search function for HELP SEGMENT
'********************************************
    'Declare Var

    Const AllUsedCellsColumnB = False
    Dim rFound As Range, SearchRange As Range
    Dim cellValue As Variant, totalValue As Variant

    ' Get the H value of active row and set it to totalValue
    cellValue = Range("H" & ActiveCell.Row)
    totalValue = cellValue

    ' GET & SEARCH FOR COLOR AND DATE OF ACTIVE CELL, AND GET THE VALUES IN COLUMN H AND RETURN VALUE TO "totalValue"

    ' set search range
    Set SearchRange = Range("B7:B56")

    ' If there is no search range, show Msg
    If Intersect(SearchRange, ActiveCell) Is Nothing Then
        SearchRange.Select
        MsgBox "You must select a cell in the date column before continuing", vbInformation, "Action Cancelled"
        Exit Sub
    End If

    ' Get search criteria & set it to rFound
    Set rFound = SearchRange.Find(What:=ActiveCell.Value, _
                                  After:=ActiveCell, _
                                  LookIn:=xlValues, _
                                  LookAt:=xlPart, _
                                  SearchOrder:=xlByRows, _
                                  SearchDirection:=xlNext, _
                                  SearchFormat:=False)


'********************************************
ENG of Search function for HELP SEGMENT


' BEGINNING OF HELP SEGMENT
'********************************************************************************************************************

    ' If rFound is not Nothing, then do math. If rFound is Nothing, then findnext
    If Not rFound Is Nothing Then

        Do

            If rFound.Style.Name = "Marketing" Then
                totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet"

            ElseIf rFound.Style.Name = "Inventory" Then
                totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet"

            ElseIf rFound.Style.Name = "Office" Then
                totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet"

            ElseIf rFound.Style.Name = "Shipping" Then
                totalValue = totalValue + rFound.Offset(0, 6).Value ' THIS VALUE GOES TO Column E "Summary Sheet"

            End If

            Set rFound = SearchRange.FindNext(rFound)

        ' Loop till all matching cells are found
        Loop While Not rFound Is Nothing And rFound.Address <> ActiveCell.Address
    End If ' End of the Color & Date search
'********************************************************************************************************************    
' END OF HELP SEGMENT    

    'Select & copy Columns B - I of Row of Active Cell

    Range("B" & ActiveCell.Row & ":G" & ActiveCell.Row).Select
    Selection.Copy

    'Go to "Summary" Sheet & Paste data in next available empty Row

    Sheets("Summary").Select
    Range("B56").End(xlUp).Offset(1, 0).Select
    ActiveSheet.Paste

    'Select Column D & delete unneeded Qty # and input a "y" for "Expsense"
    Range("D" & ActiveCell.Row).Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "y"

    'Set Value of Column H

    Range("E" & ActiveCell.Row) = totalValue


    'Goto Column C, Check Cell Style and input where supplies came from

    Range("C" & ActiveCell.Row).Select

    If Worksheets("Summary").Range("C" & ActiveCell.Row).Style.Name = "Marketing" Then
        ActiveCell.FormulaR1C1 = "Marketing Supplies"

    ElseIf Worksheets("Summary").Range("C" & ActiveCell.Row).Style.Name = "Inventory" Then
        ActiveCell.FormulaR1C1 = "Inventory Supplies"

    ElseIf Worksheets("Summary").Range("C" & ActiveCell.Row).Style.Name = "Office" Then
        ActiveCell.FormulaR1C1 = "Office Supplies"

    ElseIf Worksheets("Summary").Range("C" & ActiveCell.Row).Style.Name = "Shipping" Then
        ActiveCell.FormulaR1C1 = "Shipping Supplies"

    End If

End Sub

Here is a picture, before taking out the code in lines 52 & 53, I hope this helps with my explanation as to what is happening: 这是一张图片,在删除第52和53行中的代码之前,我希望这有助于我对发生的事情进行解释:

当前代码未更改

Here is a picture, after taking out the code in lines 52 & 53, this is what it's supposed to do: 这是一张图片,取出第52和53行中的代码后,这是应该做的:

第52和53行的结果从代码中删除

Much appreciation in advance! 提前多谢!

Start by checking if all the style names in the search range have the expected values: 首先检查搜索范围内的所有样式名称是否均具有期望值:

Sub styleNames()

    Dim cl As Range, SearchRange As Range

    Set SearchRange = Range("B7:B56")

    For Each cl In SearchRange
        If cl.Value <> vbNullString Then _
            Debug.Print " row: " & cl.Row & " style name: " & cl.Style.name
    Next cl

End Sub

If they do, then you know for sure it's your code which is the problem. 如果他们这样做,那么您可以确定问题出在您的代码上。 Try rewriting it in a simpler and less convoluted way by introducing conditional statements in the for each loop instead. 通过在for each循环中引入条件语句,尝试以一种更简单,更省力的方式重写它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM