VBA, input box don't work with multiple selection

ToseSenpai

New Member
Joined
Apr 18, 2021
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Hi all, i need some help for this vba code! Can you please help me?
I got this VBA code that allows me to insert a value (with a input box) in a active cell and then fill cell with a certain color.
It works perfecly when i select only one cell, but how can i do that with multiple selection?

Thank you very much for your help :)

VBA Code:
Sub ButtonInsert()

  
    Dim cell As Object
    Dim Count As Integer
  
On Error Resume Next
   Dim dblAmount As Double
   dblAmount = InputBox("Insert Hours", "Insert")
   If dblAmount <> 0 Then
      ActiveCell = dblAmount
   Else
      MsgBox "You must insert a number"
 Exit Sub
   End If


Count = 0
    For Each cell In Selection
        Count = Count + 1
    Next cell

    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 15773696
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

  
    ActiveSheet.Calculate
End Su
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
This seems to work. HTH. Dave
Code:
Sub ButtonInsert()   
   Dim cel As Range
    Dim Count As Integer
  
'On Error Resume Next
   Dim dblAmount As Double
   dblAmount = InputBox("Insert Hours", "Insert")
   If dblAmount = 0 Then
      'ActiveCell = dblAmount
   'Else
      MsgBox "You must insert a number"
 Exit Sub
   End If


Count = 0
    For Each cel In Selection
        Count = Count + 1
    cel.Value = dblAmount

    With cel.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 15773696
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
Next cel
  
    ActiveSheet.Calculate
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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