2016-06-17 2 views
0

私はVBAの初心者で、私の質問に助けを求めています。私は、次の操作を行うにしようとしています:2に二つの値の組み合わせに対応する行を見つけるために私のスプレッドシート内のカラムに通してVBAのIF文を使用して範囲オブジェクトを返す

  1. ループを
  2. 次に返すように水平に行くその組み合わせに対応する行をループif文を渡すすべての値の範囲(> 40)(if文を渡す数値のセットは、常に1行につき1セットだけ連続する&です(15,34,32,42,45,56,67,56 、43、39、23、14)
  3. また、別の行の日付範囲を抽出できるようにif文を渡す最初と最後の値の列番号を返すこともできます。
  4. For r = 1 To 10000 'Loop through 10000 rows to find the correct ACV field 
           If WkSht.Range("B" & r).Value = "%ACV" And WkSht.Range("C" & r) = bp_upc Then 
            'For column in row(r) 
             'If column > 40 add cell address to range object to be returned 
            'Next Column 
           'End If 
    

    が次に別の行の値を取得するために、その範囲内の最初と最後のセルアドレスの列成分を使用する:

擬似コードでは、このようなものであろう。

何か助けやヒントをいただければ幸いです。

答えて

1
public sub test 
    dim res as range 
    dim i as long 

    for i = 1 to 10000 
    if WkSht.cells(i, 2).value = "%ACV" and WkSht.cells(i, 3) = bp_upc then 
     dim numbers_range as range 
     with application.intersect(WkSht.rows(i), WkSht.usedrange) 
     set numbers_range = WkSht.range(WkSht.cells(i, 4), .cells(.cells.count)) 
     end with 

     if res is nothing then 
     set res = Get40Range(numbers_range) 
     else 
     dim current_res as range 
     set current_res = Get40Range(numbers_range) 

     if not current_res is nothing then set res = application.union(res, current_res) 
     end if 
    end if 
    next 

    'use res 
    'e.g. print res.address, do res.select or loop over res.areas 
end sub 

private function Get40Range(byval row as range) as range 
    dim c as range, start40 as range, end40 as range 

    for each c in row 
    if c.value > 40 then 
     if start40 is nothing then set start40 = c 
     set end40 = c 
    elseif c.value <= 40 and not start40 is nothing then 
     exit for 
    end if 
    next 

    if end40 is nothing and not start40 is nothing then 
    set end40 = row.cells(row.cells.count) 
    end if 

    if not start40 is nothing then set Get40Range = start40.resize(, end40.column - start40.column + 1) 
end function 
+0

Wowza - これは、提供される小さな擬似コードのための大きな仕事です。期待しています。 – dbmitch

+0

@GSerg素早い返信をありがとう。あなたが提供したコードは、私が必要とするものを正確に行います。私はあなたが使用したいくつかの機能を調べなければなりませんでしたが、それは理にかなっています。再度、感謝します! –

関連する問題