Combining of VBA

JackReacher85

Banned user
Joined
Sep 14, 2021
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

Imlooking to create a clear button within an excel worksheet, i need the below vba to be combined to allow it to clear cells and an ActiveX textbox, heres the VBA i have thus far
VBA Code:
Sub Clr()
'
' Clr Macro
'

'
    Range("C7:E8").Select
    Selection.ClearContents
    Range("G7:I8").Select
    Selection.ClearContents
    Range("C12").Select
    Selection.ClearContents
    Range("F12").Select
    Selection.ClearContents
    ActiveWindow.SmallScroll Down:=18
    Range("C25").Select
    Selection.ClearContents
    Range("C26").Select
    Selection.ClearContents
    Range("C27").Select
    Selection.ClearContents
    ActiveWindow.SmallScroll Down:=-36
    Range("C7:E8").Select
End Sub

VBA Code:
Sub Clear_TextBox()
Dim tbx As OLEObject
For Each tbx In ActiveSheet.OLEObjects
If TypeName(tbx.Object) = "TextBox" Then
tbx.Object.Text = ""
End If
Next
End Sub

Appreciate any help you can offer on this one
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Do you just mean something like this ?
(I have assumed your clear textbox code works)

VBA Code:
Sub Clear_TextBox()
Dim tbx As OLEObject
For Each tbx In ActiveSheet.OLEObjects
    If TypeName(tbx.Object) = "TextBox" Then
        tbx.Object.Text = ""
    End If
Next

With ActiveSheet
    .Range("C7:E8, G7:I8, C12, F12, C25:C27").ClearContents
    .Range("C7:E8").Select
End With

End Sub
 
Upvote 0
Thank you, the above didnt work but i managed to come up with this and its working as expected

VBA Code:
Private Sub CommandButton6_Click()
  '  ActiveX Textbox
  TextBox1.Text = ""
      Range("C7:E8").Select
    Selection.ClearContents
    Range("G7:I8").Select
    Selection.ClearContents
    Range("C12").Select
    Selection.ClearContents
    Range("F12").Select
    Selection.ClearContents
    ActiveWindow.SmallScroll Down:=18
    Range("C25").Select
    Selection.ClearContents
    Range("C26").Select
    Selection.ClearContents
    Range("C27").Select
    Selection.ClearContents
    ActiveWindow.SmallScroll Down:=-36
    Range("C7:E8").Select
End Sub

Thank you again for taking the time to help, have a great weekend.
 
Upvote 0
Solution
Glad you got it working.
I am not sure what was not working for you but this:

VBA Code:
With ActiveSheet
    .Range("C7:E8, G7:I8, C12, F12, C25:C27").ClearContents
    .Range("C7:E8").Select
End With

Should do the same as everything you have from Range("C7:E8").Select to the end.
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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