美好的一天 我有一个包含数据的表,其中一列有一个数字(有重复的数字),另一列有字母,最后一列有反馈。
我的参赛作品每天都在变化,我希望有一个空间可以放置今天的数字,字母和反馈,然后创建一个按钮,查找表格中的字母和数字并发布反馈
数据表:
Number Letter Feedback Todays Number Todays letter Todays Feedback
1 A 3 B 100
1 B
2 A
2 B
3 A
3 B
4 A
4 B
5 A
5 B
在堆栈溢出时发布了类似的问题,我尝试使用类似的方法,但这仅适用于针对一个条件进行搜索:
我有以下内容:
Private Sub CommandButton1_Click()
Dim MatchFormula As Long
MatchFormula = WorksheetFunction.Match(Range("Number"), Range("A:A"), 0)
Range("c" & MatchFormula).Value = Range("f2").Value
End Sub
请协助
答案 0 :(得分:1)
您可以使用AutoFilter
和2个标准来实现此目的。
<强> 代码 强>
Option Explicit
Sub AutoFilt()
Dim Sht As Worksheet
Dim Rng As Range, VisRng As Range, FiltRngArea As Range
Dim LastRow As Long
Set Sht = ThisWorkbook.Sheets("Sheet1") ' modife "Sheet1" to your sheet's name
With Sht
.Range("A1:C1").AutoFilter
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' get last row with data in column "A"
With .Range("A1:C" & LastRow)
.AutoFilter field:=1, Criteria1:=.Range("E2").Value2
.AutoFilter field:=2, Criteria1:=.Range("F2").Value2
' set the visible rows range after Auto-Filter was applied
Set VisRng = .SpecialCells(xlCellTypeVisible)
' loop through areas of Filterred Range
For Each FiltRngArea In VisRng.Areas
If FiltRngArea.Row > 1 Then ' not header row
FiltRngArea.Cells(1, 3).Value = .Range("G2").Value ' set the value
End If
Next FiltRngArea
End With
End With
End Sub
答案 1 :(得分:0)
另一种方法:
Option Explicit
Private Sub CommandButton1_Click()
Dim rngNumbers As Range
Dim varCounter As Variant
Dim intTodaysNumber As Integer
Dim strTodaysLetter As String
'Determine numbers range and filter arguments
With ThisWorkbook.Worksheets("Tabelle1")
Set rngNumbers = .Range("A2", .Cells(Rows.Count, 1).End(xlUp))
intTodaysNumber = .Range("F2").Value
strTodaysLetter = .Range("G2").Value
'Loop through numbers range and compare number and letter
For Each varCounter In rngNumbers
If varCounter.Value = intTodaysNumber And _
varCounter.Offset(0, 1).Value = strTodaysLetter Then
'Write found feedback value
.Range("H2") = varCounter.Offset(0, 2).Value
debug.print "Entry found for number " & intTodaysNumber & _
" and letter " & strTodaysLetter & _
" at row " & varCounter.Row & " and column " & _
varCounter.Column
Exit For
End If
Next varCounter
End With
End Sub
答案 2 :(得分:0)
感谢您的所有意见和解答。帮了很多
以下是最终有效的方法:
Private Sub CommandButton2_Click()
Dim MatchNumber As Long 'The First Row number with the matching Number'
Dim LastRow As Long 'The Last Row number with matching Number'
Dim MatchLetter As Long 'The First Row Number with Matching number and matching letter'
Dim Post As Long 'The Row Number where the feedback need sto be posted'
'Find the Row in Column A:A the matches the value in the Number range
MatchNumber = WorksheetFunction.Match(Range("Number"), Range("a:a"), 0)
'Find the Last row that mathces the number (+1 because there is only 2 entries for each number, if there was 4 entries per number then +3)
LastRow = MatchNumber + 1
'Find the Matching Letter in the new range
MatchLetter = WorksheetFunction.Match(Range("Letter"), Range(Cells(MatchNumber, 2), Cells(LastRow, 2)), 0)
'The Row number where the feedback need sto be posted
Post = MatchLetter + MatchNumber - 1
'Post the value captured in h2 to column c in the 'post' row
Range("c" & Post).Value = Range("h2").Value
End Sub
&#13;