VBA Change cells' contents in user's selection

Bowhaven

New Member
Joined
Nov 25, 2016
Messages
6
Hello!

I am trying to make a sheet that is as easy-to-use as possible for the end user. I've made buttons which I would like to use to add extra information into the selected cells, without deleting any pre-existing information. All info will be text strings. The selection the user is making could be in any column and any row.

Here's my code so far:

Code:
Sub MLD()


numRows = Selection.Rows.Count


numColumns = Selection.Columns.Count


Range(Cells(Selection.Row, Selection.Column), Cells(Selection.Row + numRows - 1, Selection.Column + numColumns - 1)).Select


Selection.Value = "MLD "


End Sub

At the moment it works fine for selecting and changing the value of multiple cells to "MLD " but anything that's already in the cell will of course be overwritten.

So I tried this:

Code:
Selection.Value = Selection.Value + "MLD "

Which works absolutely fine if only one cell is selected but turns up a "Run-time error '13': Type mismatch" when multiple cells are selected.

I thought about copying any pre-existing data in the user's selection to a separate column, concatenating it with the MLD then putting the resulting string back into the original selection range. Buuut that's super long-winded and I want to try and make it as easy to modify in the future as possible.

Any thoughts would be greatly appreciated, if I haven't been clear at any point I'll happily clarify!

Thanks,

Chris
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try this:
Code:
Sub MLD()

Dim cell as Range

For Each cell in Selection
    cell = cell & "MLD "
Next cell

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top