How to use wildcard in an Access VBA if then statement - vba

I have 22 items for a text box in the dropdown selection menu. 7 of them start with "Makeup - Answer1-7", 7 of them start with "Ad-Hoc Answer1-7" and the other 8 will be the else statement. The below code is what I have been using since it was the only answer to cause the Makeup_Date.visible = True. How can I do the same with multiple items 7 - Makeup & 7 - Ad-Hoc. I tried using a wildcard but it didn't work.
Example text box drop down items list
Makeup - Answer 1
Makeup - Answer 2...
Makeup - Answer 7
Ad-Hoc - Answer 1
Ad-Hoc- Answer 2...
Ad-Hoc- Answer 7
Other 8 Items...
Private Sub Week1_Attended_AfterUpdate()
Me.Refresh
If (Me.Week1_Attended = "YES - MAKEUP COMPLETED") Then
Me.Week1_Makeup_Date.Visible = True
Else
Me.Week1_Makeup_Date.Visible = False
End If
Me.Refresh
End Sub

Related

How to detect if the previous heading in the document is heading 1 or heading 7 through VBA

My templates contain Heading 1 to 5 for normal text and Appendix headings (based on heading 7, 8 and 9).
Chapter text
Appendix A Chapter text
The table and figure captions are different: based on Heading 1 or Appendix.
Table 1.1 or
Table A.1
Works fine. But ... for now I have two buttons in the ribbon to insert the table caption:
Insert table caption
Insert table caption appendix
Same for figure caption.
There must be a way to detect the previous main heading (heading 1 or heading 7 or outlinelevel 1 or 7), so I only need one button to insert a table (or figure) caption. But I just can't find it.
Does anyone have a clue? Thank you.
Helma
There is no specific property in Word that will return the previous level with outline level x. You will need to create a macro that loops backwards from the current paragraph until it finds outline level 1 or outline level 7.
Here are two examples of a function that return true if a preceding heading level was outline level 7 and false if it was outline level 1. The code compile without error and have no findings when inspected with the RubberDuck code inspector.
Option Explicit
'Recursive function
Public Function IsAppendixR(ByVal ipPara As Range) As Boolean
Select Case ipPara.Paragraphs.Item(1).OutlineLevel
Case 1
IsAppendixR = False
Case 7
IsAppendixR = True
Case Else
IsAppendixR = IsAppendixR(ipPara.Previous)
End Select
End Function
' Loop version
Public Function IsAppendixL(ByVal ipPara As Range) As Boolean
Do
Select Case ipPara.Paragraphs.Item(1).OutlineLevel
Case 1
IsAppendixL = False
Exit Function
Case 7
IsAppendixL = True
Exit Function
Case Else
ipPara.Previous
End Select
Loop
End Function
Word contains a hidden bookmark that you can use to get the range of the current heading level. By testing the outline level of the first paragraph in that range you can determine whether you are in an appendix.
Sub TestIsAppendix()
If IsAppendix(Selection.Range) Then MsgBox "In appendix"
End Sub
Public Function IsAppendix(ByVal para As Range) As Boolean
Dim headingBlock As Range
Set headingBlock = para.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
Select Case headingBlock.Paragraphs.Item(1).OutlineLevel
Case 7 To 9
IsAppendix = True
Case Else
IsAppendix = False
End Select
End Function

How to delete rows based on empty cell value in VBA [duplicate]

This question already has answers here:
Code in VBA loops and never ends. How to fix this?
(2 answers)
Closed 5 years ago.
I am trying to delete entire rows based on whether the cell value in the D column is NULL or not. My entire code so far is:
Sub DeleteNULL()
Dim i As Long
For i = 2 To 119713
If IsEmpty(Range("Di")) = True Then
Rows([i]).EntireRow.Delete
Else
If IsEmpty(Range("Di")) = False Then
Next i
End If
End Sub
I keep getting compile errors, either If without Else or Next without For, how should I fix this?
Thanks in advance.
A few things:
Placement of a lot of syntax is off.
When adding or deleting rows, you need to loop backwards based on how Excel handles these events.
See code below:
Sub DeleteNULL()
Dim i As Long
For i = 119713 To 2 Step -1
If IsEmpty(Range("D" & i)) Then Rows([i]).EntireRow.Delete
Next i
End Sub

Excel VBA how to use index when we have more if then else function

I have a VBA code with more then 50 lines of If Then ElseIf function. When I run macro, it will satisfy one "if" condition. If the data meet, 50th Elseif condition, then macro will go through unto 49 if condition. I know, one basic value that meet the condition. Since I am able to find value that meet condition, Can I directly jump into that particular if Condition? Any help would be much appreciated
If A=10 and B=39 then
Do Something....
Elseif A=11 and B=30 and C=56 then
Do Something....
Elseif A=13 and B=35 and C=60 then
Do Something....
...
etc
Here Since I know A=13, Can I directly jump to
Elseif A=13 and B=35 and C=60 then
without going through first two if condition?
To your question - can you prioritize the 49. ElseIf condition - NO.
But you can put it in number 1 or 2, if you want to do it faster in debugging.
Like this:
Public Sub TestMe()
Dim a As Long
a = 5
If a < 6 Then
Debug.Print "The most popular"
ElseIf a = 5 Then
Debug.Print "The 2. most popular"
ElseIf a > 2 Then
Debug.Print "The 3. most popular"
End If
End Sub
As mentioned in the comments, the fact that you would have about 50 small evaluations would not slow your code (unless there is a non optimized function in the evaluations or you are accessing a database there).

excel vba anomaly (for loop result) [duplicate]

This question already has an answer here:
Why is 134.605*100 not equal 13460.5 in VBA Access? [duplicate]
(1 answer)
Closed 8 years ago.
Sorry for not giving more detailed title, but it is because of this special case. My google search did not give me any similar topic.
The following simple code should give a series of numbers from 0.1 to 10 with step 0.1 (I hoped at least) in column A:
Cells(1, 1) = 0.1
For i = 2 To 100
Cells(i, 1) = Cells(i - 1, 1) + 0.1
Next i
Until 5.9 it works well, but after that the result is not as expected:
instead of 6 I get 5,99999999999999
instead of 6.1 I get 6,09999999999999
instead of 6.2 I get 6,29999999999999
...
Could anyone explain what is wrong with the code or why I get this result?
Thanks!
Or simply this?
Sub Sample()
Dim i As Long
For i = 1 To 100
'~~> Change Sheet1 to respective sheet
ThisWorkbook.Sheets("Sheet1").Cells(i, 1) = i * 0.1
Next i
End Sub
Or like this
Sub Sample()
'~~> Change Sheet1 to respective sheet
With ThisWorkbook.Sheets("Sheet1").Range("A1:A100")
.Formula = "=Row()*.1"
.Value = .Value
End With
End Sub

VBA display selection options

I am trying to write a code that will display a value depending on what checkbox is selected. There are a total of 5 checkboxes and I will be adding additional checkboxes in the future so I was wondering if there is an easy way to determine which checkboxes are checked to determine which values to display. I can do this in a really round about way but I would like minimize my code if possible.
In other words, if i write each scenario out I would have to write a separate code for all of the different selection possbilities:
1 only,2 only,3 only,4 only,5 only
1+2, 1+3, 1+4, 1+5, 2+3, 2+4, 2+5, 3+4, 3+5, 4+5
1+2+3, 1+2+4,1+2+5, 1+3+4,1+3+5, 1+4+5,2+3+4, 2+3+5,3+4+5
1+2+3+4, 1+2+3+5, 1+3+4+5, 2+3+4+5
1+2+3+4+5
Each value is associated with a sub that will fill the array if it is selected. And after the arrays are filled I need to perform an additional function on the ones that are selected. The function performed is the same but I do not want to perform the function if a value is not selected because it will defeat the purpose of my function otherwise. The function itself is to select duplicates from the arrays that were selected into another array.
You can use binary numbers for each checkbox:
First value is 1 (=20)
Second value is 2 (=21)
Third value is 4 (=22)
Fourth value is 8 (=22)
Fifth value is 16 (=24)
Hence, when you sum each permutation, you have a unique number you can check.
1 only is 0, 2 only is 1 and so on
1+2 is 3, 1+3 is 5, 1+4 is 9, 1+5 is 17
and so on
You can create arrays with every case you want to check.
At least, i hope this will give you some ideas or tips.
Regards,
What #JMax is referring to is more commonly known as bit-masking. Here's a quick tutorial:
Create a sample form named Form1 with 5 checkboxes named Check1, Check2, Check3, Check4, Check5. Then add the following two functions in a standard code module:
Function GetSelectedBoxes() As Long
Dim Ctl As Control, Total As Long
For Each Ctl In Forms!form1
If Ctl.ControlType = acCheckBox Then
If Ctl Then
Total = Total + 2 ^ CLng(Mid(Ctl.Name, 6))
End If
End If
Next Ctl
GetSelectedBoxes = Total
End Function
Sub ShowSelectedBoxes()
Dim Total As Long, i As Integer
Total = GetSelectedBoxes
For i = 1 To 5
If Total And 2 ^ i Then Debug.Print "Check" & i & " selected"
Next i
End Sub
Now open Form1 and check boxes 1, 3, and 4. Run the ShowSelectedBoxes routine and you should get the following output in the immediate window:
Check1 selected
Check3 selected
Check4 selected
I used the For...Loop for compactness in my sample, but you can just as easily break it out into separate If...Then statements to do something more meaningful with the checked boxes.
This approach will support up to 31 or 32 separate checkboxes.