How to increment number in a cell after printing on excel - excel

I was wondering if some one can help me. I need to increment by one a number in a cell and then clean a few textbox but after the printing. There is an beforeprint event but doesnt do what I want becuase before the sheet is printed all the data is cleaned, obviously (I apologize for my english) Is there a way to do what I need or a better practice for it. Thank you.
Private Sub Workbook_BeforePrint(Cancel As Boolean)
[I6] = [I6] + 1
[C11] = ""
[C12] = ""
Range("B16:B27").ClearContents
Range("C16:C27").ClearContents
Range("D16:D27") = ""
Range("H16:H27").ClearContents
End Sub

To act AFTER printing, you need to print the document via VBA's ActiveSheet.PrintOut inside of the BeforePrint event and set Cancel to true, so it doesn't run its own print too.
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Cancel = True
Application.EnableEvents = False
ActiveSheet.PrintOut
Application.EnableEvents = True
[I6] = [I6] + 1
[C11] = ""
[C12] = ""
Range("B16:B27").ClearContents
Range("C16:C27").ClearContents
Range("D16:D27") = ""
Range("H16:H27").ClearContents
End Sub

Related

Hide Entire given rows if a specified cell is blank

I am a newbie in VBA coding.
I am trying to achieve that inside my function
sub Private Sub Worksheet_Change(ByVal Target As Range)
where it checks some specified cells value change to run a given macro. The additional macro that i want to add is that when ever cell a62 is Empty, it will hide rows a56:a61. and consecutively if a82 is empty, it will hide rows a78:a82.
i have the following code but it only hides the rows from the first empty cell until end of sheet.
Sub Test()
Dim i As Long
For i = 4 To 800
If Sheets("Results").Cells(i, 1).Value = "" Then
Rows(i & ":" & Rows.Count).EntireRow.Hidden = True
Rows("1:" & i - 1).EntireRow.Hidden = False
Exit Sub
End If
Next
End Sub
Please, check the next event code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$62" Or Target.Address = "$A$82" Then
Select Case Target.Address
Case "$A$62"
If Target.Value = "" Then
Range("A56:A61").EntireRow.Hidden = True
Else
Range("A56:A61").EntireRow.Hidden = False
End If
Case "$A$82"
If Target.Value = "" Then
Range("A78:A81").EntireRow.Hidden = True
Else
Range("A78:A81").EntireRow.Hidden = False
End If
End Select
End If
End Sub
It will be triggered only if you MANUALLY change the value of one of the two required cells.
If their value is the result of a formula, Worksheet_Calculate event must be used, but in a different way. This event does not have any argument (no Target) and you must check the two cells in discussion and act according to their value, independent if they were changed or not when the Calculate event is triggered. If this is the case, I can post such an event code, too.
Edited:
For the version which does not involve the manual changing of the values, please copy this event code in the sheet code module:
Private Sub Worksheet_Calculate()
If Me.Range("A62").Value = "" Then
Me.Range("A56:A61").EntireRow.Hidden = True
Else
Me.Range("A56:A61").EntireRow.Hidden = False
End If
If Me.Range("A82").Value = "" Then
Me.Range("A78:A82").EntireRow.Hidden = True
Else
Me.Range("A78:A82").EntireRow.Hidden = False
End If
'Edited:
'The part for both analyzed ranges being empty:
If Me.Range("A62").Value = "" And _
Me.Range("A82").Value = "" Then
'Do here what you need...
End If
End Sub

How to make two textboxes in a userform mutually exclusive

I have two textboxes in a userform.
I need that if the user has entered some number in one textbox and then places the cursor in the other textbox, the first text box data is deleted and vice versa.
I was able to lock the other textbox if one has data in it, as a workaround.
I tried to replicate the lock method for deleting the value, but it does not work.
Sub checkTB()
If ConversionForm.Controls("UnitFromEntry").Text <> "" Then
ConversionForm.Controls("UnitToEntry").Locked = True
Else
ConversionForm.Controls("UnitFromEntry").Locked = True
End If
End Sub
I call this sub in the before update event of the text box.
Private Sub UnitFromEntry_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Call checkTB
End Sub
Try this
Private Sub UnitToEntry_Enter()
UnitToEntry.Locked = False
UnitFromEntry.Locked = True
UnitFromEntry.Text = ""
End Sub
Private Sub UnitFromEntry_Enter()
UnitFromEntry.Locked = False
UnitToEntry.Locked = True
UnitToEntry.Text = ""
End Sub

After user input, and I have autofilled 2 other textboxes, how do I stop my macro from running a search on the other boxes?

I have 3 textboxes, of which when you input data into any one of the textboxes, the other 2 will populate.
the codes are very long, so I've just added what it does at every line of code
Private Sub tbAC_AfterUpdate()
'Declarations
textbox 1 = tbAC
textbox 2 = tbAN
textbox 3 = tbIC
If tbAC is Nothing Then
tbAC = ""
Elseif tbAC.TextLength > 0 Then
'Find tbAC in column AGC
Set MCodef = wsa.Columns(AGC).Find(what:=tbAC, LookIn:=xlValues, Lookat:=xlPart)
If MCodef is Nothing Then
Msgbox ("Invalid Code")
Else
Mcoder = Mcodef.Row
tbAN = wsa.Range(AGN & Mcoder).Value
tbIC = wsa.Range(AIC & Mcoder).Value
End if
End If
End Sub
'It's pretty much the same if you fill in tbAN or tbIC
Private Sub tbAN_AfterUpdate()
End Sub
Private Sub tbIC_AfterUpdate()
End Sub
Given that user input in tbAN, I only want excel to run the sub tbAN_AfterUpdate and not the others. Any help would be appreciated, thanks in advance!
If you have code that is triggered by entering data into the text box, but don't want that code to execute when using an alternate function in VBA, you can use:
Application.EnableEvents = False
So if you are entering into TextBox1 and want to update TextBox2 when you enter into TextBox1, put this into TextBox1's Code. You would use this when the code is first initialized, but you need to remember to reverse that at the end of the code or prior to the code terminating (e.g. through Exit Sub or some other forced command) as once EnableEvents = False, no code will trigger without setting it to:
Application.EnableEvents = True

Checking one box unchecks the adjacent box in Excel

I am currently trying work on a code that can loop through all the check boxes and uncheck a box if the box next to it is checked.
Currently, I have something written out, and I know it's no where near what I need it to be. I know how to do it individually by box:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox2.Value = False
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
CheckBox1.Value = False
End If
End Sub
However, I have several hundred lines of check boxes that I'd have to write this for so if I could just loop through the check boxes it would be a life saver!So I started out with:
Dim i As Integer
i = 1
a = 2
If CheckBoxi.Value = True Then
CheckBoxa.Value = False
End If
If CheckBoxa.Value = True Then
CheckBoxi.Value = False
End If
i = i + 2
a = a + 2
End
However this doesn't seem to work and I have no idea where to go from here. Any help would be greatly appreciated!

excel vba slowing excel down, causing 10 second egg timer delay when clicking anywhere on sheet

I am using the following vba codes which im using to hide a set of rows and unhide rows depending on if a cell contains text or not, and they are causing my excel spreadsheet to be slow and unresponsive and causing the egg timer to show for about 10 seconds.
If I take the code out It speeds things up so what can I do to my codes to get them to speed up and not take so long? perhaps there is a better way of structuring the code but im really new to vba so am not sure what I would need to do, would appreciate someone's help thanks.
the reason I am using worksheet change and worksheet selection change is so that whether a user clicks on a cell or not the page still updates
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("K22").Value <> "" Then
Application.ScreenUpdating = False
Rows("25:38").EntireRow.Hidden = False
Rows("40:48").EntireRow.Hidden = True
ElseIf Range("K22").Value = "" Then
Rows("25:38").EntireRow.Hidden = True
Rows("40:48").EntireRow.Hidden = False
End If
Application.ScreenUpdating = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("K22").Value <> "" Then
Application.ScreenUpdating = False
Rows("25:38").EntireRow.Hidden = False
Rows("40:48").EntireRow.Hidden = True
ElseIf Range("K22").Value = "" Then
Rows("25:38").EntireRow.Hidden = True
Rows("40:48").EntireRow.Hidden = False
End If
Application.ScreenUpdating = True
End Sub
The main issue is from the Worksheet_Change event, but it could be applied to any event.
The worksheet change is triggering each time you hide a column, so it's trying several times to hide the same columns, before (eventually) failing with an out of memory error:
Hide these columns... Oh, a worksheet change... Hide these columns... Oh, A worksheet change... Hide th...
To avoid this, you need to use
Application.EnableEvents = False
when you decide you are going to make changes, and
Application.EnableEvents = True
when done.
You may also want to put some error handling that turns the events on again, as if something else occurs that stops the code from running, the triggers will be turned off, and the spreadsheet will no longer update as you expect it to.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
If Range("K22").Value <> "" Then
Rows("25:38").Hidden = False
Rows("40:48").Hidden = True
Else
Rows("25:38").Hidden = True
Rows("40:48").Hidden = False
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
This works instantly for me:
Application.ScreenUpdating = False
Select Case Range("K22")
Case Is <> ""
Rows("25:38").Hidden = False
Rows("40:48").Hidden = True
Case Else
Rows("25:38").Hidden = True
Rows("40:48").Hidden = False
End Select
Application.ScreenUpdating = True

Resources