How to NOT repeat a piece of code again and again?
In the short example of above, first find out what is a duplicate (the two outer if statements including their body), than the differences:


Rfb versus Rfd
+5 versus -5


Than make a function for the duplicated code, and turn all differences into a parameter (I don't know the type of Rfd, so I take an int for it).

You will get:

case 12:
  processButton(&amp;Rfb,  5); // Offset magnet distance by +5
  processButton(&amp;RFd, -5); // Offset magnet distance by -5
  break;

  ...


void processButton(int* rf, int diff)
{
  if ((millis() - millistimer) &gt; BounceD) {
    if (receivedValue == *rf) {
      magnetDistance += diff;
      millistimer = millis();
      *rf = 0;
    }
  }
}


Note that if magnetDistance and millistimer are local variables you also have to pass them (as pointer to change the value), if they are globals you can keep the code as is above.

UPDATE

The way parameter works is as follows. E.g. 

processButton(Rfb,  5);


Will call the following function:

void processButton(int rf, int diff)


Where value rf will be Rfb, and diff will be 5 (+5).

Thus when magnetDistance += diff; is executed, diff will be +5 and magnetDistance will be increased with +5. In the next call, magnetDistance will be -5 and thus will be subtracted.

UPDATE 2

To change a variable, you have to pass it as pointer, otherwise the (copied) parameter value will be changed and not return its value to the called function. So you have to give the parameter as an address (&amp; operator), and use -> instead of . to get to the value.