Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

replace the fixed range "F7:F80000" with a dynamic range up

0

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
Answer
Discuss

Answers

0

Hello Amin25,

A quick review of your code and I think the problem lies in you code for "lastrow"

 lastRow = .Range("F80000").End(xlUp).Row + 1

Change this line to:

 lastRow = Range("F7" & Rows.Count).End(xlUp).Row + 1

Hope this helps, if so please mark my answer as Selected.

Cheers   :-)

Discuss

Discussion

I did it but still did not display the correct invoice number and details, can you try with the attached file

    Dim LastRow As long 
   fw2 = Application.WorksheetFunction.CountIfs(Rep.Range("F7:F" & lastRow), invn, Rep.Range("C7:C" & lastrow), invs) 

     lastRow = Range("F7" & Rows.Count).End(xlUp).Row + 1
Amin25 (rep: 16) Mar 2, '24 at 1:18 am
Hi Amin25,
I downloaded your file and I am starting to work through your code. The first thing I did was to add "Option Explicit" to the top of the code windows. Doing this causes VBA to flag any code errors (there are several). Putting  "Option Explicit" at the top of all code windows is a good practice. You can make this the default - when the VB Editor window is open, click "Tools", then "Options", and in that window select "Require Variable Declaration". Also, disabling Excel features in your code affects Excel - not just your file.
Give me some time and I will try to find a solution.
WillieD24 (rep: 557) Mar 2, '24 at 10:19 pm
Ok thank you so much 
Amin25 (rep: 16) Mar 3, '24 at 2:17 am
@WillieD24, is there any update?
Amin25 (rep: 16) Mar 5, '24 at 1:25 pm
@Amin25

I don't usually give up on fixing mistakes, but I was considering "throwing in the towel" on this due to the amount of work required to fix things. Since you are still interested I will get back to working on it. I first need to step through your code to understand what is intended, then make necessary repairs. I can only devote an hour or two per day so it will be a while.
WillieD24 (rep: 557) Mar 5, '24 at 7:49 pm
Ok 
Amin25 (rep: 16) Mar 6, '24 at 6:35 am
Not yet?
Amin25 (rep: 16) Mar 8, '24 at 6:16 am
@Amin25,

I wish I had an answer for you but the deeper I looked the more mistakes I found (ex: there are command buttons which appear to do nothing.) In my opinion this file would be best served by giving it a complete re-make. That amount of work is not what this forum is about. Also, I would need to have an in-person discussion because there are so many questions which need answering. I hate to disappoint but I just don't have the time to dedicate to this issue.

Sorry.   :-(
WillieD24 (rep: 557) Mar 8, '24 at 3:32 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login