Button code for module

jrgsea

New Member
Joined
Feb 16, 2015
Messages
18
Hi legends!

I'm trying to code a button that allows you to take the contents of one cell (that will be a time unit - hh:mm, let's say, cell F12) and add it to a range of cells that you define in another cell (say H12).

Could anyone help me with this??

Thank you all for you awesome help!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Well we could make it look like anything at this stage. The idea being that the user would insert the value into F12 to be added, and the range it is applicable to into H12. So, I guess H12=C12:C36 or H12="C12:C36".
Or as you recommend.

Thanks for the reply!
 
Upvote 0
Try this. The user will select the destination range from the mouse.
Code:
Sub test()
    Dim uiRange As Range
    Dim strPrompt As String, strDefault
    
    strPrompt = "Select a range."
    strPrompt = strPrompt & vbCr & "The time in F12 will be added to each cell in this range."
    If TypeName(Selection) = "Range" Then strDefault = Selection.Address(,,,True)
    
    On Error Resume Next
        Set uiRange = Application.InputBox(strPrompt, Default:=strDefault, Type:=8)
    On Error GoTo 0
    If uiRange Is Nothing Then Exit Sub: Rem cancel pressed
    
    Range("F12").Copy
    uiRange.PasteSpecial Paste:=xlAll, Operation:=xlAdd, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Thanks mikerickson!

The only problem is it seems to copy the formatting from F12 to that range as well.

Otherwise selecting the range with the mouse is a great touch!
 
Upvote 0
Try this, untested
Code:
Sub test()
    Dim uiRange As Range
    Dim strPrompt As String, strDefault
    
    strPrompt = "Select a range."
    strPrompt = strPrompt & vbCr & "The time in F12 will be added to each cell in this range."
    If TypeName(Selection) = "Range" Then strDefault = Selection.Address(,,,True)
    
    On Error Resume Next
        Set uiRange = Application.InputBox(strPrompt, Default:=strDefault, Type:=8)
    On Error GoTo 0
    If uiRange Is Nothing Then Exit Sub: Rem cancel pressed
    
    Range("F12").Copy
    uiRange.PasteSpecial Paste:=[COLOR="#FF0000"]xlValues[/COLOR], Operation:=xlAdd, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Thanks again, mikerson!

I actually made that change myself and it worked! Thanks for your help.

Have a good day mate.
 
Upvote 0

Forum statistics

Threads
1,216,144
Messages
6,129,120
Members
449,488
Latest member
qh017

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