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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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