Hi guys I need help on how to use checkbox array. I have 4 checkboxes:
check1(0)
check1(1)
check1(2)
check1(3)
If I check one checkbox the other 3 will disable. And when I uncheck the checkbox the four will enable.
Private Sub Check1_Click(Index As Integer)
Dim i As Long, ChkCount As Long
ChkCount = 0
For i = 0 To 3
If Check1(i).Value = 1 Then ChkCount = ChkCount + 1
Next i
For i = 1 To 3
If ChkCount < 1 Then
Check1(i).Enabled = True
Else
If Check1(i).Value = 0 Then Check1(i).Enabled = False
End If
Next i
End Sub
Here's my code but the check1(0) doesn't disable.
I fixed it guys thanks for response.
got an error in line 6 it should be for i = 0 to 3
This is an working demo, I just tested in my VB6.
Option Explicit
Private Sub Check1_Click(Index As Integer)
Dim i As Long
Dim isDisable As Boolean
isDisable = Not (Check1(Index).Value = 1)
For i = 0 To Check1.Count - 1
If i <> Index Then
Check1(i).Enabled = isDisable
End If
Next i
End Sub
Private Sub check1_Click(Index As Integer)
Select Case Index
Case 0
'Checks or unchecks the first checkbox and gives the other checkboxes the opposite values
check(0).Checked = Not check(0).Checked
check(1).Checked = Not check(0).Checked
check(2).Checked = Not check(0).Checked
check(3).Checked = Not check(0).Checked
Case 1
'Checks or unchecks the second checkbox and gives the other checkboxes the opposite values
check(1).Checked = Not check(1).Checked
check(0).Checked = Not check(1).Checked
check(2).Checked = Not check(1).Checked
check(3).Checked = Not check(1).Checked
Case 2
'Checks or unchecks the third checkbox and gives the other checkboxes the opposite values
check(2).Checked = Not check(2).Checked
check(0).Checked = Not check(2).Checked
check(1).Checked = Not check(2).Checked
check(3).Checked = Not check(2).Checked
Case 3
'Checks or unchecks the fourth checkbox and gives the other checkboxes the opposite values
check(3).Checked = Not check(3).Checked
check(0).Checked = Not check(3).Checked
check(1).Checked = Not check(3).Checked
check(2).Checked = Not check(3).Checked
End Select
End Sub
Related
Listview1,Listview2 compare both parent and child items.
the items are named some-name.zip a-z and child files are loaded in each parent.
Listview1 is Target
Listview2 is my loaded files i want to compare with Listview1
image = 4 'green icon
image = 3 ' Red icon
Compare 2 listview and if it matches then listview2 image = 4 else image = 3
if child matches then image = 4 else image = 3
startt = True
Dim FoundIt As Boolean, ii As Integer, ix As Integer
Dim NodX As Node, NodX2 As Node
For Each NodX In TreeView1.Nodes
ix = NodX.Index
For Each NodX2 In TreeView2.Nodes
ii = NodX2.Index
If NodX.FullPath = NodX2.FullPath Then
FoundIt = True
On Error Resume Next
Exit For
End If
DoEvents
'pause 0
If TreeView2.Nodes(ii).Index = TreeView2.Nodes.Count - 0 Then
Exit For
Exit Sub
End If
Next
If FoundIt Then
TreeView1.Nodes(ix).Image = 4
TreeView2.Nodes(ii).Image = 4
Else
TreeView2.Nodes(ix).Image = 3
End If
If TreeView2.Nodes(ii).Index = TreeView2.Nodes.Count - 0 Then
DoEvents
'Call Command16_Click
If downnn = True Then
Exit For
Exit Sub
End If
End If
FoundIt = False
Next
End Sub
Here's a Sub that takes in two TreeView controls and compares their contents. If a node in the Source TreeView doesn't have a corresponding node in the Target TreeView, its Image gets set to 3. Otherwise, Image is set to 4:
Private Sub CompareTreeViews(ByRef p_objSourceTreeView As TreeView, ByRef p_objTargetTreeView As TreeView)
Dim objSourceNode As Node
Dim objTargetNode As Node
Dim objMatchNode As Node
Dim objSourceChildNode As Node
Dim objTargetChildNode As Node
Dim iSourceCounter As Integer
Dim iTargetCounter As Integer
Dim fFound As Boolean
Dim fChildrenMatch As Boolean
On Error Resume Next
For Each objSourceNode In p_objSourceTreeView.Nodes
' Reset ChildrenMatch flag, used to track if all children match
fChildrenMatch = True
' Find matching node in Source TreeView
For Each objTargetNode In p_objTargetTreeView.Nodes
If objTargetNode.Text = objSourceNode.Text Then
' Match found
Set objMatchNode = objTargetNode
Exit For
End If
Next
If Not objMatchNode Is Nothing Then
' Check all children
If objSourceNode.Children > 0 Then
' Get first Child and Loop through all Children
Set objSourceChildNode = objSourceNode.Child
For iSourceCounter = 1 To objSourceNode.Children
' Check if it exists in Target Treeview
If objMatchNode.Children > 0 Then
' Set Found flag to False
fFound = False
' Get first Child and Loop through all Children
Set objTargetChildNode = objMatchNode.Child
For iTargetCounter = 1 To objMatchNode.Children
' Check for match
If objTargetChildNode.Text = objSourceChildNode.Text Then
fFound = True
Exit For
End If
' Get next node
Set objTargetChildNode = objTargetChildNode.Next
Next
' Mark Node
Select Case fFound
Case True
objSourceChildNode.Image = 4
Case False
objSourceChildNode.Image = 3
fChildrenMatch = False
End Select
' Get next node
Set objSourceChildNode = objSourceChildNode.Next
End If
DoEvents
Next ' Source Child
End If
End If
Select Case fChildrenMatch
Case True
objSourceNode.Image = 4
Case False
objSourceNode.Image = 3
End Select
DoEvents
Next ' Source Node
End Sub
Based on your question, you want to call the Sub this way:
Label1.Caption = "Comparing..."
CompareTreeViews TreeView2, TreeView1
Label1.Caption = "Done!"
I would appreciate if anybody can help me with this issue I am having. Basically, the VBA is a search function that enables the user to search part of or the entire name of the job, from a job database.
However, it results in "Runtime error 7: Out of Memory." This happens only on my Macbook, and does not happen on a Windows computer. Upon clicking "debug", it brought me to this line of code:
`If scd.Cells(i, j) Like "*" & Search & "*" Then
please help! Thank you!
The rest of the code is below:
Option Compare Text
Sub SearchClientRecord()
Dim Search As String
Dim Finalrow As Integer
Dim SearchFinalRow As Integer
Dim i As Integer
Dim scs As Worksheet
Dim scd As Worksheet
Set scs = Sheets("Client Search")
Set scd = Sheets("Client Database")
scs.Range("C19:S1018").ClearContents
Search = scs.Range("C12")
Finalrow = scd.Range("D100000").End(xlUp).Row
SearchFinalRow = scs.Range("D100000").End(xlUp).Row
For j = 3 To 19
For i = 19 To Finalrow
If scd.Cells(i, j) Like "*" & Search & "*" Then
scd.Range(scd.Cells(i, 3), scd.Cells(i, 19)).Copy
scs.Range("C100000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
End If
Next i
Next j
scs.Range("C19:S1018").Select
scs.Range("$C$18:$S$1009").RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6 _
, 7), Header:=xlYes
Call Border
Columns("C:S").HorizontalAlignment = xlCenter
End Sub
I created an alternate function called "aLike" below.
In your code you would use it by saying: If aLike("*" & Search & "*",scd.Cells(i, j)) Then
I can't guarantee it works exactly the same way, but I would be interested to see if the Mac can process this function better than "like".
Function aLike(asterixString As Variant, matchString As Variant, Optional MatchCaseBoolean As Boolean) As Boolean
Dim aStr As Variant, mStr As Variant, aStrList As New Collection
Dim i As Long, aPart As Variant, mPart As Variant, TempInt As Long, aStart As Boolean, aEnd As Boolean
aStr = asterixString: mStr = matchString
If Not MatchCaseBoolean Then aStr = StrConv(aStr, vbLowerCase): mStr = StrConv(mStr, vbLowerCase)
' Get rid of excess asterix's
While InStr(aStr, "**") > 0
aStr = Replace(aStr, "**", "*")
Wend
' Deal with trivial case
If aStr = mStr Then aLike = True: GoTo EndFunction
If aStr = "*" Then aLike = True: GoTo EndFunction
If Len(aStr) = 0 Then aLike = False: GoTo EndFunction
' Convert to list
aStart = Left(aStr, 1) = "*": If aStart Then aStr = Right(aStr, Len(aStr) - 1)
aEnd = Right(aStr, 1) = "*": If aEnd Then aStr = Left(aStr, Len(aStr) - 1)
aLike_Parts aStr, aStrList
' Check beginning
If Not aStart Then
aPart = aStrList.Item(1)
If Not (aPart = Left(mStr, Len(aPart))) Then aLike = False: GoTo EndFunction
End If
' Check end
If Not aEnd Then
aPart = aStrList.Item(aStrList.Count)
If Not (aPart = Right(mStr, Len(aPart))) Then aLike = False: GoTo EndFunction
End If
' Check parts
mPart = mStr
For i = 1 To aStrList.Count
aPart = aStrList.Item(i): TempInt = InStr(mPart, aPart)
If TempInt = 0 Then aLike = False: GoTo EndFunction
mPart = Right(mPart, Len(mPart) - TempInt - Len(aPart) + 1)
If Len(mPart) = 0 And i < aStrList.Count Then aLike = False: GoTo EndFunction
Next i
aLike = True
EndFunction:
Set aStrList = Nothing
End Function
Function aLike_Parts(Str As Variant, StrList As Collection) As Variant
Dim Char As String, wPart As String
For i = 1 To Len(Str)
Char = Mid(Str, i, 1)
If Char = "*" Then
StrList.Add wPart: wPart = ""
Else
wPart = wPart & Char
End If
Next i
If Len(wPart) > 0 Then StrList.Add wPart
End Function
Good Luck!
#Alex P , now .find is NOT more efficient, for example :
Option Explicit
Option Compare Text
Sub SearchClientRecord()
With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
Dim Search As String
Dim Finalrow As Long
Dim SearchFinalRow As Long
Dim i&, j&
Dim scs As Worksheet
Dim scd As Worksheet
Dim DATA() As Variant
Dim Range_to_Copy As Range
Set scs = Sheets("Client Search")
Set scd = Sheets("Client Database")
With scd
Finalrow = .Range("D100000").End(xlUp).Row
DATA = .Range(.Cells(19, 3), .Cells(Finalrow, 19)).Value2
End With
With scs
.Range("C19:S1018").ClearContents
Search = .Range("C12").Value
SearchFinalRow = .Range("D100000").End(xlUp).Row
End With
With scd
For j = 3 To 19
For i = 19 To Finalrow
If InStr(DATA(i, j), Search) > 0 Then
'If scd.Cells(i, j) Like "*" & Search & "*" Then
If Not Range_to_Copy Is Nothing Then
Set Range_to_Copy = Union(Range_to_Copy, .Range(.Cells(i, 3), .Cells(i, 19)))
'scd.Range(scd.Cells(i, 3), scd.Cells(i, 19)).Copy
'scs.Range("C100000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
Else
Set Range_to_Copy = .Range(.Cells(i, 3), .Cells(i, 19))
End If
End If
Next i
Next j
End With 'scd
Erase DATA
With scs
Range_to_Copy.Copy _
Destination:=.Range("C100000").End(xlUp).Offset(1, 0) '.PasteSpecial xlPasteFormulasAndNumberFormats
.Range("C19:S1018").Select 'this line might be superflous
.Range("$C$18:$S$1009").RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7), Header:=xlYes
End With
Call Border
Columns("C:S").HorizontalAlignment = xlCenter 'on wich worksheet ??
Set Range_to_Copy = Nothing
Set scs = Nothing
Set scd = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
I have an auto-filtered range of data. The auto filter was created by the following VB code:
Sub Colour_filter()
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.AutoFilter
End Sub
I would like to sort the values in column "A" (the data actually start from cell "A4") by the following colour ( Color = RGB(255, 102, 204) ) so all the cells with that colour sort to the top.
It would be fab if the extra code could be added to my existing code?
My office is really noisy and my VB isn’t the best. It is doubly hard with laughing, chatting ladies all about. Any help will be stress relief heaven!! (p.s. no poke at the ladies it’s just my office is 95% women).
Edited per request by #ScottHoltzman.
My requested code forms part of a larger code which would confuse matters, although here is a slimmed down version of the aspect I currently need.
Sub Colour_filter()
' Following code( using conditional formatting) adds highlight to 'excluded' courses based
'on 'course code' cell value matching criteria. Courses codes matching criteria are highlighted
'in 'Pink'; as of 19-Nov-2012 the 'excluded' course codes are
'(BIGTEST, BIGFATCAT).
' <====== CONDITIONAL FORMATTING CODE STARTS HERE =======>
Columns("A:A").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""BIGTEST"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Color = 13395711
End With
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""BIGFATCAT"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Color = 13395711
End With
' <====== CONDITIONAL FORMATTING CODE ENDS HERE =======>
' Following code returns column A:A to Font "Tahoma", Size "8"
Columns("A:A").Select
With Selection.Font
.Name = "Tahoma"
.FontStyle = "Regular"
.Size = 8
.ThemeColor = xlThemeColorLight1
.ThemeFont = xlThemeFontNone
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = False
End With
' Following code adds border around all contiguous cells ion range, similar to using keyboard short cut "Ctrl + A".
Range("A4").Select
ActiveCell.CurrentRegion.Select
With Selection
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
End With
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
' Following code adds 'Blue' cell colour to all headers in Row 4 start in Cell "A4".
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Selection.Font.Bold = True
'<== adds auto-filter to my range of cells ===>
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.AutoFilter
End Sub
Well here is a small Sub that does the following sorting as per shown image. Most of the values like dimensions/range sizes are very static since this is a sample. You may improve it to be dynamic. Please comment if this code is going in the right direction so I can update with the final sort.
EDITTED CODE WITH DOUBLE SORT KYES
code:
Option Explicit
Sub sortByColor()
Dim rng As Range
Dim i As Integer
Dim inputArray As Variant, colourSortID As Variant
Dim colourIndex As Long
Set rng = Sheets(1).Range("D2:D13")
colourIndex = Sheets(1).Range("G2").Interior.colorIndex
ReDim inputArray(1 To 12)
ReDim colourSortID(1 To 12)
For i = 1 To 12
inputArray(i) = rng.Cells(i, 1).Interior.colorIndex
If inputArray(i) = colourIndex Then
colourSortID(i) = 1
Else
colourSortID(i) = 0
End If
Next i
'--output the array with colourIndexvalues and sorting key values
Sheets(1).Range("E2").Resize(UBound(inputArray) + 1) = _
Application.Transpose(inputArray)
Sheets(1).Range("F2").Resize(UBound(colourSortID) + 1) = _
Application.Transpose(colourSortID)
'-sort the rows based on the interior colour
Application.DisplayAlerts = False
Set rng = rng.Resize(, 3)
rng.Sort Key1:=Range("F2"), Order1:=xlDescending, _
Key2:=Range("E2"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Application.DisplayAlerts = True
End Sub
output:
I am having a problem while searching for an item in datagridview
here is my code but whenever i search for an item which already exist in the database, it is telling not found
If txtfirstname.Text = "" Then
MsgBox("Please enter first name!")
Else
Dim totalrow As Integer = DataGridView1.RowCount - 2
Dim rowin As Integer
Dim flag As Boolean = False
Dim sear As String = CStr(txtfirstname.Text)
For rowin = 0 To totalrow
Dim id As String = DataGridView1.Item(0, rowin).Value
If sear = id Then
DataGridView1.ClearSelection()
DataGridView1.Rows(rowin).Selected = True
DataGridView1.CurrentCell = DataGridView1.Item(0, rowin)
flag = True
Exit Sub
Else
flag = False
End If
Next rowin
If flag = False Then
MessageBox.Show("Firstname " & txtfirstname.Text & " is not found in database.", "Search Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
By setting
Dim totalrow As Integer = DataGridView1.RowCount - 2
you are always missing the last record in your dataset.
Try
Dim totalrow As Integer = DataGridView1.RowCount - 1
to set the upper bound value of your For loop.
Below code is working for list view, but i want to use listbox instead of list view
lst = listview from the below code
Dim idx as integer
idx = 1
lst.ListItems.Clear
If Emp.Employees.RecordCount > 0 Then
Emp.Employees.MoveFirst
While Not Employees.EOF
lst.ListItems.Add idx, , EmployeeID
lst.ListItems(idx).ListSubItems.Add , , FirstName
If IsAssigned(EmployeeID, CurrentSchedule) Then
lst.ListItems(idx).Checked = True
Else
lst.ListItems(idx).Checked = False
End If
idx = idx + 1
Employees.MoveNext
Wend
End If
Listbox name is lstbox
I tried
lstbox.selected(I) = true is not working instead of lst.ListItems(idx).Checked = True
with aListbox
.Clear
'//loop here
.additem "The Item Text"
'//add the numeric id value
.itemdata(.NewIndex) = 112233
'//check it
if (condition) then
.Selected(.NewIndex) = True
end if
end with
'//sample click
msgbox aListbox.list(aListbox.listindex) & " id=" & aListbox.itemdata(aListbox.listindex)