简体   繁体   中英

Cannot make .Offset to work in excel vba macro

I am stuck, and would like a little hlep. I'm trying to get a qty on Column D12, d14, d16 .... to d42. Than multiply this qty by a value. the value sheet look like this.

Value Sheet

         A        B          C
       ItemName   Quality   Confort
  1     Chair     2          1
  2     Bed       0          3
  3     Table     1          1

Quantity Sheet

      A      B         C      D
            ItemName        QTYColumn
12    ..    Table             2
13 
14    ..    Chair             5 
15          
16    ..    Bed               6

Total Sheet

    A         B    
  Quality    12    (2*1 + 5*2 + 6*0 )
  Confort    25    (2*1 + 5*1 + 6*3 )

I'm pretty sur I have the hardest part done. I can check and grab the quantity from all the sheets I want. I also got a function done where you pass the name of the item, and the stats name, and it return me the results I want.

so, I got this part of the code atm which doesnt work, and its driving me nuts.

 For Counter = 12 To 42 Step 2
     For Each qColumn In QTYColumn

       Set QTY = Range(qColumn & Counter)
       Dim ItemName As Range
       ItemName= QTY.Offset(-2, 0).Select

       total = total + (QTY * GetValue(ItemName, "Confort"))
    Next qColumn 
Next Counter

My problem is with the ItemName variable. Its always empty and as soon as I get to it with the debugger, the function stops and it closes. Anyone have any idea as to why ? it's important for me to get it base on the offset -2 and not the column adress because it might be different depending of the sheet, and the only "sure" way to find it is the get the 2nd cell to the left of the quantity cell.

ItemName= QTY.Offset(-2, 0).Select does not mean anything !

Either you Select:
QTY.Offset(-2, 0).Select

or you get the value:
ItemName= QTY.Offset(-2, 0).Value '(value can be omitted here)
But then, Dim ItemName As Range does not make sense. It should be a String or aa number.

or you get the range:
Set ItemName= QTY.Offset(-2, 0) ' then you need Set

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM