PC Review


Reply
Thread Tools Rate Thread

Clearing an Active X text box with a macro

 
 
covingj
Guest
Posts: n/a
 
      10th Jul 2009

I have an Active X text box in an Excel workbook that I would like to be able
to clear the contents of the box using a macro. Currently the workbook uses a
macro to clear the contents of cells to prepare it for use. I would like to
add instructions to this exiting macro to also clear the text box being used
to enter notes.

Below is the macro. The workbook has multiple sheets, but the text box only
appear on the BG Info sheet. That section appears at teh end of the Macro.

Thanks for any input.

Sub Clear_All()
'
' Clear_All Macro
' Macro recorded 03/03/2005 by covingj
'
'
Sheets("FI Resources").Select
Range("Clear_FI_Resource").Activate
Selection.ClearContents
Range("C8").Select
ActiveCell.FormulaR1C1 = "1500"
Range("B2:C2").Select
Sheets("PW").Select
Range("Clear_PW").Activate
Selection.ClearContents
Range("F3").Select
Sheets("Preg Minor").Select
Range("Clear_Preg_Minor").Activate
Selection.ClearContents
Range("B7:C7").Select
Sheets("FP").Select
Range("Clear_FP").Activate
Selection.ClearContents
Range("F2").Select
Sheets("LIF").Select
Range("Clear_LIF").Activate
Selection.ClearContents
Range("N2").Select
Sheets("TMA").Select
Range("Clear_TMA").Activate
Selection.ClearContents
Range("F2").Select
Sheets("HCPC").Select
Range("Clear_HCPC").Activate
Selection.ClearContents
Range("F2").Select
Sheets("ABD-SLMB").Select
Range("C7").Select
ActiveCell.FormulaR1C1 = "1500"
Range("Clear_ABD").Activate
Selection.ClearContents
Range("G2").Select
Sheets("QI").Select
Range("C7").Select
ActiveCell.FormulaR1C1 = "1500"
Range("Clear_QI").Activate
Selection.ClearContents
Range("G2").Select
Sheets("OSS").Select
Range("Clear_OSS").Activate
Selection.ClearContents
Range("C7").Select
ActiveCell.FormulaR1C1 = "1500"
Range("F2").Select
Sheets("NH-HCBS").Select
Range("E7").Select
ActiveCell.FormulaR1C1 = "1500"
Range("Clear_NH").Activate
Selection.ClearContents
Range("J28").Select
ActiveCell.FormulaR1C1 = "=R[-6]C"
Range("I2:J2").Select
Sheets("IT").Select
Range("Clear_IT").Activate
Selection.ClearContents
Range("C16").Select
ActiveCell.FormulaR1C1 = "='NH-HCBS'!R22C10"
Range("F16").Select
ActiveCell.FormulaR1C1 = "='NH-HCBS'!R22C10"
Range("C7").Select
With ActiveCell.Characters(Start:=1, Length:=1)
End With
Range("C7").Select
Sheets("BG Info").Select
Range("Clear_BG_Info").Activate
Selection.ClearContents
Range("Primary").Select
End Sub

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      10th Jul 2009

If you know the name of the textbox:
Worksheets("BG Info").TextBox1.Value = ""

ps.

This code:

> Sheets("FI Resources").Select
> Range("Clear_FI_Resource").Activate
> Selection.ClearContents
> Range("C8").Select
> ActiveCell.FormulaR1C1 = "1500"


could be rewritten as:

Sheets("FI Resources").Range("Clear_FI_Resource").clearcontents
sheets("FI Resources").Range("C8").value = 1500

This kind of thing will make the code run a bit faster, but even better, it's
easier to understand.

covingj wrote:
>
> I have an Active X text box in an Excel workbook that I would like to be able
> to clear the contents of the box using a macro. Currently the workbook uses a
> macro to clear the contents of cells to prepare it for use. I would like to
> add instructions to this exiting macro to also clear the text box being used
> to enter notes.
>
> Below is the macro. The workbook has multiple sheets, but the text box only
> appear on the BG Info sheet. That section appears at teh end of the Macro.
>
> Thanks for any input.
>
> Sub Clear_All()
> '
> ' Clear_All Macro
> ' Macro recorded 03/03/2005 by covingj
> '
> '
> Sheets("FI Resources").Select
> Range("Clear_FI_Resource").Activate
> Selection.ClearContents
> Range("C8").Select
> ActiveCell.FormulaR1C1 = "1500"
> Range("B2:C2").Select
> Sheets("PW").Select
> Range("Clear_PW").Activate
> Selection.ClearContents
> Range("F3").Select
> Sheets("Preg Minor").Select
> Range("Clear_Preg_Minor").Activate
> Selection.ClearContents
> Range("B7:C7").Select
> Sheets("FP").Select
> Range("Clear_FP").Activate
> Selection.ClearContents
> Range("F2").Select
> Sheets("LIF").Select
> Range("Clear_LIF").Activate
> Selection.ClearContents
> Range("N2").Select
> Sheets("TMA").Select
> Range("Clear_TMA").Activate
> Selection.ClearContents
> Range("F2").Select
> Sheets("HCPC").Select
> Range("Clear_HCPC").Activate
> Selection.ClearContents
> Range("F2").Select
> Sheets("ABD-SLMB").Select
> Range("C7").Select
> ActiveCell.FormulaR1C1 = "1500"
> Range("Clear_ABD").Activate
> Selection.ClearContents
> Range("G2").Select
> Sheets("QI").Select
> Range("C7").Select
> ActiveCell.FormulaR1C1 = "1500"
> Range("Clear_QI").Activate
> Selection.ClearContents
> Range("G2").Select
> Sheets("OSS").Select
> Range("Clear_OSS").Activate
> Selection.ClearContents
> Range("C7").Select
> ActiveCell.FormulaR1C1 = "1500"
> Range("F2").Select
> Sheets("NH-HCBS").Select
> Range("E7").Select
> ActiveCell.FormulaR1C1 = "1500"
> Range("Clear_NH").Activate
> Selection.ClearContents
> Range("J28").Select
> ActiveCell.FormulaR1C1 = "=R[-6]C"
> Range("I2:J2").Select
> Sheets("IT").Select
> Range("Clear_IT").Activate
> Selection.ClearContents
> Range("C16").Select
> ActiveCell.FormulaR1C1 = "='NH-HCBS'!R22C10"
> Range("F16").Select
> ActiveCell.FormulaR1C1 = "='NH-HCBS'!R22C10"
> Range("C7").Select
> With ActiveCell.Characters(Start:=1, Length:=1)
> End With
> Range("C7").Select
> Sheets("BG Info").Select
> Range("Clear_BG_Info").Activate
> Selection.ClearContents
> Range("Primary").Select
> End Sub


--

Dave Peterson
 
Reply With Quote
 
covingj
Guest
Posts: n/a
 
      10th Jul 2009
Thanks, that worked like a charm.

As far as the code, that's what you get when you let Microsoft record a
macro for you. It is indented to make it a little easier on the eyes, but the
formating did not carry over when I pasted. When I have time, I'll try to
clean it up like you suggested. Of course it would help if I understood what
I was doing and not just making it up as I went, LOL.

Thanks again.

"Dave Peterson" wrote:

> If you know the name of the textbox:
> Worksheets("BG Info").TextBox1.Value = ""
>
> ps.
>
> This code:
>
> > Sheets("FI Resources").Select
> > Range("Clear_FI_Resource").Activate
> > Selection.ClearContents
> > Range("C8").Select
> > ActiveCell.FormulaR1C1 = "1500"

>
> could be rewritten as:
>
> Sheets("FI Resources").Range("Clear_FI_Resource").clearcontents
> sheets("FI Resources").Range("C8").value = 1500
>
> This kind of thing will make the code run a bit faster, but even better, it's
> easier to understand.
>
> covingj wrote:
> >
> > I have an Active X text box in an Excel workbook that I would like to be able
> > to clear the contents of the box using a macro. Currently the workbook uses a
> > macro to clear the contents of cells to prepare it for use. I would like to
> > add instructions to this exiting macro to also clear the text box being used
> > to enter notes.
> >
> > Below is the macro. The workbook has multiple sheets, but the text box only
> > appear on the BG Info sheet. That section appears at teh end of the Macro.
> >
> > Thanks for any input.
> >
> > Sub Clear_All()
> > '
> > ' Clear_All Macro
> > ' Macro recorded 03/03/2005 by covingj
> > '
> > '
> > Sheets("FI Resources").Select
> > Range("Clear_FI_Resource").Activate
> > Selection.ClearContents
> > Range("C8").Select
> > ActiveCell.FormulaR1C1 = "1500"
> > Range("B2:C2").Select
> > Sheets("PW").Select
> > Range("Clear_PW").Activate
> > Selection.ClearContents
> > Range("F3").Select
> > Sheets("Preg Minor").Select
> > Range("Clear_Preg_Minor").Activate
> > Selection.ClearContents
> > Range("B7:C7").Select
> > Sheets("FP").Select
> > Range("Clear_FP").Activate
> > Selection.ClearContents
> > Range("F2").Select
> > Sheets("LIF").Select
> > Range("Clear_LIF").Activate
> > Selection.ClearContents
> > Range("N2").Select
> > Sheets("TMA").Select
> > Range("Clear_TMA").Activate
> > Selection.ClearContents
> > Range("F2").Select
> > Sheets("HCPC").Select
> > Range("Clear_HCPC").Activate
> > Selection.ClearContents
> > Range("F2").Select
> > Sheets("ABD-SLMB").Select
> > Range("C7").Select
> > ActiveCell.FormulaR1C1 = "1500"
> > Range("Clear_ABD").Activate
> > Selection.ClearContents
> > Range("G2").Select
> > Sheets("QI").Select
> > Range("C7").Select
> > ActiveCell.FormulaR1C1 = "1500"
> > Range("Clear_QI").Activate
> > Selection.ClearContents
> > Range("G2").Select
> > Sheets("OSS").Select
> > Range("Clear_OSS").Activate
> > Selection.ClearContents
> > Range("C7").Select
> > ActiveCell.FormulaR1C1 = "1500"
> > Range("F2").Select
> > Sheets("NH-HCBS").Select
> > Range("E7").Select
> > ActiveCell.FormulaR1C1 = "1500"
> > Range("Clear_NH").Activate
> > Selection.ClearContents
> > Range("J28").Select
> > ActiveCell.FormulaR1C1 = "=R[-6]C"
> > Range("I2:J2").Select
> > Sheets("IT").Select
> > Range("Clear_IT").Activate
> > Selection.ClearContents
> > Range("C16").Select
> > ActiveCell.FormulaR1C1 = "='NH-HCBS'!R22C10"
> > Range("F16").Select
> > ActiveCell.FormulaR1C1 = "='NH-HCBS'!R22C10"
> > Range("C7").Select
> > With ActiveCell.Characters(Start:=1, Length:=1)
> > End With
> > Range("C7").Select
> > Sheets("BG Info").Select
> > Range("Clear_BG_Info").Activate
> > Selection.ClearContents
> > Range("Primary").Select
> > End Sub

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      10th Jul 2009
The problem with the Recorder is it must encode each action you take one at
a time... it has no facilities to optimize the code after it has done this.
Perhaps this previous posting of mine will help you when you do get around
to cleaning your code up:

Whenever you see code constructed like this...

Range("A1").Select
Selection.<whatever>

you can almost always do this instead...

Range("A1").<whatever>

In your particular case, you have this...

Range("C2:C8193").Select 'select cells to export
For Each r In Selection.Rows

which, using the above concept, can be reduced to this...

For Each r In Range("C2:C8193").Rows

Notice, all I have done is replace Selection with the range you Select(ed)
in the previous statement and eliminate the process of doing any
Select(ion)s. Stated another way, the Selection produced from
Range(...).Select is a range and, of course, Range(...) is a range... and,
in fact, they are the same range, so it doesn't matter which one you use.
The added benefit of not selecting ranges first is your active cell does not
change.

--
Rick (MVP - Excel)


"covingj" <(E-Mail Removed)> wrote in message
news:09727A35-7C09-4F78-906C-(E-Mail Removed)...
> Thanks, that worked like a charm.
>
> As far as the code, that's what you get when you let Microsoft record a
> macro for you. It is indented to make it a little easier on the eyes, but
> the
> formating did not carry over when I pasted. When I have time, I'll try to
> clean it up like you suggested. Of course it would help if I understood
> what
> I was doing and not just making it up as I went, LOL.
>
> Thanks again.
>
> "Dave Peterson" wrote:
>
>> If you know the name of the textbox:
>> Worksheets("BG Info").TextBox1.Value = ""
>>
>> ps.
>>
>> This code:
>>
>> > Sheets("FI Resources").Select
>> > Range("Clear_FI_Resource").Activate
>> > Selection.ClearContents
>> > Range("C8").Select
>> > ActiveCell.FormulaR1C1 = "1500"

>>
>> could be rewritten as:
>>
>> Sheets("FI Resources").Range("Clear_FI_Resource").clearcontents
>> sheets("FI Resources").Range("C8").value = 1500
>>
>> This kind of thing will make the code run a bit faster, but even better,
>> it's
>> easier to understand.
>>
>> covingj wrote:
>> >
>> > I have an Active X text box in an Excel workbook that I would like to
>> > be able
>> > to clear the contents of the box using a macro. Currently the workbook
>> > uses a
>> > macro to clear the contents of cells to prepare it for use. I would
>> > like to
>> > add instructions to this exiting macro to also clear the text box being
>> > used
>> > to enter notes.
>> >
>> > Below is the macro. The workbook has multiple sheets, but the text box
>> > only
>> > appear on the BG Info sheet. That section appears at teh end of the
>> > Macro.
>> >
>> > Thanks for any input.
>> >
>> > Sub Clear_All()
>> > '
>> > ' Clear_All Macro
>> > ' Macro recorded 03/03/2005 by covingj
>> > '
>> > '
>> > Sheets("FI Resources").Select
>> > Range("Clear_FI_Resource").Activate
>> > Selection.ClearContents
>> > Range("C8").Select
>> > ActiveCell.FormulaR1C1 = "1500"
>> > Range("B2:C2").Select
>> > Sheets("PW").Select
>> > Range("Clear_PW").Activate
>> > Selection.ClearContents
>> > Range("F3").Select
>> > Sheets("Preg Minor").Select
>> > Range("Clear_Preg_Minor").Activate
>> > Selection.ClearContents
>> > Range("B7:C7").Select
>> > Sheets("FP").Select
>> > Range("Clear_FP").Activate
>> > Selection.ClearContents
>> > Range("F2").Select
>> > Sheets("LIF").Select
>> > Range("Clear_LIF").Activate
>> > Selection.ClearContents
>> > Range("N2").Select
>> > Sheets("TMA").Select
>> > Range("Clear_TMA").Activate
>> > Selection.ClearContents
>> > Range("F2").Select
>> > Sheets("HCPC").Select
>> > Range("Clear_HCPC").Activate
>> > Selection.ClearContents
>> > Range("F2").Select
>> > Sheets("ABD-SLMB").Select
>> > Range("C7").Select
>> > ActiveCell.FormulaR1C1 = "1500"
>> > Range("Clear_ABD").Activate
>> > Selection.ClearContents
>> > Range("G2").Select
>> > Sheets("QI").Select
>> > Range("C7").Select
>> > ActiveCell.FormulaR1C1 = "1500"
>> > Range("Clear_QI").Activate
>> > Selection.ClearContents
>> > Range("G2").Select
>> > Sheets("OSS").Select
>> > Range("Clear_OSS").Activate
>> > Selection.ClearContents
>> > Range("C7").Select
>> > ActiveCell.FormulaR1C1 = "1500"
>> > Range("F2").Select
>> > Sheets("NH-HCBS").Select
>> > Range("E7").Select
>> > ActiveCell.FormulaR1C1 = "1500"
>> > Range("Clear_NH").Activate
>> > Selection.ClearContents
>> > Range("J28").Select
>> > ActiveCell.FormulaR1C1 = "=R[-6]C"
>> > Range("I2:J2").Select
>> > Sheets("IT").Select
>> > Range("Clear_IT").Activate
>> > Selection.ClearContents
>> > Range("C16").Select
>> > ActiveCell.FormulaR1C1 = "='NH-HCBS'!R22C10"
>> > Range("F16").Select
>> > ActiveCell.FormulaR1C1 = "='NH-HCBS'!R22C10"
>> > Range("C7").Select
>> > With ActiveCell.Characters(Start:=1, Length:=1)
>> > End With
>> > Range("C7").Select
>> > Sheets("BG Info").Select
>> > Range("Clear_BG_Info").Activate
>> > Selection.ClearContents
>> > Range("Primary").Select
>> > End Sub

>>
>> --
>>
>> Dave Peterson
>>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro for clearing cells mocc Microsoft Excel Misc 11 19th Sep 2009 10:43 PM
run macro although blinking cursor is active in an active cell bartman1980 Microsoft Excel Programming 1 20th Dec 2007 11:29 AM
macro for clearing autofilter =?Utf-8?B?Qm9yaXNT?= Microsoft Excel Programming 0 12th Sep 2005 07:38 PM
Clearing all Active Cells JohnHill Microsoft Excel Worksheet Functions 2 26th Jul 2005 06:30 AM
Macro - Cell clearing =?Utf-8?B?UGF1bCBI?= Microsoft Excel Programming 11 7th Jan 2005 05:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:40 PM.