-2

如果有人可以帮助我解决这个问题,我将非常感激......

我想要一个 excel 宏,如果在 sheet1 的单元格 b3 中满足展位条件,它将通过 sheet2 的第一行和第一列返回值。条件将在 sheet1 上指定;单元格 b1 将包含应搜索 sheet2 中的行的条件,单元格 b2 将包含应搜索 sheet2 中的列的条件。结果应复制到 sheet1 的单元格 b3 中。

提前致谢

添加..............

我有这个子程序,它遍历行并查找条件 1(strDate),但我只能设法做到这一点,列是固定的。我应该再添加一个计数器,它会通过列以满足条件 2(strProduct),但我没有

   Sub LookUpValuesRCC2()
'
Dim shtData     As Worksheet    ' Sheet containing data 
Dim shtOutput   As Worksheet    ' Output Sheet 
Dim strDate     As String       ' Date - condition 1
Dim strProduct  As String       ' Product - condition 2   
Dim i           As Integer      ' Counter in shtData Sheet
Dim j           As Integer      ' Counter in shtOutput Sheet
'
Set shtData = Worksheets("sheet2")
Set shtOutput = Worksheets("sheet1")
'
' Loop through "Data" Sheet Rows
For i = 1 To 1000

    strDate = shtData.Cells(i, 1)
    '
    ' Loop through dates in "Output" Sheet
    ' if date match then vrite values
    For j = 1 To shtOutput.Cells(Rows.Count, 14).End(xlUp).Row
        If shtOutput.Cells(j, 14) = strDate Then
            shtOutput.Cells(j, 2) = shtData.Cells(i, 18)

        End If
    Next j
Next i

End Sub
4

1 回答 1

0

首先欢迎来到SO。其次,不是 100% 清楚你的目标是什么,因为你的代码与你想要的描述不完全匹配,或者在我看来没有这样做。

我已经根据您的描述编写了以下代码,因为您拥有的代码没有得到您想要的,所以我假设它无论如何都需要修改。

如果这不满足您的要求,请发表评论。

Sub LookUpValuesRCC2()

Dim shtData     As Worksheet    ' Sheet containing data
Dim shtOutput   As Worksheet    ' Output Sheet
Dim strDate     As Date       ' Date - condition 1
Dim strProduct  As String       ' Product - condition 2
Dim strResult As String         'result to print

Dim rngFound As range, rngFoundAgain As range

Set shtData = Worksheets("sheet2")
Set shtOutput = Worksheets("sheet1")

strDate = shtOutput.range("B1").Value
strProduct = shtOutput.range("B2").Value

strResult = "Nothing Found"

With shData

    'first look down the first column for the date
    Set rngFound = .Columns(1).Find(strDate, lookat:=xlWhole)

    If Not rngFound Is Nothing Then

        'if that is found, look for the product in the row with the date
        Set rngFoundAgain = rngFound.EntireRow.Find(strProduct, lookat:=xlWhole)

        If Not rngFoundAgain Is Nothing Then strResult = rngFoundAgain.Value

    End If

End With

shtData.range("B3") = strResult


End Sub
于 2012-12-11T16:08:52.940 回答