[英]VBA: How to return a cell reference from a variable that looks up a value?
Good day I have a table with data, one of the columns have a number (There are repeating numbers) and the other column has letters and the last column has the feedback. 早上好,我有一个带有数据的表,其中一列有一个数字(有重复的数字),另一列有字母,最后一列有反馈。
My entries change every day and I want to have one space where I can put today's Number, letter and feedback and then create a button that looks for the letter and number in the table and posts the feedback 我的输入每天都在变化,我想在一个空格中放置今天的数字,字母和反馈,然后创建一个按钮来查找表格中的字母和数字并发布反馈
Data Table: 数据表:
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
There is a similar problem posted on stack overflow and I tried to use a similar method, but this only works for searching against one criteria: 在堆栈溢出上也发布了类似的问题,我尝试使用类似的方法,但这仅适用于根据一个条件进行搜索:
I have the following: 我有以下几点:
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
Please assist 请协助
You can use AutoFilter
with 2 criterias to achieve this. 您可以将AutoFilter
与2个条件结合使用来实现此目的。
Code 码
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
Another approach: 另一种方法:
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
Thank you for all the comments and answers. 感谢您的所有评论和答案。 Helped a lot 很有帮助
Here is what worked in the end: 最终的工作如下:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.