Could not get column property.Invalid argument - excel

Could net get column property. invalid argument
How not to populate the listbox and put all the data in textbox instead? Is there anyway to loop this?
Private Sub ListBox1_Click()
Dim say, LastRow As Long
TextBox1 = ListBox1.Column(0)
TextBox2 = ListBox1.Column(1)
TextBox3 = ListBox1.Column(2)
TextBox4 = ListBox1.Column(3)
TextBox5 = ListBox1.Column(4)
TextBox6 = ListBox1.Column(5)
TextBox7 = ListBox1.Column(6)
TextBox8 = ListBox1.Column(7)
TextBox11 = ListBox1.Column(8)
TextBox12 = ListBox1.Column(9)
TextBox13 = ListBox1.Column(10)
TextBox14 = ListBox1.Column(11)
TextBox15 = ListBox1.Column(12)
TextBox16 = ListBox1.Column(13)
TextBox17 = ListBox1.Column(14)
TextBox18 = ListBox1.Column(15)
TextBox19 = ListBox1.Column(16)
TextBox20 = ListBox1.Column(17)
TextBox21 = ListBox1.Column(18)
TextBox22 = ListBox1.Column(19)
TextBox23 = ListBox1.Column(20)
TextBox24 = ListBox1.Column(21)
TextBox25 = ListBox1.Column(22)
TextBox26 = ListBox1.Column(23)
LastRow = Sheets("Database").Cells(Rows.Count, "B").End(xlUp).Row
Sheets("Database").Range("B2:B" & LastRow).Find(What:=ListBox1.Value,
LookIn:=xlValues, LookAt:=xlWhole).Activate
say = ActiveCell.Row
Sheets("Database").Range("A" & say & ":Y" & say).Select

Have a look at the following demo:
Option Explicit
Private Sub UserForm_Initialize()
With ListBox1
.ColumnCount = 2
.AddItem
.List(.ListCount - 1, 0) = "Test"
.List(.ListCount - 1, 1) = "abc"
.AddItem
.List(.ListCount - 1, 0) = "Test2"
.List(.ListCount - 1, 1) = "def"
End With
End Sub
Private Sub ListBox1_Click()
Dim SelectedItem As Long
Dim j As Long
Dim ctrls As Variant
Dim TextBoxCounter As Long
SelectedItem = ListBoxSelectedIndex(Me.ListBox1)
ctrls = Array(TextBox1, TextBox2)
For j = 0 To Me.ListBox1.ColumnCount - 1
If j <> 9 And j <> 10 And j <= UBound(ctrls) Then
ctrls(j).Text = Me.ListBox1.List(SelectedItem, TextBoxCounter)
TextBoxCounter = TextBoxCounter + 1
End If
Next j
End Sub
Private Function ListBoxSelectedIndex(lBox As msforms.ListBox) As Long
With lBox
For ListBoxSelectedIndex = 0 To .ListCount - 1
If .Selected(ListBoxSelectedIndex) Then Exit For
Next ListBoxSelectedIndex
End With
End Function
This produces:
This can be easily expanded with the rest of your controls for your situation

Related

Autofit won't run as part of a sub

I'm working on a sub where, after doing a whole bunch of other things, it just selects all the cells in the active sheet and sets the rows to auto-fit height. For some reason it won't work! I've tried to the autofit row height command in various different places, but it seems that so long as it's part of the larger sub, or called within it, it won't work. However, if I write a separate sub that is run separately, it will work just fine. Does anyone know why this could be?
Below is the sub where it's misbehaving, plus the other sub I made that I can run separately. Any suggestions on how to make this all more efficient is also very welcome! (I also kind of learned coding in the wild, so I don't really know best practices...)
Option Explicit
Sub WriteToIndex(ByRef rowsArray() As Variant, ByRef Indexes() As Integer, ByRef HeaderNames() As String, myTable As ListObject, sheetName As String)
Debug.Print sheetName
Sheets(sheetName).Activate
Dim i, j As Variant
Dim count As Integer
'If no rows, no write
If (Not Not rowsArray) <> 0 Then
Else:
'If the index is longer than 5000 rows, this will need to be updated!
Range("B4:L5000").Clear
MsgBox "Sheet " & sheetName & " was not populated because there is nothing attributed to it"
Exit Sub
End If
'If nothing in Int/Ext section, no write
count = 0
For i = LBound(rowsArray) To UBound(rowsArray)
For j = LBound(Indexes) To UBound(Indexes)
If myTable.DataBodyRange(rowsArray(i), Indexes(j)).Value = "n" Then count = count + 1
Next
Next
If count = 0 Then
'If the index is longer than 5000 rows, this will need to be updated!
Range("B4:L5000").Clear
MsgBox "Sheet " & sheetName & " was not populated because there is nothing attributed to it"
Exit Sub
End If
Sheets(sheetName).Activate
'Pulls desired index fonts and sizes from Settings tab
Dim IndexFont As String, HeaderFSize, BodyFSize As Integer, HeaderBold As Boolean
IndexFont = Worksheets("Settings").Cells(10, 11).Value
HeaderFSize = Worksheets("Settings").Cells(11, 11).Value
HeaderBold = Worksheets("Settings").Cells(12, 11).Value
BodyFSize = Worksheets("Settings").Cells(13, 11).Value
'Remove headers from array if there are no items for the index
Dim loopno, pos, zeroloops(), zeroloopstart As Integer
count = 0
zeroloopstart = 0
loopno = 1
ReDim Preserve zeroloops(zeroloopstart)
For i = LBound(Indexes) To UBound(Indexes)
For j = LBound(rowsArray) To UBound(rowsArray)
If myTable.DataBodyRange(rowsArray(j), Indexes(i)).Text = "n" Then count = count + 1
Next
If count = 0 Then
pos = loopno - 1
ReDim Preserve zeroloops(0 To zeroloopstart)
zeroloops(zeroloopstart) = pos
zeroloopstart = zeroloopstart + 1
End If
count = 0
loopno = loopno + 1
Next
'If a header is in the zeroloops array, it gets removed from the Header array
If IsEmpty(zeroloops(0)) Then
Debug.Print "Empty"
Else
For i = LBound(zeroloops) To UBound(zeroloops)
For j = zeroloops(i) To UBound(Indexes) - 1
Indexes(j) = Indexes(j + 1)
Next j
For j = zeroloops(i) To UBound(HeaderNames) - 1
HeaderNames(j) = HeaderNames(j + 1)
Next j
For j = LBound(zeroloops) To UBound(zeroloops)
zeroloops(j) = zeroloops(j) - 1
Next j
Debug.Print
ReDim Preserve Indexes(0 To (UBound(Indexes) - 1))
ReDim Preserve HeaderNames(0 To (UBound(HeaderNames) - 1))
Next i
End If
'If the index is longer than 5000 rows, this will need to be updated!
Range("B4:L5000").Clear
'Apply preferred font to entire sheet
Cells.Font.Name = IndexFont
Dim KeyIDCol, DescCol, SourceCol, ProductCol, CatCol, ColorCol, FinishCol, SizeCol, ContactCol, SpecCol, RemarkCol As Integer
'Index for each value to report
'If additional column needs to be reported, add the line and swap out the name in the Listcolumns definition
KeyIDCol = Worksheets("Database").ListObjects("Database").ListColumns("KEYNOTE #").Index
DescCol = Worksheets("Database").ListObjects("Database").ListColumns("KEYNOTE DESCRIPTION").Index
SourceCol = Worksheets("Database").ListObjects("Database").ListColumns("SOURCE").Index
ProductCol = Worksheets("Database").ListObjects("Database").ListColumns("PRODUCT").Index
CatCol = Worksheets("Database").ListObjects("Database").ListColumns("CAT. NO.").Index
ColorCol = Worksheets("Database").ListObjects("Database").ListColumns("COLOR").Index
FinishCol = Worksheets("Database").ListObjects("Database").ListColumns("FINISH").Index
SizeCol = Worksheets("Database").ListObjects("Database").ListColumns("SIZE").Index
ContactCol = Worksheets("Database").ListObjects("Database").ListColumns("CONTACT").Index
SpecCol = Worksheets("Database").ListObjects("Database").ListColumns("SECTION #").Index
RemarkCol = Worksheets("Database").ListObjects("Database").ListColumns("REMARKS").Index
'Definitions for write loop
Dim NextWriteRow, HeaderListIndex As Integer
Dim ArrayItem As Variant
Dim WriteStartCell, Cell As Range
NextWriteRow = 4
HeaderListIndex = 0
i = 1 ' for moving to the next KeyID
j = 0 ' start counter for steps
Set WriteStartCell = Cells(NextWriteRow, 2)
Dim k As Variant
'Outer loop puts in headers
For Each ArrayItem In Indexes
With Cells(NextWriteRow, 2)
.Value = HeaderNames(HeaderListIndex)
.VerticalAlignment = xlBottom
.Font.Size = HeaderFSize
.Font.Bold = HeaderBold
End With
HeaderListIndex = HeaderListIndex + 1
'Second loop puts in KeynoteID with all pertinent info
For k = LBound(rowsArray) To UBound(rowsArray)
If myTable.DataBodyRange(rowsArray(k), ArrayItem).Value = "n" Then
With WriteStartCell
.Offset(i, 0).Value = myTable.DataBodyRange(rowsArray(k), ArrayItem).Offset(, KeyIDCol - ArrayItem).Value
.Offset(i, 1).Value = myTable.DataBodyRange(rowsArray(k), ArrayItem).Offset(, DescCol - ArrayItem).Value
.Offset(i, 2).Value = myTable.DataBodyRange(rowsArray(k), ArrayItem).Offset(, SourceCol - ArrayItem).Value
.Offset(i, 3).Value = myTable.DataBodyRange(rowsArray(k), ArrayItem).Offset(, ProductCol - ArrayItem).Value
.Offset(i, 4).Value = myTable.DataBodyRange(rowsArray(k), ArrayItem).Offset(, CatCol - ArrayItem).Value
.Offset(i, 5).Value = myTable.DataBodyRange(rowsArray(k), ArrayItem).Offset(, ColorCol - ArrayItem).Value
.Offset(i, 6).Value = myTable.DataBodyRange(rowsArray(k), ArrayItem).Offset(, FinishCol - ArrayItem).Value
.Offset(i, 7).Value = myTable.DataBodyRange(rowsArray(k), ArrayItem).Offset(, SizeCol - ArrayItem).Value
.Offset(i, 8).Value = myTable.DataBodyRange(rowsArray(k), ArrayItem).Offset(, ContactCol - ArrayItem).Value
With .Offset(i, 9)
.NumberFormat = "000000"
.HorizontalAlignment = xlCenter
.Value = myTable.DataBodyRange(rowsArray(k), ArrayItem).Offset(, SpecCol - ArrayItem).Value
End With
.Offset(i, 10).Value = myTable.DataBodyRange(rowsArray(k), ArrayItem).Offset(, RemarkCol - ArrayItem).Value
End With
With Range(WriteStartCell.Offset(i, 0), WriteStartCell.Offset(i, 10))
.VerticalAlignment = xlTop
.WrapText = True
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Font.Size = BodyFSize
With .Borders
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
End With
i = i + 1
j = j + 1
End If
Next
j = j + 2
NextWriteRow = NextWriteRow + j
i = i + 2
j = 0
Next
'This is the autofit that won't work for some reason
Cells.Rows.Autofit
Debug.Print "Works"
End Sub
'---
Sub AutofitRowHeight()
Dim sheetnames() As String
ReDim sheetnames(0 To 17)
sheetnames(0) = "SF-ALL-I"
sheetnames(1) = "SF-ALL-E"
sheetnames(2) = "SF-H-I"
sheetnames(3) = "SF-H-E"
sheetnames(4) = "SF-CUP-I"
sheetnames(5) = "SF-CUP-E"
sheetnames(6) = "SF-GB-I"
sheetnames(7) = "SF-GB-E"
sheetnames(8) = "LM-ALL-I"
sheetnames(9) = "LM-ALL-E"
sheetnames(10) = "LM-H-I"
sheetnames(11) = "LM-H-E"
sheetnames(12) = "LM-CC-I"
sheetnames(13) = "LM-CC-E"
sheetnames(14) = "LM-SCC-I"
sheetnames(15) = "LM-SCC-E"
sheetnames(16) = "LM-GB-I"
sheetnames(17) = "LM-GB-E"
Dim i As Variant
For i = LBound(sheetnames) To UBound(sheetnames)
Sheets(sheetnames(i)).Activate
Cells.Rows.AutoFit
Next
Sheets("Database").Activate
Cells(1, 1).Select
End Sub

When I delete a row VBA jumps from Modules to Excel Objects code

I have this code in which I do some checking to delete items in a table according to their numeration. When debugging the code whenever it reaches the critical line (shown below) it jumps to the respective Excel Objects code of that sheet (shown even lower). I already tested positioning it in different places across the code and using Application.Enableevents = false but it didn't work.
Any suggestions?
First Code:
Sub deleta_linhas()
Dim linha As Integer, coluna As Integer
Dim n_item As Integer, n_prod As Integer
Dim soma As Integer, linha_selecionada As Integer
linha_selecionada = ActiveCell.Row
Dim linha_atual(1 To 2) As Integer, next_line(1 To 2) As Integer, line_before(1 To 2) As Integer
Dim i As Integer
i = 0
Dim novos_numeros As String
' Condição 1 em que há apenas 1.1 e mais nada
If IsEmpty(Cells(36, 1)) = True Then
ActiveCell.EntireRow.Delete
Range("B5").Value = 1
Range("B6").Value = 1
Range("B7").Value = 0
Range("B8").Value = 35
Else
' Condição 2 em que há 1.1, 1.2 e mais ou 1.1, 2.1 e mais
linha_atual(1) = Mid(Cells(linha_selecionada, 1), 1, 1)
linha_atual(2) = Mid(Cells(linha_selecionada, 1), 5, 1)
next_line(1) = Mid(Cells(linha_selecionada + 1, 1), 1, 1)
next_line(2) = Mid(Cells(linha_selecionada + 1, 1), 5, 1)
line_before(1) = Mid(Cells(linha_selecionada - 1, 1), 1, 1)
line_before(2) = Mid(Cells(linha_selecionada - 1, 1), 5, 1)
If linha_atual(1) = next_line(1) = False And linha_atual(2) = next_line(2) Then
Selection.EntireRow.Delete '(THIS IS WHERE IT JUMPS TO EXCEL OBJECTS)
Range("B8").Value = Range("B8").Value - 1
Range("B5").Value = Range("B5").Value - 1
While IsEmpty(Cells(linha_selecionada + i, 1)) = False
n_item = Mid(Cells(linha_selecionada + 1, 1), 1, 1) - 1
novos_numeros = n_item & " . 1"
Cells(linha_selecionada + 1, 1).Value = novos_numeros
i = i + 1
Wend
End If
End If
linha = 36
coluna = 9
soma = 0
Selection.EntireRow.Delete
While IsEmpty(Cells(linha, coluna)) = False
soma = soma + Cells(linha, coluna).Value
linha = linha + 1
Wend
linha = linha + 1
Cells(linha, coluna).Value = soma
End Sub
Code in the Excel Objects (mostly of combobox buttons)
Private Sub ComboBox1_Change()
If ComboBox1.Value = "Fabricação" Then
Rows("41:52").Select
Selection.EntireRow.Hidden = True
Range("B28").Select
Range("B11").Value = "Fabricação"
End If
If ComboBox1.Value = "Nacionalização" Then
Rows("41:52").Select
Selection.EntireRow.Hidden = False
Range("B28").Select
Range("B11").Value = "Nacionalização"
End If
If ComboBox1.Value = "Projeto" Then
Rows("41:52").Select
Selection.EntireRow.Hidden = False
Range("B28").Select
Range("B11").Value = "Projeto"
End If
If ComboBox1.Value = "Manutenção" Then
Rows("41:52").Select
Selection.EntireRow.Hidden = False
Range("B28").Select
Range("B11").Value = "Manutenção"
End If
If ComboBox1.Value = "Industrialização" Then
Rows("41:52").Select
Selection.EntireRow.Hidden = False
Range("B28").Select
Range("B11").Value = "Industrialização"
End If
End Sub
' Lógicas dos tratamentos
Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then
Range("B10").Value = "Tempera"
End If
End Sub
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then
Range("B10").Value = "Nitretação"
End If
End Sub
Private Sub OptionButton3_Click()
If OptionButton3.Value = True Then
Range("B10").Value = "Cementação"
End If
End Sub
Private Sub OptionButton4_Click()
If OptionButton4.Value = True Then
Range("B10").Value = "---"
End If
End Sub
' FIM

Insert TextBox value into first empty cell in column

I have the following code which works fine. (TextBoxes are on "UserForm" in Excel VBA, and executes with clicking CommandButton1 on that UserForm.) But can anyone help me to insert the value of each TextBox into the first empty cell in Column M? This way inserts empty rows. I will use this in several workbooks, so using "ActiveSheet" is easiest for me.
Sub UserForm_Initialize()
Me.TextBox1.Value = CStr(ActiveSheet.Range("M1").Value)
Me.TextBox2.Value = CStr(ActiveSheet.Range("M2").Value)
Me.TextBox3.Value = CStr(ActiveSheet.Range("M3").Value)
Me.TextBox4.Value = CStr(ActiveSheet.Range("M4").Value)
Me.TextBox5.Value = CStr(ActiveSheet.Range("M5").Value)
Me.TextBox6.Value = CStr(ActiveSheet.Range("M6").Value)
Me.TextBox7.Value = CStr(ActiveSheet.Range("M7").Value)
Me.TextBox8.Value = CStr(ActiveSheet.Range("M8").Value)
Me.TextBox9.Value = CStr(ActiveSheet.Range("M9").Value)
Me.TextBox10.Value = CStr(ActiveSheet.Range("M10").Value)
Me.TextBox11.Value = CStr(ActiveSheet.Range("M11").Value)
Me.TextBox12.Value = CStr(ActiveSheet.Range("M12").Value)
Me.TextBox13.Value = CStr(ActiveSheet.Range("M13").Value)
Me.TextBox14.Value = CStr(ActiveSheet.Range("M14").Value)
Me.TextBox15.Value = CStr(ActiveSheet.Range("M15").Value)
Me.TextBox16.Value = CStr(ActiveSheet.Range("M16").Value)
Me.TextBox17.Value = CStr(ActiveSheet.Range("M17").Value)
Me.TextBox18.Value = CStr(ActiveSheet.Range("M18").Value)
Me.TextBox19.Value = CStr(ActiveSheet.Range("M19").Value)
Me.TextBox20.Value = CStr(ActiveSheet.Range("M20").Value)
Me.TextBox21.Value = CStr(ActiveSheet.Range("M21").Value)
Me.TextBox22.Value = CStr(ActiveSheet.Range("M22").Value)
End Sub
'Insert Button
Private Sub CommandButton1_Click()
Range("M1") = Me.TextBox1.Text
Range("M2") = Me.TextBox2.Text
Range("M3") = Me.TextBox3.Text
Range("M4") = Me.TextBox4.Text
Range("M5") = Me.TextBox5.Text
Range("M6") = Me.TextBox6.Text
Range("M7") = Me.TextBox7.Text
Range("M8") = Me.TextBox8.Text
Range("M9") = Me.TextBox9.Text
Range("M10") = Me.TextBox10.Text
Range("M11") = Me.TextBox11.Text
Range("M12") = Me.TextBox12.Text
Range("M13") = Me.TextBox13.Text
Range("M14") = Me.TextBox14.Text
Range("M15") = Me.TextBox15.Text
Range("M16") = Me.TextBox16.Text
Range("M17") = Me.TextBox17.Text
Range("M18") = Me.TextBox18.Text
Range("M19") = Me.TextBox19.Text
Range("M20") = Me.TextBox20.Text
Range("M21") = Me.TextBox21.Text
Range("M22") = Me.TextBox22.Text
Range("A25").Value = "Hide"
Unload Me
End Sub
Sub CommandButton2_Click()
'Cancel button
Unload Me
End Sub
EDIT: last guess
You can do something like this:
Private Sub CommandButton1_Click()
Dim i As Long, c As Range
ActiveSheet.Range("M1:M22").ClearContents
Set c = ActiveSheet.Range("M1")
For i = 1 to 22
c.Value = Me.Controls("TextBox" & i).Text
If Len(c.Value) > 0 then Set c = c.offset(1, 0)
Next i
Unload Me
End Sub

Creating a loop within a UserForm

this is a continuation of my previous question...
I'm trying to create a user form that will go through a list on a worksheet (TESTER). The form should display the first row of data from the list. User will also be able to select one of two options Active or ITW. Finally, the user is free to add additional comments.
This is where I'm running into trouble, once the user clicks Add, the values from the form should populate the next blank row in a separate sheet (pasteHere). I have no issues with the form displaying the next line of data on the list, but I don't know how to create a loop that will allow me to find the next blank row after clicking the add button. At the moment, I've only initialized j as 1. And every time I click add, it will paste on the first row in the pasteHere worksheet.
Sub addBtn_Click()
Application.ScreenUpdating = False
Dim pasteSheet As Worksheet
Dim j As Long
j = 1 'how can I loop this part
Set pasteSheet = Application.Worksheets("pasteHere")
pasteSheet.Cells(j, j) = ric
pasteSheet.Cells(j, j + 2) = name
pasteSheet.Cells(j, j + 4) = valueUSD
pasteSheet.Cells(j + 1, j) = dstr
i = i + 1
j = j + 2
UserForm1_Initialize
End Sub
Would appreciate any help here. Full code below:
Public valueUSD, name, ric, dstr, sitchStr, pStr As String
Public i, lRow As Long
Sub UserForm1_Initialize()
If Worksheets("pasteHere").Range("A1") = "" Then
i = 2
End If
activeCheck.Value = False
itwCheck.Value = False
TextBox2.Value = ""
ric = Worksheets("Tester").Range("H" & i)
name = Worksheets("Tester").Range("B" & i)
valueUSD = Worksheets("Tester").Range("C" & i)
sitchStr = ""
dstr = ""
pStr = ric & " " & name & " " & valueUSD & " "
UserForm1.Label1.Caption = pStr
End Sub
Sub activeCheck_Change()
If activeCheck.Value = True Then
sitchStr = sitchStr + activeCheck.Caption
Else
sitchStr = ""
End If
End Sub
Sub itwCheck_Change()
If activeCheck.Value = False And itwCheck.Value = True Then
sitchStr = sitchStr + itwCheck.Caption
ElseIf activeCheck.Value = True And itwCheck.Value = True Then
MsgBox ("You can only be active OR ITW")
End If
End Sub
Sub TextBox2_Change()
dstr = sitchStr & ", " & TextBox2.Value
End Sub
Sub addBtn_Click()
Application.ScreenUpdating = False
Dim pasteSheet As Worksheet
Dim j As Long
j = 1 'how can I loop this part
Set pasteSheet = Application.Worksheets("pasteHere")
pasteSheet.Cells(j, j) = ric
pasteSheet.Cells(j, j + 2) = name
pasteSheet.Cells(j, j + 4) = valueUSD
pasteSheet.Cells(j + 1, j) = dstr
i = i + 1
j = j + 2
UserForm1_Initialize
End Sub
Sub skipBtn_Click()
i = i + 1
UserForm1_Initialize
End Sub
Sub exitBtn_Click()
Unload Me
End Sub

Unload form after printing in Excel

In frmClothingPricer, when cmdPrint is pressed, frmPrint activates and is printed however many times asked for. I don't want 10+ frmPrint "active". How can I close frmPrint after each print loop? I have tried it on frmPrint "Unload Me" but that doesn't unload it either. ?? what am I missing?
Routine for printing
If Len(HowMany) = 0 Then
End
Else
Do Until i = HowMany
frmPrint.Show 'prints form on activation
i = i + 1
Unload frmPrint 'this isn't working = several forms are open
Loop
End If
frmPrint code
Private Sub UserForm_Initialize()
PrintMe
End Sub
Private Sub PrintMe()
lblPrintMonthCode.Caption = frmClothingPricer.MonthCode
lblPrintPricer.Caption = frmClothingPricer.Pricer
lblPrintCost.Caption = (frmClothingPricer.Cost * 100)
lblPrintDescription.Caption = frmClothingPricer.Description
lblPrintPrice.Caption = frmClothingPricer.Price
lblPrintItemNumber = frmClothingPricer.ItemNumber
frmPrint.PrintForm
'tried unload.me here with same results
End Sub
I solved it by keeping all the code except labels on the original form. The latest errors revolved around variables I messed up switching around. It now works perfectly (below):
form1
Public Price As Double
Public Percent As Double
Public Cost As Currency
Public Description As String
Public MonthCode As Integer
Public Pricer As String
Public ItemNumber As Double
Private Sub UserForm_Initialize()
Pricer = InputBox("Enter Your Pricer Number", vbOKOnly, "")
If Len(Pricer) = 0 Then 'Checking if Length of name is 0 characters
End
Else
End If
End Sub
Private Sub cmdSearch_Click()
Dim Response As Long
Dim NotFound As Integer
Dim arr As Variant
Dim i As Long
Dim str1 As String, str2 As String, str3 As String
lbxCost.BackColor = &H80000005
lbxCost.Locked = False
NotFound = 0
ActiveWorkbook.Sheets("Items").Activate
Response = Val("0" & Replace(txtItemNumber.Text, "-", ""))
ItemNumber = Response
If Response <> False Then
With ActiveSheet
arr = .Range("A2:D" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With
For i = 1 To UBound(arr)
If arr(i, 1) = Response Then
str1 = IIf(str1 = "", arr(i, 2), str1 & "|" & arr(i, 2))
str2 = IIf(str2 = "", arr(i, 3), str2 & "|" & arr(i, 3))
str3 = IIf(str3 = "", arr(i, 4), str3 & "|" & arr(i, 4))
End If
Next
If str1 = "" Then
MsgBox "Item Number Not Found!", vbExclamation
NotFound = 1
txtItemNumber.Text = ""
txtItemNumber.SetFocus
Else
Frame1.Visible = True
lbxDescription.List = Split(str1, "|")
lbxCost.List = Split(str2, "|")
ListBox3.List = Split(str3, "|")
End If
End If
lbxCost.ListIndex = 0
End Sub
Private Sub lbxCost_Click()
Frame2.Visible = True
End Sub
Private Sub lbxPercent_Click()
Frame3.Visible = True
lbxCost.BackColor = &H80000004
lbxCost.Locked = True
For x = 0 To lbxCost.ListCount - 1
If lbxCost.Selected(x) = True Then
Cost = lbxCost.List(x)
Description = lbxDescription.List(x)
End If
Next x
For y = 0 To lbxPercent.ListCount - 1
If lbxPercent.Selected(y) = True Then
Percent = lbxPercent.List(y)
End If
Next y
lblPrice.Caption = (Round(Cost * (1 + (Percent / 100)), 0)) - 0.01
Price = lblPrice.Caption
lblItemNumber.Caption = txtItemNumber.Text
lblDescription.Caption = Description
MonthCode = (Year(Now)) + (Month(Now)) - 1765
lblMonthCode.Caption = MonthCode
lblPricer.Caption = Pricer
cmdPrint.SetFocus
End Sub
Private Sub cmdPrint_Click()
Dim i As Integer
Dim Howmany As Double
Load frmPopup
Howmany = Val(txtQuantity.Text)
i = 1
Do Until i > Howmany
frmPopup.PrintForm
i = i + 1
Loop
lbxPercent.ListIndex = -1
Frame1.Visible = False
Frame2.Visible = False
Frame3.Visible = False
txtItemNumber.Text = ""
txtItemNumber.SetFocus
Unload frmPopup
End Sub
form2
Private Sub UserForm_Initialize()
lblPrintMonthCode.Caption = frmClothingPricer.MonthCode
lblPrintPricer.Caption = frmClothingPricer.Pricer
lblPrintCost.Caption = (frmClothingPricer.Cost * 100)
lblPrintDescription.Caption = frmClothingPricer.Description
lblPrintPrice.Caption = frmClothingPricer.Price
lblPrintItemNumber = frmClothingPricer.ItemNumber
End Sub

Resources