简体   繁体   中英

Calling a cell value and replacing it

在此处输入图像描述

The photo attached is the spreadsheet I am working with. I have a userform that takes a spinbutton value which I defined as "cases". I can get the code to take the spinbutton value and subtract it from the cell I want, however it subtracts it from 0 and not the original value that was already in the cell. For example, I want to subtract 5 from cell C9 but instead of getting 300-5 = 295 for that cell, I am getting a value of -5.

This is my code so far:

    For i = 1 To 5
        For j = 1 To 6
            'If inventorysheet.Cells(inventoryrow, 1).Value = ComboBox1 And inventorysheet.Cells(inventoryrow, 2).Value = ComboBox2 And inventorysheet.Cells(inventoryrow, 3).Value = s Then
                currentinventory = Sheets("Inventory").Range("C" & (7 * k - 2)).Cells(i, j).Value
                Exit Do
            'End If
        Next
    Next
    
    'inventoryrow = inventoryrow + 1
Loop

Sheets("Inventory").Range("C" & (7 * k - 2)).Cells(i, j) = Sheets("Inventory").Range("C" & (7 * k - 2)).Cells(i, j) - cases

For clarification purposes, k optionbutton values, i and j are combobox indexes, I do not believe these are causing me any problems. TIA!

I cannot figure out how to get the code to pull the original value from the cell, it always just subtracts from 0.

just following up. I realized I was overcomplicating things and ended up working it out. This is what ended up working. It turns out what I was doing before was not even the right thing to do. Thank you to GSerg for the comment as well, it allowed me to realize my mistake.

'Check the current inventory level
With Sheets("Inventory")  
    currentinventory = Sheets("Inventory").Range("C" & (7 * k - 2)).Cells(i, j).Value  
    newinventory = currentinventory - cases  
'Update the inventory level
    inventorysheet.Range("C" & (7 * k - 2)).Cells(i, j).Value = newinventory  
End With

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