Selecting and deselecting specific boxes - excel

I'm trying to create a catalog containing parts from different brands and machines. As I previously asked, I tried to loop through the catalog to use diferent worksheets and not comprise everything into one, but every useful tip that was kindly given to me was, quite unsuccesfully, applied by me.
The way the code is written is to pull information from a database based on a few selection boxes on a customizing menu, as in the picture attached. The selection boxes are set to change from "selected" to "deselected" from a font when clicked on.
However, the database is going to contain too many machines to search on the list by name, and I'm trying to figure out a way to deselect all of the other boxes when a specific one is selected.
For example, if the selection box for "CATERPILLAR" is selected, I want the macro to automatically deselect all of the other boxes that aren't linked in the database, or in the catalog, every machine that isn't from the brand "Caterpillar" by changing the "empty box" character into the "checked box" character.
The font used was "Wingdings" and the empty box is character 168, while the checked box is 254.
Is there any way I can do that? Maybe if I create another variable other than "LIST", "TEXT" or "NUMBER" to show only the ones under the category "LIST" related? Because "Modelo" and "Marca" are both under "LIST". Been fiddling with this one for a while! Code of the customizing part follows:
Dim CustRow As Long, ProdCol As Long, ProdRow As Long, LastProdRow As Long, LastResultRow As Long
Dim DataType As String
Dim ItemShp As Shape
Sub Customize_Open()
With Sheet1
'limpar grupos menos amostra
For Each ItemShp In .Shapes
On Error Resume Next
If InStr(ItemShp.Name, "Picture") <> Empty Then ItemShp.Delete
If InStr(ItemShp.Name, "ItemGrp") <> Empty Then ItemShp.Delete
On Error GoTo 0
Next ItemShp
On Error Resume Next
.Shapes("SampleGrp").Visible = msoCTrue
.Shapes("SampleGrp").Ungroup
On Error GoTo 0
'colocar em free floating
For Each ItemShp In .Shapes
If InStr(ItemShp.Name, "Sample") > 0 Then
ItemShp.Placement = xlFreeFloating
ItemShp.Visible = msoCTrue
End If
Next ItemShp
.Shapes("CloseCustBtn").Visible = msoCTrue
.Shapes("ResetBtn").Visible = msoCTrue
.Shapes("LabelTemplate").Visible = msoCTrue
.Shapes("OpenCustBtn").Visible = msoFalse
.Range("C:F").EntireColumn.Hidden = False
End With
End Sub
Sub Customize_Close()
With Sheet1
On Error Resume Next
.Shapes("SampleGrp").Placement = xlFreeFloating
.Shapes("SampleGrp").Visible = msoFalse
On Error GoTo 0
.Shapes("CloseCustBtn").Visible = msoFalse
.Shapes("LabelTemplate").Visible = msoFalse
.Shapes("ResetBtn").Visible = msoFalse
.Shapes("OpenCustBtn").Visible = msoCTrue
.Range("C:F").EntireColumn.Hidden = True
End With
End Sub
Sub Customize_Refresh()
LastProdRow = Sheet2.Range("A99999").End(xlUp).Row 'ultima linha
With Sheet1
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
.Range("A8:F999").ClearContents
.Range("D5:F999").Characters.Font.Name = "Calibri"
.Range("10:999").EntireRow.Hidden = False
Sheet2.Range("AA4:AS999").ClearContents 'limpar critérios e resultados
'definir campos de informação
Range("A8").Value = 9
.Range("C8").Value = Chr(117)
.Range("D8").Value = "CAMPOS INFORMAÇÃO"
.Range("D9:D17").Value = Chr(168) 'caixinha desmarcada
.Range("D9:D17").Characters.Font.Name = "Wingdings"
.Range("E9:E17").Value = Sheet2.Range("l3:l11").Value
.Range("9:17").EntireRow.Hidden = True
'definir filtros
.Range("D18").Value = "FILTROS"
CustRow = 19
For ProdCol = 1 To 8
DataType = Sheet2.Cells(2, ProdCol).Value
Select Case DataType
Case Is = "TEXT"
.Range("D" & CustRow).Value = Sheet2.Cells(3, ProdCol).Value 'cabeçalho
Case Is = "LIST"
On Error Resume Next
Sheet2.Names("Criteria").Delete
Sheet2.Names("Extract").Delete
On Error GoTo 0
Sheet2.Range("A3:H" & LastProdRow).AdvancedFilter xlFilterCopy, , CopyToRange:=Sheet2.Cells(3, ProdCol + 14), Unique:=True
LastResultRow = Sheet2.Cells(9999, ProdCol + 14).End(xlUp).Row 'ultima linha de dado unico
If LastResultRow < 4 Then GoTo NoResult
.Range("A" & CustRow).Value = LastResultRow - 3 '# de itens
.Range("B" & CustRow + 1 & ":B" & CustRow + LastResultRow - 3).Value = ProdCol 'coluna peça
.Range("C" & CustRow).Value = Chr(117) 'triangulo pra direita
.Range("D" & CustRow).Value = Sheet2.Cells(3, ProdCol + 14).Value
.Range("A" & CustRow + 1 & ":A" & CustRow + LastResultRow - 3).Value = CustRow + 1 'linha
.Range("D" & CustRow + 1 & ":D" & CustRow + LastResultRow - 3).Value = Chr(254) 'caixinha marcada
.Range("D" & CustRow + 1 & ":D" & CustRow + LastResultRow - 3).Font.Name = "Wingdings"
.Range("E" & CustRow + 1 & ":E" & CustRow + LastResultRow - 3).Value = Sheet2.Range(Sheet2.Cells(4, ProdCol + 14), Sheet2.Cells(LastResultRow, ProdCol + 14)).Value 'trazer resultados
.Range(CustRow + 1 & ":" & CustRow + LastResultRow - 3).EntireRow.Hidden = True
CustRow = CustRow + LastResultRow - 2
Case Is = "NUMBER"
.Range("A" & CustRow).Value = 2
.Range("B" & CustRow + 1 & ":B" & CustRow + 2).Value = ProdCol
.Range("C" & CustRow).Value = Chr(117)
.Range("D" & CustRow).Value = Sheet2.Cells(3, ProdCol).Value
.Range("D" & CustRow + 1).Value = ">="
.Range("D" & CustRow + 2).Value = "<="
.Range(CustRow + 1 & ":" & CustRow + 2).EntireRow.Hidden = True
CustRow = CustRow + 3
NoResult:
End Select
Next ProdCol
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End With
End Sub

Related

Searching Multiple Criteria In Large Data Set to make new Data Set Excel VBA

I have a very large data set that gets updated multiple times a day. It can vary from 1000-20000 entries. I have a macro in place that searches for specific criteria and makes a new table from that data and works but it takes a very long time to sift through all the points. I want to know if there is a more eloquent way to achieve the same result.
I tried a new different methods of the same thing. Poked around at other solutions but could not get them to fit what I needed. I even tried the advanced filtering tables but to no avail.
Function AgedDivert()
'Pull from scraped data to display compact data set
On Error GoTo ErrorHandler
ufProgress.Caption = "Loading Aged Divert"
ufProgress.LabelProgress.Width = 0
pasterow = 31
sname = "Aged Divert Report"
ThisWorkbook.Sheets(sname).Rows(30 & ":" & 999999).Clear
ThisWorkbook.Sheets("Temp").Range("1:1").Copy ThisWorkbook.Sheets(sname).Range("30:30")
RowCount = WorksheetFunction.CountA(ThisWorkbook.Sheets("Scraped Data").Range("A:A"))
'Create new data sort by age and location
For i = 2 To RowCount
pctComplete = (i - 2) / (RowCount - 2)
'Filter out Direct Loads, PA2, Less than 180 Minutes, Secondary, not diverted
If Len(ThisWorkbook.Sheets("Scraped Data").Range("D" & i).Value) <> 2 And _
(ThisWorkbook.Sheets("Scraped Data").Range("J" & i).Value = "Ship Sorter" Or _
ThisWorkbook.Sheets("Scraped Data").Range("K" & i).Value = "Divert Confirm") And _
ThisWorkbook.Sheets("Scraped Data").Range("D" & i).Value <> "" And _
ThisWorkbook.Sheets("Scraped Data").Range("M" & i).Value > 180 And _
ThisWorkbook.Sheets("Scraped Data").Range("I" & i).Value <> "Left to Pick" And _
InStr(1, ThisWorkbook.Sheets("Scraped Data").Range("C" & i).Value, "Location") = 0 And _
(InStr(1, ThisWorkbook.Sheets("Scraped Data").Range("C" & i).Value, "Warehouse A") > 0 Or _
InStr(1, ThisWorkbook.Sheets("Scraped Data").Range("C" & i).Value, "Warehouse C") > 0 Or _
InStr(1, ThisWorkbook.Sheets("Scraped Data").Range("C" & i).Value, "PA") = 0) Then
ThisWorkbook.Sheets("Scraped Data").Range(i & ":" & i).Copy ThisWorkbook.Sheets(sname).Range(pasterow & ":" & pasterow)
pasterow = pasterow + 1
End If
ufProgress.LabelProgress.Width = pctComplete * ufProgress.FrameProgress.Width
ufProgress.Repaint
Next i
ufProgress.Caption = "Loading Complete. Cleaning Data"
'Remove Unnecessary Data
ThisWorkbook.Sheets(sname).Columns("R").Delete
ThisWorkbook.Sheets(sname).Columns("Q").Delete
ThisWorkbook.Sheets(sname).Columns("O").Delete
ThisWorkbook.Sheets(sname).Columns("N").Delete
ThisWorkbook.Sheets(sname).Columns("L").Delete
ThisWorkbook.Sheets(sname).Columns("K").Delete
ThisWorkbook.Sheets(sname).Columns("J").Delete
ThisWorkbook.Sheets(sname).Columns("H").Delete
ThisWorkbook.Sheets(sname).Columns("F").Delete
ThisWorkbook.Sheets(sname).Columns("E").Delete
ThisWorkbook.Sheets(sname).Range("C30:C999999").Delete
ThisWorkbook.Sheets(sname).Range("B30:B999999").Delete
'Set Data as Table
ThisWorkbook.Sheets(sname).ListObjects.Add(xlSrcRange, ThisWorkbook.Sheets(sname).Range("A30:F" & pasterow), , xlYes).Name = "AgedDivert"
AgedDivert = True
Exit Function
ErrorHandler:
AgedDivert = False
Debug.Print "Error occured in Aged Divert"
Debug.Print Err.Number & ": " & Err.Description
End Function
Copy the data to an array, filter to another array and copy back to sheet. 20,000 rows should take a few seconds.
Function AgedDivert()
Dim wb As Workbook
Dim wsData As Worksheet, wsReport As Worksheet, wsTemp As Worksheet
Dim arData, arReport
Dim lastrow As Long, i As Long, r As Long
Dim colC, colD, colI, colJ, colK, colM, msg As String
Dim t0 As Single: t0 = Timer
Const RPT_NAME = "Aged Divert Report"
'Pull from scraped data to display compact data set
On Error GoTo ErrorHandler
Set wb = ThisWorkbook
With wb
Set wsData = .Sheets("Scraped Data")
Set wsReport = .Sheets(RPT_NAME)
Set wsTemp = .Sheets("Temp")
End With
' copy data
With wsData
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
' copy sheet to array
arData = .Range("A1:P" & lastrow)
ReDim arReport(1 To lastrow, 1 To 6) ' A to F
For i = 2 To lastrow
colC = arData(i, 3)
colD = arData(i, 4)
colI = arData(i, 9)
colJ = arData(i, 10)
colK = arData(i, 11)
colM = arData(i, 13)
'Filter out Direct Loads, PA2, Less than 180 Minutes,
'Secondary, not diverted
If Len(colD) <> 2 And colD <> "" And _
(colJ = "Ship Sorter" Or colK = "Divert Confirm") _
And colM > 180 _
And colI <> "Left to Pick" _
And InStr(1, colC, "Location") = 0 And _
(InStr(1, colC, "Warehouse A") > 0 Or _
InStr(1, colC, "Warehouse C") > 0 Or _
InStr(1, colC, "PA") = 0) Then
r = r + 1 ' report row
arReport(r, 1) = arData(i, 1) ' A
arReport(r, 2) = arData(i, 4) ' D
arReport(r, 3) = arData(i, 7) ' G
arReport(r, 4) = arData(i, 9) ' I
arReport(r, 5) = arData(i, 13) ' M
arReport(r, 6) = arData(i, 16) ' P
End If
Next i
End With
' output
With wsReport
' delete existing table
.Rows("30:" & .Rows.Count).Clear
.Range("A30:F30") = Array("Col A", "Col D", "Col G", "Col I", "Col M", "Col P")
If r = 0 Then
MsgBox "No data to report", vbExclamation
Else
' copy rows and set Data as Table
.Range("A31").Resize(r, 6) = arReport
.ListObjects.Add(xlSrcRange, .Range("A30:F" & 30 + r), xlYes).Name = "AgedDivert"
End If
End With
msg = lastrow - 1 & " rows scanned from " & wsData.Name & vbLf & _
r & " rows copied to " & wsReport.Name
MsgBox msg, vbInformation, Format(Timer - t0, "0.0 secs")
AgedDivert = True
Exit Function
ErrorHandler:
AgedDivert = False
Debug.Print "Error occured in Aged Divert"
Debug.Print Err.Number & ": " & Err.Description
End Function

Excel / VBA / Rows.Count return Error 1004

The goal is to create a new entry to the Table via a Form. In the code I try to look for the last row in order to put the infromation into the blank field at the bottom. The Error 1004 appearse when i'm looking for the last row in the Table with the help of Rows.Count. This is the problem part:
iRow = Sheet1.Range("A" & Rows.Count).End(x1Up).Row + 1
Here's the whole code:
Private Sub cmdSubmit_Click()
Dim iRow As Long
iRow = Sheet1.Range("A" & Rows.Count).End(x1Up).Row + 1
With Sheet1
.Range("A" & iRow).Value = Me.txtName.Value
'Gender
If Me.optFemale.Value Then .Range("B" & iRow).Value = "Female"
If Me.optMale.Value Then .Range("B" & iRow).Value = "Male"
If Me.optUnknown.Value Then .Range("B" & iRow).Value = "Unknown"
'Maritial Status
If Me.optSingle.Value Then .Range("C" & iRow).Value = "Single"
If Me.optMarried.Value Then .Range("C" & iRow).Value = "Married"
If Me.optOther.Value Then .Range("C" & iRow).Value = "Other"
End With
'Reset the controls after submitting
Me.txtName.Value = ""
Me.optFemale.Value = False
Me.optMale.Value = False
Me.optUnknown.Value = False
Me.optSingle.Value = False
Me.optMarried.Value = False
Me.optOther.Value = False
MsgBox "Data submitted Successfully!"
End Sub

VBA How to do a recurring cycle

I have code that generates stickers, I wrote it this way below, so for 15 stickers. Can it be done with a loop for easier code writing because I have to do it up to 100 stickers? I have currently written it for 15 stickers and it works by changing the cell names manually because they are different.
Thank you
Code is:
Sheets("Helper").Range("A1:Z50").Clear
Sheets("DUT Sticker").Range("A8:C100").Clear
If Sheets("Helper").Range("A1").Value <> "" Then
Sheets("DUT Sticker").Range("A3:C7").Copy
Sheets("DUT Sticker").Range("A8").PasteSpecial Paste:=xlPasteAll
Sheets("DUT Sticker").Range("A8").Value = Sheets("DUT Sticker").Range("A3") + 1
Sheets("DUT Sticker").Select
ActiveSheet.Shapes.Range(Array("Picture 3")).Select
Selection.Copy
Sheets("DUT Sticker").Range("B8").Select
ActiveSheet.Paste
Selection.ShapeRange.IncrementLeft 2
Selection.ShapeRange.IncrementTop 4
Sheets("Helper").Range("A1").Copy
Sheets("DUT Sticker").Range("B8").PasteSpecial Paste:=xlPasteAll
Sheets("DUT Sticker").Range("B9") = Sheets("Helper").Range("B2")
Sheets("DUT Sticker").Range("B10") = Sheets("Helper").Range("J2")
Sheets("DUT Sticker").Range("B11") = Sheets("Helper").Range("K2")
Sheets("DUT Sticker").Range("C9").Formula = "=IF(Helper!R[-7]="""",""SPARE"",""SS ""&Helper!R[-7]&""#""&Helper!R[-7]C[1])"
Sheets("DUT Sticker").Range("C10") = Sheets("Sample Recording Form").Range("F5")
Sheets("DUT Sticker").Range("C12").Formula = "S/N: " & Sheets("Helper").Range("I2")
Sheets("DUT Sticker").PageSetup.PrintArea = "$B$3:$C$12"
End If
If Sheets("Helper").Range("A2").Value <> "" Then
Sheets("DUT Sticker").Range("A3:C7").Copy
Sheets("DUT Sticker").Range("A13").PasteSpecial Paste:=xlPasteAll
Sheets("DUT Sticker").Range("A13").Value = Sheets("DUT Sticker").Range("A8") + 1
Sheets("DUT Sticker").Select
ActiveSheet.Shapes.Range(Array("Picture 3")).Select
Selection.Copy
Sheets("DUT Sticker").Range("B13").Select
ActiveSheet.Paste
Selection.ShapeRange.IncrementLeft 2
Selection.ShapeRange.IncrementTop 4
Sheets("Helper").Range("A2").Copy
Sheets("DUT Sticker").Range("B13").PasteSpecial Paste:=xlPasteAll
Sheets("DUT Sticker").Range("B14") = Sheets("Helper").Range("B3")
Sheets("DUT Sticker").Range("B15") = Sheets("Helper").Range("J3")
Sheets("DUT Sticker").Range("B16") = Sheets("Helper").Range("K3")
Sheets("DUT Sticker").Range("C14").Formula = "=IF(Helper!R[-11]="""",""SPARE"",""SS ""&Helper!R[-11]&""#""&Helper!R[-11]C[1])"
Sheets("DUT Sticker").Range("C15") = Sheets("Sample Recording Form").Range("F5")
Sheets("DUT Sticker").Range("C17").Formula = "S/N: " & Sheets("Helper").Range("I3")
Sheets("DUT Sticker").PageSetup.PrintArea = "$B$3:$C$17"
End If
end sub
There are different ways to access another cell:
i = 15
Cell_A15_Value = Range("A" & i).Value
Cell_A15_Value = Range("A1").OffSet(14,0).Value
(Obviously, as you start by row 1, you need to add 14 to get to cell A15.
Please, try the next code. It is not tested, of course. I only tried following your code logic, adapting it to avoid selections and working faster. I would suggest you to try it line by line (pressing F8) and see what it does. I shouldn't be impossible to miss something in terms of variables incrementation:
Sub MakeStickers()
Dim shH As Worksheet, shD As Worksheet, lastRH As Long, i As Long, k As Long, J As Long, M As Long
Dim sh As Shape, constVal
constVal = Sheets("Sample Recording Form").Range("F5").value
k = 8: J = 3: M = 2
Set shH = Sheets("Helper")
lastRH = shH.Range("A" & shH.rows.count).End(xlUp).row
Set shD = Sheets("DUT Sticker")
shD.Range("A8:C100").Clear
'Sheets("Helper").Range("A1:Z50").Clear 'clearing this sheet, the iteration will never find a range in A:A column <> ""
For i = 1 To lastRH
If shH.Range("A" & i).value <> "" Then
shD.Range("A3:C7").Copy shD.Range("A" & k)
shD.Range("A" & k).value = shD.Range("A" & J) + 1
shD.Shapes("Picture 3").Copy: shD.Paste
Set sh = shD.Shapes(shD.Shapes.count)
sh.top = shD.Range("B" & k).top
sh.left = shD.Range("B" & k).left
shD.Range("B" & k).value = shH.Range("A" & i).value
shD.Range("B" & k + 1).value = Sheets("Helper").Range("B" & M).value
shD.Range("B" & k + 2).value = Sheets("Helper").Range("J" & M).value
shD.Range("B" & k + 3).value = Sheets("Helper").Range("K" & M)
shD.Range("C" & k + 1).Formula = "=IF(Helper!R[-" & k - 1 & "]="""",""SPARE"",""SS ""&Helper!R[-" & k - 1 & "]&""#""&Helper!R[-" & k - 1 & "]C[1])"
shD.Range("C" & k + 2).value = constVal
shD.Range("C" & k + 4).value = "S/N: " & Sheets("Helper").Range("I" & M).value
Sheets("DUT Sticker").PageSetup.PrintArea = "$B$3:$C$" & k + 4
'reset variables:
k = k + 5: M = M + 1: J = J + 5
End If
Next i
End Sub
If not confidential, can you share the workbook in discussion? I will try adapting the above code according to its behavior on the real workbook...

Looping through two cell ranges in two worksheets

The following code runs but, not getting the results. The information is there in the correct range.
Dim ID As Range
Dim SN As Range
Dim i As Integer
Set ID = Sheet6.Range("B2:B8")
Set SN = Sheet2.Range("C7:C184")
For i = 2 To ID.Cells.count
If ID.Cells(i) = SN.Cells(i) Then
MsgBox "do something"
ID.Cells.Offset(0, 2).Value = SN.Cells.Offset(0, -2).Value
Else
MsgBox "sorry"
End If
Next
i found another code and modified it to my work sheet. This one works great.
Dim i As Long
Dim j As Long
For i = 2 To 40
If Sheet6.Range("C" & i).Value = "" Then
Exit For
End If
For j = 7 To 1000
If Sheet2.Range("c" & j).Value = "" Then
Exit For
End If
If Sheet6.Range("C" & i).Text = Sheet2.Range("c" & j).Text Then
Sheet6.Range("C" & i).Offset(0, 1).Value = Sheet2.Range("c" & j).Offset(0, -2).Value
Sheet6.Range("C" & i).Offset(0, 2).Value = Sheet2.Range("c" & j).Offset(0, 2).Value
Exit For
End If
Next j
Next i

How to merge several cells using VBA

I have some problems with excel and VBA, in that don't know have much knowledge. I copied text from pdf and it's awful.
I have cells which contain some text.
The problem is that the text from one paragraph is broken down over several cells. At the beginning of each paragraph is a word in bold (e.g. CLR.) which describes the rest of the text. As such, it defines where each paragraph should start. How I can merge these cells into one?
I see
I want
Sub MergeText()
Dim strMerged$, r&, j&, i&
r = 1
Do While True
If Cells(r, 1).Characters(1, 1).Font.Bold Then
strMerged = "": strMerged = Cells(r, 1)
r = r + 1
While (Not Cells(r, 1).Characters(1).Font.Bold) And Len(Cells(r, 1)) > 0
strMerged = strMerged & Cells(r, 1)
r = r + 1
Wend
i = i + 1: Cells(i, 2) = strMerged
Cells(i, 2).Characters(1, InStr(1, strMerged, ".", vbTextCompare)).Font.Bold = True
Else
Exit Do
End If
Loop
End Sub
Modify (if needed) and try:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long, j As Long, Count As Long
Dim str As String
With ThisWorkbook.Worksheets("Sheet1") 'Change sheet name if needed
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = LastRow To 2 Step -1
If (UCase(Left(.Range("A" & i), 1)) <> Left(.Range("A" & i), 1)) And UCase(Left(.Range("A" & i - 1), 1)) = Left(.Range("A" & i - 1), 1) Then
Count = 0
For j = 1 To Len(.Range("A" & i - 1))
If .Range("A1").Characters(j, 1).Font.FontStyle = "Bold" Then
Count = Count + 1
Else
Exit For
End If
Next j
str = .Range("A" & i - 1).Value & " " & .Range("A" & i).Value
With .Range("A" & i - 1)
.Value = str
.Font.Bold = False
With .Characters(Start:=1, Length:=Count).Font
.FontStyle = "Bold"
End With
End With
.Rows(i).EntireRow.Delete
ElseIf (UCase(Left(.Range("A" & i), 1)) <> Left(.Range("A" & i), 1)) And UCase(Left(.Range("A" & i - 1), 1)) <> Left(.Range("A" & i - 1), 1) Then
str = .Range("A" & i - 1).Value & " " & .Range("A" & i).Value
With .Range("A" & i - 1)
.Value = str
.Font.Bold = False
End With
.Rows(i).EntireRow.Delete
End If
Next i
End With
End Sub

Resources