资源规划器针对基于项目的工作日期搜索

huangapple go评论52阅读模式
英文:

Ressource planner for project based working date search

问题

我正在努力将所有工作表中相同单元格的值相加,并将结果放在第一个工作表的相应单元格中。出于某种原因,所有的求和结果都是0。通过逐步尝试,我发现了问题出在这一行上:

Set cell = ws.Range("B5 : BZ5").Find(rng.Value)

这在以前对我有效,我不明白为什么它在这里不起作用。

如果将这行代码改成:

Set cell = ws.Range("B5")

那么它可以工作。

英文:

I am working on adding up all values from the same cells in all worksheets in the corosponding cell on the first worksheet. For some reason all sums are 0. By trying it step by step i found the problem in this line:

Set cell = ws.Range("B5 : BZ5").Find(rng.Value)

This worked for me before and I dont understand why it should not work here.

Sub LoopTest()

Dim ws As Worksheet
Dim sum As Double
Dim cell As Range
Dim rng As Range
Dim i As Long
Dim n As Long


i = 2 ' start in Spalte B

    Do While Len(Cells(5, i).Value) > 0
    n = 7 ' start in Reihe 7
    Do While Len(Cells(n, 1).Value) > 0
        Set rng = Worksheets("Ressourcen").Cells(5, i)
        sum = 0
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name <> "Ressourcen" Then
                Set cell = ws.Range("B5:BZ5").Find(rng.Value)
                If Not cell Is Nothing Then
                    sum = sum + cell.Offset(n - 5, 0).Value
                End If
            End If
        Next ws

        Worksheets("Ressourcen").Cells(n, i).Value = sum
        n = n + 1
        Loop
        i = i + 1
    Loop
End Sub

if this were to be:

Set cell = ws.Range("B5")

it works

答案1

得分: 0

Range.Find

Range.Find 可以被VBA命令调用,或者在Excel中被用户使用。

当你没有指定所有Find所需的参数,比如LookIn设置和/或LookAt设置,它会采用上次使用的设置。这通常会导致与预期不同的结果,甚至可能导致没有匹配,从而引发错误。

如果你的VBA停止像之前的测试一样工作,很可能是其中一个设置已被用户/你手动更改。

因此,在编写VBA脚本时,强烈建议至少包括 LookInLookAt 参数,以确保始终使用你打算的参数。

例如:

Set cell = ws.Range("B5:BZ5").Find(What:=Rng.Value, LookIn:=xlValues, LookAt:=xlWhole)

其他参数也会影响搜索,但经常会引起问题,比如 MatchCase(尽管在你的情况下,搜索日期时不会有问题)。完整的参数集包括:

Range.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
英文:

Range.Find

Range.Find can be called by VBA commands, or by the user in Excel.

When you do not specify all the parameters required for a Find, such as the LookIn setting and/or the LookAt setting, it will employ the last settings used. This often results in a different result than expected and can result in no matches at all which can cause errors.

If your VBA stops behaving as it did in previous testing, it's likely that one of these settings has been manually changed by the user/yourself.

Therefore it is highly recommended to include at the very least the LookIn and LookAt parameters when forming your VBA script to ensure the parameters you intended are always used.

eg:

Set cell = ws.Range("B5 : BZ5").Find(What:=Rng.Value, LookIn:=xlValues , LookAt:=xlWhole)

Other parameters can effect the search but can cause issues as often - such as MatchCase (though not in your case, when searching for dates). The full set of parameters are:

Range.Find( What , After , LookIn , LookAt , SearchOrder, SearchDirection , MatchCase _
, MatchByte , SearchFormat )

huangapple
  • 本文由 发表于 2023年2月8日 22:35:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定