I have this code:
Dim name, surname As Boolean
surname, name = False
It doesnt work, is there a way for my above code to work? I looked everywhere and cant seem to find a suitable fix. Thanks!
This isn't supported in VB.net.
You are stuck with:
Dim name As Boolean = False
Dim surname As Boolean = False
Boolean is initialized False by default, so if that's the value you want, there is no need to set them False explicitly at initialization.
If you declare it inside a loop, like the code below, be careful, because it will only be initialized once. Only the first Console.WriteLine in the first iteration will output False. If you want it to be initialized each time, you have to do Dim a As Boolean = False.
For i As Integer = 1 To 10
Dim a As Boolean
Console.WriteLine(a)
a = True
console.WriteLine(a)
Next
Output:
False
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
Related
Brand new to coding junk in VBA for Microsoft Word. I have a table with 12 rows and I want to place a standard content control checkbox next to each row, and when any given checkbox is checked, the other rows disappear.
Currently I've had good luck at this with purely text, but trying to bookmark to hide an entire row of a table only seems to work for the very first checkbox. (Sorry if my code is more complicated than it needs to be. I also skipped pasting all of the code since the other 10 lines are the same, so the final 12 End Ifs are necessary):
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
If cc.Title = "impact" Then
If cc.Checked = True Then
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = True
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = True
ActiveDocument.Bookmarks("license").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank").Range.Font.Hidden = True
ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
Else: ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = False
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank").Range.Font.Hidden = False
End If
Exit Sub
Else: If cc.Title = "license" Then
If cc.Checked = True Then
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = True
ActiveDocument.Bookmarks("impact").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = True
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank3").Range.Font.Hidden = True
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = True
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
Else: ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = False
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank3").Range.Font.Hidden = False
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
Next
End Sub
Assuming that the content control Title is the same as the bookmark name you can try this simplified version of your code.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
If ActiveDocument.Bookmarks.Exists(cc.Title) Then
ActiveDocument.Bookmarks(cc.Title).Range.Font.Hidden = cc.Checked
End If
Next cc
End Sub
EDIT:
The issue you have with your original code is that it will only allow one row to be hidden.
To make your solution work you need to query the checked status of the corresponding content control for each bookmark. Your best option to achieve that is to ensure that the bookmark name matches either cc.Title or cc.Tag, otherwise you are back to complex and unwieldy code.
You actually don't need anything more complicated than:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
With CCtrl
If .Range.Information(wdWithInTable) = True Then
If .Checked = True Then
.Range.Tables(1).Range.Font.Hidden = True
.Range.Rows(1).Range.Font.Hidden = False
Else
.Range.Tables(1).Range.Font.Hidden = False
End If
End If
End With
End Sub
Looping through all the content controls is quite unnecessary. You don't even need any titles or bookmarks.
For i = 2 To 7
If SlideSeen(i) = False Then GoTo EndSlide
Next i
How do I make the logic in such a manner that I can make an If Condition reading multiple items of an array.
In this case: If SlideSeen(2) to SlideSeen(7) = false Then Goto EndSlide
EDIT: This code works:
Dim allTrues As Boolean
allTrues = True
For i = 2 To 7
If SlideSeen(i) = False Then allTrues = False
Next
If allTrues = True Then
'
Else
'
End If
One option is to check if any of those array elements are True... if not, all are False.
For i = 2 To 7
Dim anyTrues As Boolean
If slideSeen(i) Then anyTrues = True
Next
If Not anyTrues Then GoTo EndSlide
though I would caution against using GoTo here as that could be the start of spaghetti code.
I have a 2 scenarios that involves using My.Settings.sampleSettings with a dataType boolean and a sampleVariable as data type boolean.
Code: Since both sampleVariable and sampleSettings are boolean I declare them like that
Dim sampleVariable As Boolean = My.Settings.sampleSettings
Console.WriteLine("Result: " & sampleVariable)
If sampleVariable = False Then
Console.WriteLine("1")
sampleVariable = True
Else
Console.WriteLine("2")
sampleVariable = False
End If
My.Settings.Save()
Output: The output seems to not satisfy the condition 1, it always satisfies the condition 2 which is false
Result: False
1
Result: False
1
Result: False
1
Code: In this code I didn't put the sampleSettings to a boolean variable and it is working fine.
Console.WriteLine("Result: " & My.Settings.sampleSettings)
If My.Settings.sampleSettings = False Then
Console.WriteLine("1")
My.Settings.sampleSettings = True
Else
Console.WriteLine("2")
My.Settings.sampleSettings = False
End If
My.Settings.Save()
Output: Whenever I click the button it triggers a different condition, this is my goal.
Result: False
1
Result: True
2
Result: False
1
Question: How do I properly contain the My.Settings.sampleSettings to a boolean variable?
In the first block of code, you are not changing the value of the setting. You are only changing the value of the variable.
sampleVariable = True
This only changes the value of sampleVariable. It does not change the value of My.Settings.sampleSettings.
In the second block of code, you are changing the value of My.Settings.sampleSettings, which is why the value is being saved.
Hye there, I new with vba here.
I want to use checkboxes to link with the series collections of a chart. I put the check boxes in a sheet which contain the chart altogether. I have a lot of checkboxes here to be assigned to "true" value.
Private Sub Controls_Initialize()
'Make default for checkboxes
CheckBox1.Value = True
CheckBox2.Value = True
CheckBox3.Value = True
CheckBox4.Value = True
CheckBox5.Value = True
CheckBox6.Value = True
CheckBox7.Value = True
CheckBox8.Value = True
CheckBox9.Value = True
CheckBox10.Value = True
CheckBox11.Value = True
CheckBox12.Value = True
CheckBox13.Value = True
CheckBox14.Value = True
CheckBox15.Value = True
CheckBox16.Value = True
CheckBox17.Value = True
CheckBox18.Value = True
CheckBox19.Value = True
CheckBox20.Value = True
CheckBox21.Value = True
CheckBox22.Value = True
CheckBox23.Value = True
CheckBox24.Value = True
End Sub
I have tried this code but can't
For i = 1 to 24
Controls("CheckBox" & i).Value = True
Next i
The questions are
1. Is there any other code that can make it simple?
2. How to link the check boxes with the series collection in the activechart? Example, if the checkbox return value false, the series collection will be deleted/hide(perhaps?). And when it returns value true, the series collection of the same data will be added back in the chart. I would like to make the chart interactive.
If there is any reference that I can reviewed, do tell me.
Thanks in advance.
Regards.
Alright, so assuming from what you've given, I'd think the problem is that the interpreter doesn't know i is an integer.
To fix this, we can implement something along the lines of Dim i As Integer to implement i as an integer.
We could try this:
Dim i As Integer
For i = 1 to 24
Controls("CheckBox" & i).Value = True
Next i
I have code for a button that sets a boolean named ProjectCompleteOrgBtn.
I am trying to make this button set the boolean to true if you click it, then if you click it again after it makes it false, then again true, repeat.
The true and false sort the form below differently. Though at the moment everytime you click the button, it is always true. How do I fix this?
Private Sub ProjectCompleteOrgBtn_Click()
Dim ProjectCompeleteOrgB As Boolean
If ProjectCompleteOrgB = True Then
Set ProjectCompleteOrgB = False
Else
Set ProjectCompleteOrgB = True
If ProjectCompleteOrgB = False Then
MsgBox (False)
Forms!DatabaseF.ProjectQSubF.Form.RecordSource = "Select * from ProjectsQ ORDER BY ProjectComplete ASC"
Forms!DatabaseF.ProjectQSubF.Form.Refresh
Else
MsgBox (True)
Forms!DatabaseF.ProjectQSubF.Form.RecordSource = "Select * from ProjectsQ ORDER BY ProjectComplete DESC "
Forms!DatabaseF.ProjectQSubF.Form.Refresh
ProjectCompleteOrgB = False
End If
End Sub
First, you do
Dim ProjectCompeleteOrgB As Boolean
but notice it is spelled wrong (CompElete).
Then you write
If ProjectCompleteOrgB = False Then
but your code only changes that property or variable's value in the True block of your If. It doesn't get changed anywhere else.
Furthermore, you can make this:
If ProjectComplete = True Then
Set ProjectComplete = False
Else
Set ProjectComplete = True
much shorter by just writing
ProjectComplete = Not ProjectComplete