2017-12-19 24 views
0

こんにちは 私はデータを持つテーブルを持っています。列の1つに数字(繰り返し番号があります)があり、他の列には文字があり、最後の列にはフィードバックがあります。VBA:値を参照する変数からセル参照を返す方法

私のエントリは毎日変更し、私は私が

データを、今日の数、手紙やフィードバックを配置し、テーブル内の文字と数字を探しボタンを作成し、フィードバックを投稿することができます1つのスペースを持つようにしたいです表:

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 

あり、スタックオーバーフローに掲載同様の問題があると私は同様の方法を使用しようとしましたが、1つの基準に照らして検索するための、この唯一の作品:

私は、次のしている:

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 

あなたはこれを達成するために2つのcriteriasにAutoFilter使用することができます enter image description here

+0

あなたはStackOverflowの上で同様の問題へのリンクを提供することはできますか? – Petrichor

+0

C列に1つの番号があり、AとBに対応するエントリが必要ですか?範囲 "Number"とは何ですか? – SJR

+0

数字+文字の組み合わせは常に一意ですか、それとも2つ以上の一致が可能ですか? – Chrowno

答えて

1

を支援してください。

コード

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 
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 
+0

フィルタメソッドは単に私の行を隠し、私のフィードバックエントリをコピーしません。 – Constantia

+0

私は代替アプローチが何をしようとしているのか理解していますが、私の現在の「今日のフィードバック」は細胞h2から単に消えてしまい、何も起こりません。 – Constantia

+0

あなたのテーブルの例では、列に空の行があります。 2つの検索パラメータの列は適合していますか?その場合は、値を書き込む箇所にストップマークを設定し、ローカルウィンドウに設定された値を確認します。または、私が追加したdebug.printを使用してください – Chrowno

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