[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『優先順位をつけて情報を取得したい。』(金太郎)
表題の件で質問です。
例えば、机の個数を調べたくて、
データの中に机という項目があれば、机の個数を、
なければ、椅子の個数を、
机も椅子の項目もなければ、筆箱の個数
それもなければ、その他(何でも良い)の項目の個数を取得したいです。
a列 b列
椅子 50
机 100
ゴミ箱 10
というデータがあれば、100を。
a列 b列
ゴミ箱 30
長テーブル 10
筆箱 30
椅子 5
というデータがあれば、5を取得したいです。
そこで下記のようにしてるのですが、非常に効率が悪く作っており、
メンテの時も時間がかかってしまいます。
何か良いスッキリ書くような方法は無いでしょうか、、?
どうぞ宜しくお願い致します。
dim i
dim lastrow
dim num
dim cnt
lastrow=Cells(Rows.Count, 1).End(xlUp).Row
cnt = 0
for i = 1 to last row
if cells(i,1)="机" then
num = cells(i,2)
cnt =1
exit for
end if
next
for i = 1 to last row
if cells(i,1)="椅子" and cnt =0 then
num = cells(i,2)
cnt =1
exit for
end if
next
for i = 1 to last row
if cells(i,1)="筆箱" and cnt =0 then
num = cells(i,2)
cnt =1
exit for
end if
next
for i = 1 to last row
if cells(i,1)<>"" and cnt =0 then
num = cells(i,2)
exit for
end if
next
< 使用 Excel:Excel2010、使用 OS:Windows7 >
その他の時、どの値持ってくればいいのでしょう? |[A] |[B] [1]|ロッカー | 50 [2]|長テーブル|100 [3]|ゴミ箱 | 10 Sub test1() Dim a As Variant Dim b As Variant Dim c As String For Each a In Array("机", "椅子", "筆箱") b = Evaluate("=VLOOKUP(""" & a & """,A:B,2,0)") If Not IsError(b) Then c = a & "が" & b & "個": Exit For Next a If c <> "" Then MsgBox c Else MsgBox "その他" End If End Sub
(稲葉) 2019/03/11(月) 17:41
Sub test() Dim i As Long Dim cnt As Long
For i = 0 To 2 On Error Resume Next cnt = WorksheetFunction.Match(Array("机", "椅子", "筆箱")(i), Range("A:A"), 0) On Error GoTo 0 If 0 < cnt Then Exit For End If Next i If cnt = 0 Then cnt = Range("A:A").SpecialCells(xlCellTypeConstants)(1).Row End If
MsgBox Cells(cnt, "B").Value, vbInformation, Cells(cnt, "A").Value End Sub (???) 2019/03/11(月) 17:56
???さんご指摘ありがとうございます! >Range("A:A").SpecialCells(xlCellTypeConstants)(1).Row これいいですね。 今後の参考にさせてもらいます。 (稲葉) 2019/03/11(月) 18:43
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.