I have a macro that works perfectly well when triggered on an onchange event. It simply checks a cell value and hides or unhides rows elsewhere on the same active sheet. Here is the macro that hides or unhides rows:
Sub ToggleTaskTable()
MsgBox "Toggling Tasks"
If Cells(56, 3).Value = "No" Then
Cells(57, 1).EntireRow.Hidden =
MsgBox "Hding Rows 106, 148 and 190"
ActiveSheet.Rows("106").Hidden = True
ActiveSheet.Rows("148").Hidden = True
ActiveSheet.Rows("190").Hidden = True
' try it another way
Rows("106").Hidden = True
Rows("148").Hidden = True
Rows("190").Hidden = True
Else
Cells(57, 1).EntireRow.Hidden = False
' MsgBox "Showing task Rows 106, 148 and 190"
Rows("106").Hidden = False
Rows("148").Hidden = False
Rows("190").Hidden = False
End If
End Sub
If I call the macro as a result of changing a cell C56 to "No", it works perfectly.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$56" Then
Call ToggleTaskTable
End If
End Sub
If I call the macro from another macro, with C56 set to "No", it does not work at all.
Sub CallAllMacros()
Call ToggleTaskTable
End Sub
Even though the msgbox shows indicating it is hiding the rows, it does not actually hide them.
I am totally stumped!
Sub ToggleTaskTable()
With ActiveSheet
.Range("A57,A106,A148,A190").EntireRow.Hidden = (.Cells(56, 3).Value = "No")
End With
End Sub
Related
I used checkboxes, when clicked depending on which checkbox is selected, specific rows unhide.
Code runs fine. Only issue is that code is not triggered by clicking the checkbox but works when I select any cell in the sheet.
Below is some of the code used:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Activate
If Range("C2").Value Or Range("C3").Value Or Range("C4").Value Or Range("C5").Value Or Range("C6").Value Or Range("C7").Value Or Range("C8").Value Or Range("C9").Value Then
Rows("39:52").EntireRow.Hidden = False
Rows("166:169").EntireRow.Hidden = False
Rows("173:175").EntireRow.Hidden = False
Else
Rows("39:52").EntireRow.Hidden = True
Rows("166:169").EntireRow.Hidden = True
Rows("173:175").EntireRow.Hidden = True
End If
End Sub
Because you using event [Worksheet_SelectionChange], so it only run when you change selection cell.
If you want run when you click checkbox, you must write event [click] of checkbox
Ex:
Sub CheckBox1_Click()
End Sub
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
I have an excel macro that is launched by clicking a button.
What macro should do is print out one excel worksheet and increment value in one cell after each print.
Everything works fine EXCEPT the macro ALSO PRINTS the sheet where macro is launched (eventhough that sheet is not selected in code..)
Here is my macro code:
Sub Painike_Napsauta()
Dim i As Long
If MsgBox("Tulosta?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
Cancel = True
Application.EnableEvents = False
Application.Dialogs(xlDialogPrint).Show
Sheets("Lappu").Range("C1").Value = Sheets("Tulosta").Range("C2").Value
For i = Sheets("Tulosta").Range("C3").Value To Sheets("Tulosta").Range("C4").Value
Sheets("Lappu").Range("C2").Value = i
Sheets("Lappu").PrintOut
Next i
Application.EnableEvents = True
End Sub
So all I want to print is "Lappu" sheet in every iteration, but for some reason also "Tulosta" sheet is printed and it is the first page that is printed.
Where is the problem?
Here is the workaround kind of solution to my problem:
Sub Painike_Napsauta()
Dim i As Long
If MsgBox("Tulosta?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
Cancel = True
Application.EnableEvents = False
Sheets("Lappu").Range("C1").Value = Sheets("Tulosta").Range("C2").Value
Sheets("Lappu").Range("C2").Value = Sheets("Tulosta").Range("C3").Value
Sheets("Lappu").Activate
Application.Dialogs(xlDialogPrint).Show
For i = Sheets("Lappu").Range("C2").Value To Sheets("Tulosta").Range("C4").Value - 1
Sheets("Lappu").Range("C2").Value = i + 1
ActiveSheet.PrintOut
Next i
Application.EnableEvents = True
End Sub
So I first select "Lappu" sheet the active one with .Activate and then printout the active sheet with ActiveSheet.PrintOut
This is the first time I'm working with Macros.
I've created a dropdown in B2 with a "Yes" and "No" options.
If User selects "Yes", Row 10 Shows / Row 11 Hides
If User Selects "No", Row 11 Shows / Row 10 Hides
I used this code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then
If Range("B2") = Yes Then
ActiveSheet.Rows("10:10").EntireRow.Hidden = False
ActiveSheet.Rows("11:11").EntireRow.Hidden = True
ElseIf Range("B2") = No Then
ActiveSheet.Rows("10:10").EntireRow.Hidden = True
ActiveSheet.Rows("11:11").EntireRow.Hidden = False
End If
End If
End Sub
I Created a new Module in Sheet1, and put it there. I saved the excel as a Macro Enabled Tamplate, however nothing happens when I change the dropdown.
Thanks for your help!
Do yourself a huge favor and get in the habit of writing Option Explicit at the top of every module of VBA code you write.
I have added comments as well explaining your needed revisions.
'this requires you to dimension all variables
'when you used '= yes' VBA thought you were saying
'the same as, = aVariable
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then
If Range("B2").Value = "Yes" Then
'You can reference the row directly on the same sheet
'and do not need ActiveSheet
Rows("10:10").EntireRow.Hidden = False
Rows("11:11").EntireRow.Hidden = True
ElseIf Range("B2").Value = "No" Then
Rows("10:10").EntireRow.Hidden = True
Rows("11:11").EntireRow.Hidden = False
End If
End If
End Sub
Also be aware this is only using "Yes" - using "yes" or "YES" will cause problems. You can use the UCase method as follows if you want to avoid these situations in the future:
If UCase(Range("B2").Value) = "YES" Then
If Range("B2") = "Yes" Then
and similarly with the "No " option
Update to code & question:
The current code in the module for this sheet is as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B2:B2")) Is Nothing Then
Application.Run "MonthlyRead"
End If
End Sub
Sub MPrintAll()
Dim c As String
Dim MonthlyList As Range
Set MonthlyList = Worksheets("Monthly").Range("MonthlyList").Cells
For Each cell In MonthlyList
Range("b2").Value = cell.Value
ActiveWorkbook.Worksheets("Monthly").PrintOut
Next cell
End Sub
I've been stepping through the code, to try to identify where the problems occur. When I press F8 after "Range("b2").Value = cell.Value" it immediately goes to the first line of code for the sheet, completely skipping the Print command. Also, it deletes the first value in the named range, instead of copy-pasting it to cell B2.
For reference, here's the code in Module1 called by the first routine above:
Sub MonthlyRead()
Call MEFTPS
Call MUCT6
End Sub
Sub MEFTPS()
If Range("a2").Value = "EFTPS Package" Then
Call MShow
Else: Call MHide
End If
End Sub
Sub MHide()
Rows("20:20").Select
Selection.EntireRow.Hidden = True
Rows("31:31").Select
Selection.EntireRow.Hidden = True
Rows("42:42").Select
Selection.EntireRow.Hidden = True
Rows("53:53").Select
Selection.EntireRow.Hidden = True
Range("B2").Select
End Sub
Sub MShow()
Rows("20:20").Select
Selection.EntireRow.Hidden = False
Rows("31:31").Select
Selection.EntireRow.Hidden = False
Rows("42:42").Select
Selection.EntireRow.Hidden = False
Rows("53:53").Select
Selection.EntireRow.Hidden = False
Range("B2").Select
End Sub
Sub MUCT6()
If Range("g3").Value = "Y" Then
Call UCT6MShow
Else: Call UCT6MHide
End If
End Sub
Sub UCT6MHide()
Rows("19:19").Select
Selection.EntireRow.Hidden = True
Rows("30:30").Select
Selection.EntireRow.Hidden = True
Rows("41:41").Select
Selection.EntireRow.Hidden = True
Rows("52:52").Select
Selection.EntireRow.Hidden = True
Range("B2").Select
End Sub
Sub UCT6MShow()
Rows("19:19").Select
Selection.EntireRow.Hidden = False
Rows("30:30").Select
Selection.EntireRow.Hidden = False
Rows("41:41").Select
Selection.EntireRow.Hidden = False
Rows("52:52").Select
Selection.EntireRow.Hidden = False
Range("B2").Select
End Sub
I'm working with a dynamic worksheet that populates an individualized payment schedule, based on a selection from a data validation drop-list at the top of the page. There are approximately 300 options in the drop-list. These schedules are then printed, to verify information obtained from 2 other programs, all of which must be printed, copied, scanned, packed, and mailed in a single day.
I'm looking for VBA code that can select each client name from the drop-list in order, from the beginning to the end of the list. The list is populated from a named range on another sheet, named "QtrlyList."
I have some very simple code, that doesn't work.
Sub PrintAll()
For Each cell In QtrlyList
Worksheets("Normal").PrintOut
Next cell
End Sub
Whenever I try to run the code, I get a "Type Mismatch" error. I'm fairly certain this is coming from "cell" or "QtrlyList." I'm just not sure how to fix it.
Something like this might work for you (untested)
Sub PrintAll()
Dim wb as Workbook, cell as Range
Set Wb = ActiveWorkbook 'or ThisWorkBook if the code is in your reporting workbook
For Each cell In wb.Sheets("SheetNameHere").Range("QtrlyList").Cells
With wb.Worksheets("Normal")
'you want to set the value of whichever cell has the drop-down
.Range("D2")).value=cell.Value
DoEvents 'allow sheet to pick up changed value
.PrintOut
End with
Next cell
End Sub