This is my first program in VBA.
I have an excel sheet which contains multiple questions and each question has a check box to make it editable or read only.
Here below an example
How many cars you own?
How many free coupon you have?
So introducing two check boxes, using that I can make them read only or editable.
So I have tried a vba code to do the same(by Googling). Here below is the code snippet.
Private Sub CheckBox13_Click()
If Sheet3.CheckBox13.Value = False Then
Sheet3.Range("B20:CZ20").Interior.ColorIndex = 16
Range("B20:CZ20").Locked = True
ActiveSheet.Protect Contents:=True
Else
ActiveSheet.Protect Contents:=False
Range("B20:CZ20").Locked = False
Sheet3.Range("B20:CZ20").Interior.ColorIndex = 0
End If
End Sub
Private Sub CheckBox14_Click() 'eigth question Hide check box code
If Sheet3.CheckBox14.Value = False Then
Sheet3.Range("B21:CZ21").Interior.ColorIndex = 16
Range("B21:CZ21").Locked = True
ActiveSheet.Protect Contents:=True
Else
ActiveSheet.Protect Contents:=False
Range("B21:CZ21").Locked = False
Sheet3.Range("B21:CZ21").Interior.ColorIndex = 0
End If
End Sub
My problem is:
Default both rows are editable and both check boxes unchecked
Now I check the first check box, so first row color changed and
became read only.
Now I check the second check box. Getting an error.
error 1004, Application defined or object defined error.
Let me know if I missed out any basic information in order to understand the problem.
Since we don't know where the problem happen, I would try this :
Private Sub CheckBox13_Click()
If Sheet3.CheckBox13.Value = False Then
Sheet3.Protect Contents:=False
Sheet3.Range("B21:CZ21").Locked = False
Sheet3.Range("B20:CZ20").Interior.ColorIndex = 16
Sheet3.Range("B20:CZ20").Locked = True
Sheet3.Protect Contents:=True
Else
Sheet3.Protect Contents:=False
Sheet3.Range("B20:CZ20").Locked = False
Sheet3.Range("B20:CZ20").Interior.ColorIndex = 0
End If
End Sub
Private Sub CheckBox14_Click()
If Sheet3.CheckBox14.Value = False Then
Sheet3.Protect Contents:=False
Sheet3.Range("B21:CZ21").Locked = False
Sheet3.Range("B21:CZ21").Interior.ColorIndex = 16
Sheet3.Range("B21:CZ21").Locked = True
Sheet3.Protect Contents:=True
Else
Sheet3.Protect Contents:=False
Sheet3.Range("B21:CZ21").Locked = False
Sheet3.Range("B21:CZ21").Interior.ColorIndex = 0
End If
End Sub
Details :
I've added Sheet3 before your ranges, (and replaced ActiveSheet by Sheet3) to be sur you're not modifying another Sheet.
I've added ActiveSheet.Protect Contents:=False - Sheet3.Range("B21:CZ21").Locked = False before modifying Range colors to be sure that the Range is not protected.
Related
I'm trying to show and hide sheets in excel using the if else syntax but i get this error in the first line
'SUB OR FUNCTION NOT DEFINED'
the code is in** sheet 3**
any help is appreciated!
Thank you/Faleminderit
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Me.Range("C1"), Target) Is Nothing Then Exit Sub
If Worksheets(Sheet3).Range(C1).Value = "INTERIM" Then
ShowHide (False)
Worksheets(Sheet1).Visible = True
Worksheets(Sheet3).Visible = True
ElseIf Worksheets(Sheet3).Range(C1) = "ESTIMATED_BUDGET" Then
ShowHide (False)
Worksheets(Sheet5).Visible = True
Worksheets(Sheet3).Visible = True
ElseIf Worksheets(Sheet3).Range(C1) = "Final" Then
ShowHide (False)
Worksheets(Sheet6).Visible = True
Worksheets(Sheet3).Visible = True
ElseIf Worksheets(Sheet3).Range(C1) = "ALL" Then
ShowHide (True)
End If
End Sub
Sub Macro1()
End Sub
I tried different ways to do it and i get the same error
When testing here I created 2 sheets so I had Sheet1, Sheet2, and Sheet 3.
I had hadded your code to the cose of sheet 1 initially and straight away was getting errors. I think it is important to note that sheet names and cell references or ranges so need to be encapsualted with Quote marks ("Sheet3").
Once the Sheets and Cells where quoted I was able to continue. I'm not sure on the purpose of "ShowHide". I will assume you wanted to track if the sheets are visible or not, this will need to be declared.
I also recommend setting 'Option Explicit' at the top of the module, this helps with error handling and debugging when you have problems.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ShowHide As Boolean
ShowHide = False
If Intersect(Me.Range("C1"), Target) Is Nothing Then Exit Sub
If Worksheets("Sheet3").Range("C1").Value = "INTERIM" Then
ShowHide = False
Worksheets("Sheet1").Visible = True
Worksheets("Sheet3").Visible = True
ElseIf Worksheets("Sheet3").Range("C1") = "ESTIMATED_BUDGET" Then
ShowHide = False
Worksheets("Sheet5").Visible = True
Worksheets("Sheet3").Visible = True
ElseIf Worksheets("Sheet3").Range("C1") = "Final" Then
ShowHide = False
Worksheets("Sheet6").Visible = True
Worksheets("Sheet3").Visible = True
ElseIf Worksheets("Sheet3").Range("C1") = "ALL" Then
ShowHide = True
End If
End Sub
This worked as I expected.
I'm creating a workbook in excel and using activeX checkboxes to hide/unhide some rows each time as a way to enable or disable them. For that I use VBA code. However in that code I specify a static range of rows. The problem is that if I insert a new row, everything is now offset and i have to manually rewrite all the intervals. Is there a way to do this dynamically.
Here is the code I use for two consecutive checkboxes:
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
[24:41].EntireRow.Hidden = False
Else: [24:41].EntireRow.Hidden = True
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2 = True Then
[42:49].EntireRow.Hidden = False
Else: [42:49].EntireRow.Hidden = True
End If
End Sub
I have searched for a while before asking the question, and I apologise if there is an answer somewhere, I'm a total newbie to excel and VBA and I don't think I can adapt a solution that is remotely similar to the problem I'm facing.
Thanks for your precious help in advance.
Have a Column in which you write an X in each row you want to hide / unhide.
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
For each c in Range("A1:A50")
If c.Value = "x" Then
c.EntireRow.Hidden = False
End If
Next c
Else
For each c in Range("A1:A50")
If c.Value = "x" Then
c.EntireRow.Hidden = True
End If
Next c
End If
End Sub
You could try naming your range
thisworkbook.names.add name:="mydinamicrange" ,ReferstoR1C1:="Sheet1!R24c1:R49C10"
Then , you may use it with something like this :
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
thisworkbook.names("mydinamicrange").referstorange.EntireRow.Hidden = False
Else: thisworkbook.names("mydinamicrange").referstorange.EntireRow.Hidden= True
End If
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!
I'm currently in the process of building a dashboard in excel 2016 using VBA and macros. It basically involves selecting buttons on a single sheet to show/hide different chart elements whose data sources will be linked to another sheet. Its going well so far, but I've run into efficiency issues. The first issue has to do with grouping objects and the second to do with repetition of vba code.
1) Is there any way to cross-group shape objects? Right now it seems that I have to create duplicates for objects which will not be visible, and for which each parent group of objects will be hidden. Unfortunately, I cannot find a way for Excel to cross group. So if I want a group A and B and a group A and C, I can only apparently have one group (A&B or A&C, but not both). This forces me to duplicate objects so I have one pair for A&B and another for A&C. The problem is this can get quite tedious and a nightmare in organizing my objects properly when I get several dashboard buttons and sub-sections.
2) Could anyone advise on how I can make the following code more efficient so there is no repeating of code? It basically highlights a group of shapes that must show and hide with respect to what button is pressed. But right now, I have to write all the groups that must stay visible and hidden. Below is a sample of the code. Please see the True and False arguments below to get an idea of what the issue is:
Sub Pic_1_SA_click()
ActiveSheet.Shapes("Group 23").Visible = True
ActiveSheet.Shapes("Group 71").Visible = False
ActiveSheet.Shapes("Group 19").Visible = False
ActiveSheet.Shapes("Group 20").Visible = False
End Sub
Sub Pic_1_SB_click()
ActiveSheet.Shapes("Group 23").Visible = False
ActiveSheet.Shapes("Group 71").Visible = True
ActiveSheet.Shapes("Group 19").Visible = False
ActiveSheet.Shapes("Group 20").Visible = False
End Sub
Sub Pic_2_SA_click()
ActiveSheet.Shapes("Group 23").Visible = False
ActiveSheet.Shapes("Group 71").Visible = False
ActiveSheet.Shapes("Group 19").Visible = True
ActiveSheet.Shapes("Group 20").Visible = False
End Sub
Sub Pic_2_SB_click()
ActiveSheet.Shapes("Group 23").Visible = False
ActiveSheet.Shapes("Group 71").Visible = False
ActiveSheet.Shapes("Group 19").Visible = False
ActiveSheet.Shapes("Group 20").Visible = True
End Sub
Any guidance on these two issues will be greatly appreciated. Thanks!
I simple code refactoring should do it
Sub Pic_SA_click(ByVal sShapeName As String)
If (sShapeName = vbNullString) Then Exit Sub
Dim oShp As Shape, s As Shape
Set oShp = ActiveSheet.Shapes(sShapeName)
If (oShp Is Nothing) Then Exit Sub
oShp.Visible = True
With ActiveSheet
For Each s In .Shapes
If (s.Name <> oShp.Name) Then
s.Visible = False
End If
Next s
End With
End Sub
Sub Pic_1_SA_click()
call Pic_SA_click("Group 23")
end sub
Sub Pic_1_SB_click()
call Pic_SA_click("Group 71")
End Sub
Sub Pic_2_SA_click()
call Pic_SA_click("Group 19")
End Sub
Sub Pic_2_SB_click()
call Pic_SA_click("Group 20")
End Sub
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