May 05 2024 12:52 AM - edited May 05 2024 02:08 AM
I am new to Modal and Modeless Userforms. I am trying to build a simple Userform as a proof of concept that you can change the contents of cells on a sheet and still run your Userform. My goal is to have a Userform that stays loaded, shown, and swaps the values of two cells every time I hit swap cells.
For simplicity's sake, let's just assume that no data validation is needed.
This Userform has two RefEdits and a Command Button called cmdSwapCells. When hit, the command button swaps the contents of the two cells referenced by the RefEdits. Here is the code for the sub, SwapCellValues, which is called when the cmdSwapCells_Enter or the cmdSwapCells_Click events trigger.
Update:
The code seems to work if I use TextBoxes instead of RefEdits. Are RefEdits and vbModeless a known source of bugs?
Sub SwapCellValues(refOne As String, refTwo As String)
Dim RangeA As Range, RangeB As Range
Dim ValA As String, ValB As String
Set RangeA = Range(refOne)
Set RangeB = Range(refTwo)
ValA = RangeA.Value
ValB = RangeB.Value
RangeA.Value = ValB
RangeB.Value = ValA
End Sub
May 05 2024 03:17 AM
@Riley_Johnson RefEdit Controls have always been notoriously glitchy. One thing's for sure, they cannot be used on modeless forms (must be modal). This is well documented online, on various forums and blogs. Here's two such examples:
Also, I recommend ditching the cmdSwapCells_Enter routine, as it's causing the form to crash. Just use the Click event for the command button. See attached...