i have userform called 'inv_form' this form Combobox called 'Btn_invs' this one for invoice type and textbox called 'Btn_invn' to write the invoice number and show a button to search in the sheet 'Report' in columns F and C for the invoice number and display the invoice details in listbox but In this code, I want to replace the fixed range
"F7:F80000" with a dynamic range up to "F7:F" & lastrow of the table in sheet Report but with like that "F7:F80000" so i have to make the table to 80000 and if I used this "F7:F" & lastrow not display the correct invoice
Private Sub CmdRecallInv_Click() 'recall invoives
invs = Me.Btn_invs.Value
invn = Format(Me.Btn_invn.Value, "00000")
If invs = "" Or invs = "" Then MsgBox "Incorrect Data :Please input the invoice type and invoice number", vbCritical, "Inventory Program ": Exit Sub
fw2 = Application.WorksheetFunction.CountIfs(Rep.Range("F7:F80000"), invn, Rep.Range("C7:C80000"), invs)
If fw2 < 1 Then: MsgBox "This invoice not found. Please make sure the invoice number ", vbCritical, "Inventory Program " _
: Me.Btn_invn.Value = "": Me.Btn_invn.SetFocus: Exit Sub
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
With Rep
.Unprotect ("8521")
.Range("$C$6:$Z$6").AutoFilter field:=5, Criteria1:=invn
.Range("$C$6:$Z$6").AutoFilter field:=2, Criteria1:=invs
lastRow = .Range("F80000").End(xlUp).Row + 1 'here also want to lastrow
.Range("$C$6:$Z$6").AutoFilter
.Protect Password:=("8521")
Me.CbInvStore.Value = .Cells(lastRow - fw2, "C") 'inv_store
Me.Lbl_typ.Caption = .Cells(lastRow - fw2, "E").Value 'inv_type
Me.TbInvNo.Value = Format(.Cells(lastRow - fw2, "F").Value, "00000") 'inv no
Me.TbDate.Text = .Cells(lastRow - fw2, "G").Value 'date
Me.CbCustomerName.Value = .Cells(lastRow - fw2, "H").Value 'customer
'Me.CbMrName.Value = .Cells(LastRow - fw2, "U").Value 'mr
Me.CbDepartment.Value = .Cells(lastRow - fw2, "B").Value 'Department
Inv.Unprotect ("8521")
Inv.Range("B9:N" & lastRow).ClearContents
Inv.Range("B9:K" & 8 + fw2).Value = .Range(.Cells(lastRow - fw2, "J"), .Cells(lastRow - 1, "S")).Value 'all products
Inv.Range("L9:M" & 8 + fw2).Value = .Range(.Cells(lastRow - fw2, "W"), .Cells(lastRow - 1, "X")).Value 'all products
Inv.Range("N9:N" & 8 + fw2).Value = .Range(.Cells(lastRow - fw2, "B"), .Cells(lastRow - 1, "B")).Value
End With
With ListBox1
.IntegralHeight = False
.ColumnCount = 10
.ColumnWidths = "30;165;45;55;60;55;55;65;60;55;55;55;55" ';80;60
.list = Inv.Range("B8:N" & 8 + fw2).Cells.Value
.IntegralHeight = True
''''formating
For f = 1 To ListBox1.ListCount - 1
.list(i, 4) = Format(.list(i, 4), "#,##0.00")
.list(i, 5) = Format(.list(i, 5), "#,##0.00")
.list(i, 7) = Format(.list(i, 7), "#,##0.00")
.list(i, 8) = Format(.list(i, 8), "#,##0.00")
.list(i, 9) = Format(.list(i, 9), "#,##0.00") 'net price
Next f
Me.TbDate.Text = Format(Me.TbDate.Text, "yyyy/mm/dd") 'date
End With
Me.Btn_invs.Value = ""
Me.Btn_invn.Value = ""
Me.CbInvStore.Enabled = True
Me.TbInvNo.Enabled = False
Inv.Range("B9:N" & lastRow).ClearContents
Me.FrNewInv.Visible = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub