I am currently working on an excel application where I have a macro, operated by a button click, which resets the numerical values within certain cells in a table.
Within this table there are 3 columns; "Quantity Fitted (n)", "Quantity Required (m)" and "Lock Configuration".
What I need to happen, is when the button is clicked, the numerical values for each line within the "Quantity Fitted (n)" column are reset to match the values displayed within the "Quantity Required (m)" column of the same line.
However, if the cell value within the "Lock Configuration" column of that row is set to "locked", I want the "Quantity Fitted (n)" value to remain unchanged after the button click.
I hope this makes sense! Here's my current attempt to code this:
Public Sub Reset_Quantity_Fitted()
'Macro to make quantity fitted equal to quantity required
Dim rng As Range
Dim cell As Range
Set rng = Worksheets(ActiveSheet.Name).ListObjects("Table_" & ActiveSheet.Name).ListColumns("Quantity Fitted (n)").DataBodyRange
For Each cell In rng.Cells
If rng.Offset(, 5) = "Locked" Then
cell = Worksheets(ActiveSheet.Name).ListObjects("Table_" & ActiveSheet.Name).ListColumns("Quantity Fitted (n)").DataBodyRange
Else
cell = Worksheets(ActiveSheet.Name).ListObjects("Table_" & ActiveSheet.Name).ListColumns("Quantity Required (m)").DataBodyRange
End If
Next cell
End Sub
This approach was recommended by another user on this site, however hen running this code I get the following error:
Run-time error '13': Type mismatch
Can anyone help me identify what is wrong with this code?
If rng.Offset(, 5) = "Locked" ThenbeIf Cell.Offset(, 5) = "Locked" ThenWorksheets(ActiveSheet.Name)can be shortened to justActiveSheet- is the correct sheet active when you run the code? Are your tables named correctly?cellis a single cell,Worksheets(ActiveSheet.Name).ListObjects("Table_" & ActiveSheet.Name).ListColumns("Quantity Fitted (n)").DataBodyRangeis a column of cells. As you're not usingSetwhen updating thecellreference it's probably returning the value from the first cell in the range - which on the first iteration would be the value ofcellif the fifth column is Locked.If rng.Offset(,5) = "Locked" ThenforIf cell.Offset(,5) = "Locked" ThenThis has now solved the mismatch error! Thank you! I hadn't spotted that. Unfortunately, it now simply deletes all values and leaves the cells blank. It's a step forward though, thanks :)Setwhen updating the cell?