Could someone please point me in the right direction?
View the following code, instead of repetitive lines, I want to loop this to save time...
'show/hide SN# DEFECTIVE
If CheckBox2.Value = True Then Columns("f").EntireColumn.Hidden = False
If CheckBox2.Value = False Then Columns("f").EntireColumn.Hidden = True
'show/hide SN# INSTALLED
If CheckBox3.Value = True Then Columns("g").EntireColumn.Hidden = False
If CheckBox3.Value = False Then Columns("g").EntireColumn.Hidden = True
'show/hide SN# DESCRIPTION
If CheckBox4.Value = True Then Columns("h").EntireColumn.Hidden = False
If CheckBox4.Value = False Then Columns("h").EntireColumn.Hidden = True
'show/hide SN# DESCRIPTION
If CheckBox4.Value = True Then Columns("h").EntireColumn.Hidden = False
If CheckBox4.Value = False Then Columns("h").EntireColumn.Hidden = True
I have the following, which works great...
For i = 2 To 4
chk = Me.Controls("checkbox" & i).Value
MsgBox "checkbox " & i & " " & chk
If chk = False Then
Columns("f").EntireColumn.Hidden = True
Else
Columns("f").EntireColumn.Hidden = False
End If
Next i
However, I also want to index the columns as well, but these are identified as "letters", so how can I also increment an "f" to the next letter "g", and so on...
I wouldn't use loop for this unless you have many more columns to change visibility.
However, what I would do is definitely avoid Ifs and simply use the associate checkboxes' value's opposite to be set as the hidden property value like below. More readable, maintainable and of course shorter.
Columns("f").EntireColumn.Hidden = Not CheckBox2.Value
Columns("g").EntireColumn.Hidden = Not CheckBox3.Value
Columns("h").EntireColumn.Hidden = Not CheckBox4.Value
EDIT: I don't recommend this but to provide you an answer for your question in your way: use Chr function to return letters.
For i = 2 To 4
chk = Me.Controls("checkbox" & i).Value
MsgBox "checkbox " & i & " " & chk
If chk = False Then
Columns(chr(100+i)).EntireColumn.Hidden = True
Else
Columns(chr(100+i)).EntireColumn.Hidden = False
End If
Next i
even shorter using my way:
For i = 2 To 4
Columns(chr(100+i)).EntireColumn.Hidden = Not Me.Controls("checkbox" & i).Value
Next i
Related
Code that searches for duplicates works in test mode, but doesn't when I run it normally.
I have a subroutine called FindDuplicatesInColumn, placed in the section that Validates data entered into a userform.
' Check if all data has been entered on the userform
Function Data_Validation() As Boolean 'Declare Function with Bool as data type
'Default True. False if any conditions met. When a function is called, a new variable,
'with the function name and datatype given is created. You'll set the value in the
'function. When the function ends either in Exit Function or
'End Function, whatever is contained in this variable is returned as the Functions result
Data_Validation = True
' Check if all data has been entered on the userform
If ARLArea = False And KNBArea = False And LSQArea = False And RSQArea = False And RevenueControlInspectors = False And SpecialRequirementTeam = False Then
MsgBox "Select Area!", vbInformation, ("Area")
ARLArea.SetFocus
Data_Validation = False
Exit Function
End If
If EmployeeNo1 = "" Then
MsgBox "Enter Employee Number!", vbInformation, ("Employee Number")
EmployeeNo1.SetFocus
Data_Validation = False
Exit Function
End If
If FirstName1 = "" Then
MsgBox "Enter First Name!", vbInformation, ("First Name")
FirstName1.SetFocus
Data_Validation = False
Exit Function
End If
If LastName1 = "" Then
MsgBox "Enter Last Name!", vbInformation, ("Last Name")
LastName1.SetFocus
Data_Validation = False
Exit Function
End If
If CSA2 = False And CSA1 = False And CSS2 = False And CSS1 = False And CSM2 = False And CSM1 = False And AM = False And RCI = False And SRT = False Then
MsgBox "Select Grade!", vbInformation, ("Grade")
CSA2.SetFocus
Data_Validation = False
Exit Function
End If
BlnVal = 1
FindDuplicatesInColumn
End Function
The Subroutine I created
Sub FindDuplicatesInColumn()
Dim sAccNum As String
Dim rAccLst As Range
Dim res
Dim IdVal As Integer
Set rAccLst = Sheets("Data Input").Range("B5:B" & Range("B" & Rows.Count).End(xlUp).Row)
sAccNum = EmployeeNo1
If Not rAccLst.Find(What:=sAccNum, LookIn:=xlValues, Lookat:=xlPart) Is Nothing Then
MsgBox "Sorry, This person already exists in the Database!"
'Empty Area
ARLArea = False
LSQArea = False
KNBArea = False
RSQArea = False
RevenueControlInspectors = False
SpecialRequirementTeam = False
'Empty EmployeeNo1
EmployeeNo1.Value = ""
'Empty FirstName1
FirstName1.Value = ""
'Empty LastName1
LastName1.Value = ""
'Empty Grade
CSA2 = False
CSA1 = False
CSS2 = False
CSS1 = False
CSM2 = False
CSM1 = False
AM = False
RCI = False
SRT = False
sAccNum = ""
If Data_Validation() = False Then
Exit Sub
End If
Else
'Sheets("Data Input").Range("B" & Range("B" & Rows.Count).End(xlUp).Row).Offset(1, 0).Value = "ACC" & sAccNum
End If
End Sub
I am trying to check when the user enters the EmployeeNo1 in the userform. It is checked against the data in Column B of Data_Input to see if it is duplicated. If it is, then to stop the data from the form being added to the database.
Set rAccLst = Sheets("Data Input").Range("B5:B" & Range("B" & Rows.Count).End(xlUp).Row)
should be
With Sheets("Data Input")
Set rAccLst = .Range("B5:B" & .Range("B" & .Rows.Count).End(xlUp).Row)
End with
...otherwise that second Range() defaults to the active sheet and not necessarily to the sheet you want.
My VBA code is below. I have a dropdown box with 9 selectable options. Each option hides or unhides specific columns. My current code works well to hide what isn't needed. However, when I select a different drop down option, it will not reset to show only what I need to see. Essentially only building off the previous selection.
`Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = ("$F$1") Then
'LID
If Target.Text = "LID" Then
Columns("G").EntireColumn.Hidden = True
Columns("I:O").EntireColumn.Hidden = True
Columns("Q").EntireColumn.Hidden = True
ElseIf Target.Text = "RESET" Then
Columns("A:V").EntireColumn.Hidden = False
End If
'Whole - HARD FIXED
If Target.Text = "Whole -Hard Fixed" Then
Columns("G").EntireColumn.Hidden = True
Columns("I").EntireColumn.Hidden = True
Columns("K:N").EntireColumn.Hidden = True
Columns("Q").EntireColumn.Hidden = True
ElseIf Target.Text = "RESET" Then
Columns("A:V").EntireColumn.Hidden = False
End If
'Whole - FIXED W TRIGGER
If Target.Text = "Whole -Fixed w Trigger" Then
Columns("G").EntireColumn.Hidden = True
Columns("I").EntireColumn.Hidden = True
Columns("J").EntireColumn.Hidden = True
Columns("L:N").EntireColumn.Hidden = True
Columns("Q").EntireColumn.Hidden = True
ElseIf Target.Text = "RESET" Then
Columns("A:V").EntireColumn.Hidden = False
End If
'Whole - HIGH/LOW
If Target.Text = "Whole -High/Low" Then
Columns("G").EntireColumn.Hidden = True
Columns("I").EntireColumn.Hidden = True
Columns("J").EntireColumn.Hidden = True
Columns("K").EntireColumn.Hidden = True
Columns("Q").EntireColumn.Hidden = True
ElseIf Target.Text = "RESET" Then
Columns("A:V").EntireColumn.Hidden = False
End If
'VALUE ADDED
If Target.Text = "Value Added" Then
Columns("G").EntireColumn.Hidden = True
Columns("I").EntireColumn.Hidden = True
Columns("J").EntireColumn.Hidden = True
Columns("L:N").EntireColumn.Hidden = True
Columns("Q").EntireColumn.Hidden = True
ElseIf Target.Text = "RESET" Then
Columns("A:V").EntireColumn.Hidden = False
End If
'FIXED LANDED
If Target.Text = "Fixed Landed" Then
Columns("G").EntireColumn.Hidden = True
Columns("I").EntireColumn.Hidden = True
Columns("J:P").EntireColumn.Hidden = True
ElseIf Target.Text = "RESET" Then
Columns("A:V").EntireColumn.Hidden = False
End If
'FIXED QUARTERLY
If Target.Text = "Fixed Quarterly" Then
Columns("G").EntireColumn.Hidden = True
Columns("I").EntireColumn.Hidden = True
Columns("J").EntireColumn.Hidden = True
Columns("L:N").EntireColumn.Hidden = True
Columns("Q").EntireColumn.Hidden = True
ElseIf Target.Text = "RESET" Then
Columns("A:V").EntireColumn.Hidden = False
End If
'HIGH/LOW QUARTERLY
If Target.Text = "H/L Quarterly" Then
Columns("G").EntireColumn.Hidden = True
Columns("I:K").EntireColumn.Hidden = True
Columns("Q").EntireColumn.Hidden = True
ElseIf Target.Text = "RESET" Then
Columns("A:V").EntireColumn.Hidden = False
End If
'FIXED MONTHLY
If Target.Text = "Fixed Monthly" Then
Columns("G").EntireColumn.Hidden = True
Columns("I:J").EntireColumn.Hidden = True
Columns("L:N").EntireColumn.Hidden = True
Columns("Q").EntireColumn.Hidden = True
ElseIf Target.Text = "RESET" Then
Columns("A:V").EntireColumn.Hidden = False
End If
End If
End Sub
Folding in the excellent "unhide all columns first" advice:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As String
If Target.Address = ("$F$1") Then
Me.Columns("A:V").EntireColumn.Hidden = False
Select Case Target.Text
Case "LID": rng = "G1,I1:O1,Q1"
Case "Whole -Hard Fixed": rng = "G1,I1,K1:N1,Q1"
Case "Whole -Fixed w Trigger": rng = "G1,I1,J1,L1:N1,Q1"
Case "Whole -High/Low": rng = "G1,I1,J1,K1,Q1"
'etc etc
Case Else: rng = ""
End Select
If Len(rng) > 0 Then Me.Range(rng).EntireColumn.Hidden = True
End If 'F1
End Sub
Whenever I run this code, it generates a sequential number.
I want to display the new sequential number in a MsgBox, but it prints the older sequential number.
Private Sub ToggleButton1_Click()
Dim reponse As VbMsgBoxResult
Dim REVISIONRNCAUTO As Workbook
Dim Sheet2 As Worksheet
Dim cell_value As String
Set REVISIONRNCAUTO = ActiveWorkbook
Set Sheet2 = REVISIONCRNAUTO.Worksheets(2)
cell_value = Sheet2.Cells(4, "A").Value & Sheet2.Cells(4, "B").Value
If CheckBox1.Value = True And CheckBox4.Value = True And CheckBox7.Value = True And CheckBox2.Value = False And CheckBox3.Value = False _
And CheckBox6.Value = False And CheckBox5.Value = False And CheckBox8.Value = False And CheckBox9.Value = False And CheckBox10.Value = False And CheckBox11.Value = False And CheckBox12.Value = False _
And CheckBox13.Value = False And CheckBox14.Value = False And CheckBox15.Value = False Then
Sheet2.Activate
reponse = MsgBox("Êtes-vous sûr de vouloir générer ce RNC?", vbYesNo + vbQuestion, "Enregistrement RNC")
If reponse = vbYes Then
Sheets("Sheet2").Range("B4").Select
ActiveCell.EntireRow.Insert shift:=xlDown
Sheets("Sheet2").Range("B4:E4").Select
Selection.Borders.Weight = xlThin
Sheets("Sheet2").Range("B4").Select
ActiveCell.Value = "=b5+1"
Sheets("Sheet2").Range("A4").Select
Selection.Borders.Weight = xlThin
ActiveCell.Value = "E"
Else
Exit Sub
End If
End If
MsgBox ("Le nouveau RNC enregistré est le : " & cell_value)
You aren't changing the value of cell_value after you set it.
They are not linked forever like an Excel formula. You have to set it again once you change the cells that it is based on.
Put the cell_value = line right before the Else in addition to where it currently is.
I would love to optimize this code so that it executes faster...
Sub gotocfstatement()
If ActiveCell.Row < 10 Then Exit Sub
If ActiveCell.Row > 5001 Then Exit Sub
If Sheet1.Range("p" & (ActiveCell.Row)).Value = "" Then Exit Sub
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Filename = Sheet1.Range("p" & (ActiveCell.Row)).Value
Sheet2.Range("b9").Value = Filename
If Sheet2.Range("b15").Value = "" Then
Sheet2.Range("b14").Value = Sheet2.Range("s1").Value
Else
Sheet2.Range("b14").Value = Sheet2.Range("b15").Value
End If
If Sheet2.Range("a81").Value = "" Then
Sheet2.Range("a85").Value = Sheet2.Range("ab1").Value
Else
Sheet2.Range("a85").Value = Sheet2.Range("a81").Value
End If
Sheets("cash flow statement").Select
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
I need to insert the result of my userform into a header but i don't know how to combine my codes into a final project : Photos and code below
i need the header OK Button to
1: Format the header according to my header code depending on what sheet i want, in this case the sheet called metals
2. After it says "Summary of Metals in "_____" <-(Soil/Sediment...etc depending on which box is checked)
3. Insert what ever text is entered into the userform text box. (no code written yet).
The final result. = For this particular sheet would be the header saying "Summary of Metals in Soil, 100 Main Street, USA"
All help is appreciated!
The code below inserts the result into A1 just temporary
Private Sub Cancel_Click()
Me.Hide
End Sub
Private Sub OK_Click()
'--- Insert the correct matrix Wording ---
If Check_Soil.Value = -1 Then
Range("A1").Value = "Soil"
ElseIf Check_Sediment.Value = -1 Then
Range("A1").Value = "Sediment"
ElseIf Check_Ground_Water.Value = -1 Then
Range("A1").Value = "Ground Water"
ElseIf Check_Surface_Water.Value = -1 Then
Range("A1").Value = "Surface Water"
End If
Me.Hide
MsgBox "Completed", vbOKOnly
End Sub
Private Sub Check_Soil_Click()
'--- Checks if the Soil Button is Clicked ---
If Check_Soil.Value = True Then
Check_Surface_Water.Value = False
Check_Ground_Water.Value = False
Check_Sediment.Value = False
Else
Check_Soil.Enabled = True
End If
End Sub
Private Sub Check_Surface_Water_Click()
'--- Checks if the Surface Water Button is Clicked ---
If Check_Surface_Water.Value = True Then
Check_Soil.Value = False
Check_Ground_Water.Value = False
Check_Sediment.Value = False
Else
Check_Surface_Water.Enabled = True
End If
End Sub
Private Sub Check_Ground_Water_Click()
'--- Checks if the Ground Water Button is Clicked ---
If Check_Ground_Water.Value = True Then
Check_Surface_Water.Value = False
Check_Soil.Value = False
Check_Sediment.Value = False
Else
Check_Ground_Water.Enabled = True
End If
End Sub
Private Sub Check_Sediment_Click()
'--- Checks if the Sediment Button is Clicked ---
If Check_Sediment.Value = True Then
Check_Surface_Water.Value = False
Check_Soil.Value = False
Check_Ground_Water.Value = False
Else
Check_Sediment.Enabled = True
End If
End Sub
My OTHER CODE:
SubSelect_Correct_Sheet()
' Select_Correct_Sheet Macro
Sheets("Metals").Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = "&""Arial,Bold""Summary of Metals in "
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
End Sub
Try these two changes
1- change your OK_Click into this:
Private Sub OK_Click()
Dim headerText As String
Select Case True
Case Check_Soil.value: headerText = "Soil"
Case Check_Sediment.value: headerText = "Sediment"
Case Check_Ground_Water.value: headerText = "Ground Water"
Case Check_Surface_Water.value: headerText = "Surface Water"
End Select
headerText = headerText & ", " & TextBox1.value ' <-- assuming this is the name of your textbox
FormatHeader headerText ' <-- now invoke the header formatting sub with parameter
MsgBox "Completed"
End Sub
2- Change your routine of formatting the header (old name was Select_Correct_Sheet I gave it a new name, FormatHeader). I should have a parameter text in its declaration and only one line will change, the one where the text is assigned in order to add the provided parameter.
Sub FormatHeader(text As String)
' ....
.LeftHeader = "&""Arial,Bold""Summary of Metals in " & text '<-- add the text parameter into header here
' ....
End Sub