Hi,
Thanks for this illustration.
It helps a lot
Hi,
Thanks for this illustration.
It helps a lot
I want to use a cell for the input of a search field. However, I want the cell to contain a predefined text namely: +++____/____/______+++ the first section contains 3 digits, the second contains 4 digits, and the third section contains 5 digits. If one clicks in the cell, the user must enter 12 digits in the total: 3, 4 and 5. So 12 in the total that the user enters consecutively. The end result should look like this: +++123/1234/12345+++ I've tried via data validation (via a formula) but I can't find a formula that allows for a predefined text. Thanks for the help.
A CSE formula in Excel is an array formula that must be entered with control+shift+enter. When a formula is entered with CSE, Excel automatically wraps the formula in "curly braces {}". (I just looked it up, because I'm not that smart 😉)
I can't find any formula in the file I posted with "Curly braces"?
Hi Carim
I don’t understand your post.
Hello,
Just a couple of remarks :
- wk9128 has proposed a Sumproduct function which is already an array formula ... so there is no need for CSE - Ctrl Shift Enter keys in order to enter the formula -
- when designing an Excel spreadsheet, to be effective, there are 7 golden rules :
7 Golden Rules for Spreadsheet DesignHope this will help
You are talking about: there is no need for CSE? I didn’t mention or ask anything about CSE?
The designing of the workbook I have put in attach, isn’t a real workbook, it was just created to illustrate the problem I had. So it isn’t representative.
I’m sure that wk9128 can figure out the problem I have and can propose an answer which I would really appreciate.
Hi WK9128,
The formula you suggested works perfectly! Thank you for that.
However, I have some questions.
Why can't the Range (C1:C10) be changed into Range(C1:D10)? When I extend the range to multiple columns, I get an error.
Is there a solution to extend the Range to multiple columns?
If any cell is empty in the search range, the result is wrong (See attachement)
Thanks a lot!
Hi all,
How can I count how many times a date occurs between two dates in a range for a given customer? I have tried SUMIFS, COUNTIFS but I can't reach my goal.
Attached you will find an example with COUNTIFS but the result is incorrect. Can someone help me?
Many thanks in advance.
PS: date= dd/mm/yyyy
With what I sent the user clicks a Client name in the Table on the Orders sheet and an Invoice is created for that Client doing just as you want.
You are right! I didn't look very well at it and just run it through the VBE and of course, I didn't see what your code is actually doing.
Like you said, it works exactly as I asked. Thank you for that. The code you wrote to achieve this is not very clear to me, but that is not absolutely needed.
you cannot run a Worksheet_SelectionChange code through VBE. Did you try to copy/paste the code to a Standard Module? That will not work.
I know now, but I didn't knew before. So I've learned again, thank you for that.
Have you actually tested using the file I sent?
I did, but not in a proper way. I did test it again now, like you explained in your second answer, and it works beautifully!
Why would you want a Combobox created each time you want to create an invoice for a client? Why not just have a Combobox on the sheet anyway, and select a Client from there?
Seems far simpler to just select a Client directly from the Table on the Orders sheet and create an invoice for that client automatically. That is what the code I supplied does.
Correct, that seems far simpler. Thank you.
Why was this not mentioned in your original post?
Because, I thought it would be better to ask little by little in stead of asking a lot at once. I'm sorry.
Where is that column? Why have you not included it in your sample file? Your explanation reads as if you want new "Yes"/"No" column created each time an invoice is needed, which, of course, is totally impractical.
In my sample file, the column was not included yet.
I would like just two additional columns. One with the mention "invoice created" to prevent that the invoice should be created a second time. And a second column with a "yes" choice to move the invoice lines to the sheet archives.
ou mean you want a new sheet created every time an invoice is created? How should the new sheet be named? You would soon end up with a workbooks containing hundreds of sheets, if not thousands!
Certainly, there should not be created a new sheet every time an invoice is created.
I suggest you decide just what it is you want, create a sample workbook that clearly shows how the Orders sheet should be and a separate sheet(s) to show your required result. Then you may get an answer you want.
The sample I sent was just a sample to see the technique to do what I wanted.
I will now send my original workbook an a word doc with some explication.
Your help is much appreciated. Thank you so much.
Hi KjBox,
Thank you for your help but it didn't quit do what I want.
There is no commandbutton that I can hit, no dropdown with all the clients who have ordered.
When I run your code through the VBE,
It does following:
setting date
invoiceNo
Article
It does not set:
amount nor count it together
No price is transferred to the invoice
No discount is transferred
Not total has been calculated
I have seen no Commandbutton to active the making of the invoice.
There should be a dropdownbox filled with all customers in the order list.
So, the user should be able to click the dropdownbox, choose a client and make an invoice where the articles that have been ordered on several days but sometimes with the same article number, should be created as ONE single order line by counting the amount together, if the client and the Article number are equal.
Once an invoive is made, there should be a new colomn that says, "invoice created" and a second colomn with drowdown "YES/NO". If value = Yes then the order line should be completely transferred to an new sheet (Archive invoices)
Appreciate your help!
MODERATOR NOTICE: This topic has also been posted on other sites and may already have an answer elsewhere. Please take this into consideration when answering this question
I have a table, containing orders from different clients, different items, ordered on different days.
I would like when the users hits the commandbutton, vba should ask for what client (showing a dropdown with all the clients who have ordered).
When the user clicks the client, an invoice is made for that client, just 1 invoice/client regardless the dates the client ordered. VBA should count the amounts together.
An example is attached.
Any help is much appreciated.
Works fine now. Thanks a lot!
I still have an issue.
I would like that the order number appears on every line from the same order.
Now it puts the order number on the first line only (what I asked initially but I see now that this is wrong).
When I put the code line into the loop, It creates for every line a new increased number.
If the order contains 4 order lines, the order number should be the same for the those 4 lines.
Could some one help me?
Thx in advance.
You were right the first time. I thought you just replaced the line but you also changed the row to (1, 8).
I didn't noticed. I'm sorry. When I tried before, it was with (0, 8 ) and that didn't work of course.
But now it works just fine with (1, 8).
Thanks
Thanks for your answer. I tried that, but didn't work.
The code puts it on the last order line of the last (previous) order
I have a table with multi columns.
I use a code to transfer all the data from a listbox to an Excel sheet.
The listbox contains an order from 1 client at the time, with different articles the client ordered.
Everything is wroking fine except for the order number. If someone orders 4 articles, the code transfers the 4 lines perfectly but puts the order number in the last row and leaves the cells above blank. I would like that the code puts the order number in the first row of the order an leaves the cells below blank.
This is the code I'm using:
Private Sub cmdBestellen_Click()
Dim r As Long
For r = 1 To Me.lstboxBesteldeArt.ListCount - 1
ThisWorkbook.Sheets("Bestelde Artikelen").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Me.lstboxBesteldeArt.List(r, 0)
ThisWorkbook.Sheets("Bestelde Artikelen").Range("A" & Rows.Count).End(xlUp).Offset(0, 1) = Me.lstboxBesteldeArt.List(r, 1)
ThisWorkbook.Sheets("Bestelde Artikelen").Range("A" & Rows.Count).End(xlUp).Offset(0, 2) = Me.lstboxBesteldeArt.List(r, 2)
ThisWorkbook.Sheets("Bestelde Artikelen").Range("A" & Rows.Count).End(xlUp).Offset(0, 3) = Me.lstboxBesteldeArt.List(r, 3)
ThisWorkbook.Sheets("Bestelde Artikelen").Range("A" & Rows.Count).End(xlUp).Offset(0, 4) = Me.lstboxBesteldeArt.List(r, 4)
ThisWorkbook.Sheets("Bestelde Artikelen").Range("A" & Rows.Count).End(xlUp).Offset(0, 5) = Me.lstboxBesteldeArt.List(r, 5)
ThisWorkbook.Sheets("Bestelde Artikelen").Range("A" & Rows.Count).End(xlUp).Offset(0, 6) = Me.lstboxBesteldeArt.List(r, 6)
ThisWorkbook.Sheets("Bestelde Artikelen").Range("A" & Rows.Count).End(xlUp).Offset(0, 7) = Me.lstboxBesteldeArt.List(r, 7)
Next r
ThisWorkbook.Sheets("Bestelde Artikelen").Range("A" & Rows.Count).End(xlUp).Offset(0, 8) = Sheets("Bestelde Artikelen").Range("M2").Value + 1
Call M_ClearAll2.ClearAll2
Unload Me
End Sub
Display More
When I put the code line (with Range (M2) before "Next r", the code puts the order umber behind every line of the order, that is not what I want. The order should only get one order number.
It doesn't matter where I put the line for the order number, it is always wrong. Could somebody help me please?
Thanks in advance.
Display MoreHi AL,
It is related to the way you are filling the listbox in the refresh sub, if you exclude the refresh from the form initialise the problem does not seem to occur.
I am not sure how the .rowsource command works, change it to .additem or .list
Regards
Justin
Justin,
Thank you for your answer. You were right. I used the .list command and excel did NOT restart after adding a product.
So your answer solved the problem for me, thanks a lot.
Regards,
Bart
When I click on the commandbutton (cmdArtToevoegen) the code does add the new articles to my sheet and shows me the form (added, in stead of MsgBox) correctly.
But my listbox (lstboxArt) on the form (frmArtikels) is acting strange (shows only 3 or 4 entries) and reboots excel.
What is wrong with my code?
Private Sub cmdArtToevoegen_Click()
'*********************************************************************
'Deze code zoekt het laatste getal beginnend met 1, 2, 3 enz ...
'Het voegt automatisch een volgend nummer in
'als het laatste getal 1078 is, dan zal het een nieuwe "row" invoegen met nummer 1079
'*************************************************************************
On Error Resume Next
If optBrood.Value = False And optBroodDiepvries.Value = False And optBoterkoeken.Value = False _
And optPatisserie.Value = False And optTraiteur.Value = False Then
MsgBox "Gelieve een afdeling aan te duiden!", vbCritical, "Afdeling aanduiden"
Exit Sub
End If
If txtArtBenaming.Value = "" Then
MsgBox "Gelieve een artikelbenaming in te vullen!", vbCritical, "Afdeling aanduiden"
Exit Sub
End If
'*************************************************************************
Dim X As Variant
Dim i As Variant
If optBrood.Value = True Then
With Sheets("Producten_nieuw")
X = .Cells(2).CurrentRegion
For i = 2 To UBound(X)
If X(i, 2) > 1999 Then Exit For
Next
.Rows(i).Insert
.Cells(i, 2) = X(i - 1, 2) + 1 'kolom "B" (cells (i, 2)
.Cells(i, 3) = txtArtBenaming.Value 'kolom "C" (cells (i, 3)
.Cells(i, 4) = "Brood" 'kolom "D" (cells (i, 4)
End With
ElseIf optBroodDiepvries.Value = True Then
With Sheets("Producten_nieuw")
X = .Cells(2).CurrentRegion
For i = 2 To UBound(X)
If X(i, 2) > 2999 Then Exit For
Next
.Rows(i).Insert
.Cells(i, 2) = X(i - 1, 2) + 1
.Cells(i, 3) = txtArtBenaming.Value
.Cells(i, 4) = "Brood diepvries"
End With
ElseIf optBoterkoeken.Value = True Then
With Sheets("Producten_nieuw")
X = .Cells(2).CurrentRegion
For i = 2 To UBound(X)
If X(i, 2) > 3999 Then Exit For
Next
.Rows(i).Insert
.Cells(i, 2) = X(i - 1, 2) + 1
.Cells(i, 3) = txtArtBenaming.Value
.Cells(i, 4) = "Boterkoeken"
End With
ElseIf optPatisserie.Value = True Then
With Sheets("Producten_nieuw")
X = .Cells(2).CurrentRegion
For i = 2 To UBound(X)
If X(i, 2) > 4999 Then Exit For
Next
.Rows(i).Insert
.Cells(i, 2) = X(i - 1, 2) + 1
.Cells(i, 3) = txtArtBenaming.Value
.Cells(i, 4) = "Patisserie"
End With
ElseIf optTraiteur.Value = True Then
With Sheets("Producten_nieuw")
X = .Cells(2).CurrentRegion
For i = 2 To UBound(X)
If X(i, 2) > 5999 Then Exit For
Next
.Rows(i).Insert
.Cells(i, 2) = X(i - 1, 2) + 1
.Cells(i, 3) = txtArtBenaming.Value
.Cells(i, 4) = "Traiteur"
End With
End If
'****************************************************
frmArtToegevoegd.Show 'msgbox Art added
Call M_ClearAll.ClearAll
End Sub
Display More
Thx in advance for any help.
Hello JD,
Thanks for your help, much appreciated!
The problem is solved. Thanks you so much.
And thanks for the tips regarding my code.
I still have a lot to learn!
I will try to change my WITH statments as explain by you. Don't know if I will succeed but I will give it a try
What do you mean by:
QuoteI suggest rewriting the code so that very little of it runs from the form code page, rather have code written in the modules and called from the form page. This generally allows you to write code that is more flexible and can be called from multiple locations.
Could you give me an example?
Thanks in advance
Hi JD,
Thanks for wanting to help me! Much appreciated.
I have found some solutions like detecting if the number if higher than the highest number in a row beginning with a certain number (1,2,3,4,5).
I have a new problem now, If a user enters a number higher than the highest number in the row, a MsgBox should appear, saying that the number entered is to high and after clicking "OK" on the MsgBox, the backgroundcolor of the TextBox should be a different color (reddish), the value that was entered (number to high) should be selected, so that the user can re-enter a valid number without first deleting the wrong-entered number.
- Detecting the number works fine
- Changing the backgroundcolor works fine
- selecting the wrong-answered number works fine
It seems not possible to select the TextBox after clicking OK on the MsgBox without hiding the main UserForm, showing the MsgBox, clicking OK and re-show the main UserForm. I have create my own MsgBox, but that didn't help.
When I enter a valid number, the ListBox shows the correct filter, but the backgroundcolor stays modified and I cannot enter the TextBox again by clicking on it.
I would like that when a wrong number is entered, a MsgBox (self-made or not) appears, telling the entered number was wrong, click OK on the MsgBox and the "wrong-entered value in the TextBox is selected, and the user can re-enter a correct number (while value is selected, the value is automatically deleted when user enters a new value).
Any Help would be appreciated. Thx in advance.
Here is the code that doesn' do as I want (It isn't cleaned yet but it gives an idea:
CellValue1 = Sheets("Producten_nieuw").Range("K5")
CellValue2 = Sheets("Producten_nieuw").Range("K6")
CellValue3 = Sheets("Producten_nieuw").Range("K7")
CellValue4 = Sheets("Producten_nieuw").Range("K8")
CellValue5 = Sheets("Producten_nieuw").Range("K9")
'Set LastCell = Sheets("Producten_nieuw").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
' LastCell.Select
'Set LastCell = LastCell.Offset(0, -2)
'Set LastCell = Range("A" & Rows.Count).End(xlUp) 'second way to determine value of last cell in column "A"
If txtZoekOpArtNr.Value = "" Then
Me.lstboxArtGevonden.Clear
Exit Sub
End If
Const PatternFilter As String = "*[!0-9]*"
Const MaxLen As Long = 4
With txtZoekOpArtNr
If .Text Like PatternFilter Or Len(.Text) <> MaxLen Then
txtZoekOpArtNr.BackColor = &HC0C0FF
MsgBox "Het artikelnummer moet 4 cijfers bevatten!", vbCritical, "Niet meer dan 4 cijfers!"
GoTo focus
Else
If txtZoekOpArtNr > CellValue1 And txtZoekOpArtNr < 2000 Then
txtZoekOpArtNr.BackColor = &HC0C0FF
frmArtikels.Hide 'if form is not hidden, the focus will not set in txtZoekopArtNr
' In stead of msgbox, I call userform "frmBroodgetaltegroot"
' MsgBox "In de afdeling 'Brood' is het laatste artikelnummer " & CellValue1 & "." & vbNewLine & _
' "Het getal mag dus niet groter zijn dan " & CellValue1 & "!!", vbCritical, "Getal is groter dan " & CellValue1 & "!"
frmBroodGetalteGroot.lblGetal = CellValue1 'Putting the max Nr in teh userform
frmBroodGetalteGroot.lblGetal2 = CellValue1 'Putting for the second time the max Nr in teh userform
frmBroodGetalteGroot.Caption = "Getal is groter dan " & CellValue1 & "!" 'Putting the title of the header of the userform with Max nr
frmBroodGetalteGroot.Show 'show the error userform in stead of msgbox
frmArtikels.txtZoekOpArtNr.SetFocus ' setting focus on the textbox
' frmArtikels.txtZoekOpArtNr.SelStart = 0 'selecting value in textbox
frmArtikels.txtZoekOpArtNr.SelLength = Len(.Text) 'selecting value in textbox
frmArtikels.Show 'By clicking ok on the userform frmBroodGetalteGroot, the main userform is shown again
txtZoekOpArtNr.BackColor = &HC0FFFF 'setting back the original color background
' txtZoekOpArtNr.Value = ""
lstboxArtGevonden.Clear 'making sure the listbox is cleared
Exit Sub
Display More
In attachment my complete workbook.
I have a column A, containing 4 digits numbers all starting with 1, 2, 3, 4 or 5.
The starting number represents departments. So there are 5 departments.
Every department contains different items. So the numbers go for example from 1001 to 1067, from 2001 to 2134, and so on. The item numbers are stored in column A, the items in column B and the name of the department in column C.
I have a combobox on a userform. This userform is filled with all the Numbers from column A with this code:
The user can pick a number from the combobox, but he can also enter a number manually.
After picking a number from the combobox, a listbox is filled with the three columns.
When the user enter a number that doesn't exists, i get an error.
When the user enters a number starting with a "1" that is higher than the highest number in the column starting with a "1", I get an error.
This is my code that isn't working:
Private Sub cboZoekProduct_AfterUpdate()
' Is het getal groter dan het hoogste cijfer beginnend met 1, 2, 3, 4 of 5
Dim rij1 As Long
Dim rij2 As Long
Dim rij3 As Long
Dim rij4 As Long
Dim found1 As Range
Dim found2 As Range
Dim found3 As Range
Dim found4 As Range
Dim CellValue1 As String
Dim CellValue2 As String
Dim CellValue3 As String
Dim CellValue4 As String
Set found1 = Sheets("Producten_nieuw").Columns("A").Find(what:="2***", LookIn:=xlValues, lookat:=xlWhole)
rij1 = found1.Row - 1
CellValue1 = Range("A" & rij1).Value
If cboZoekProduct > CellValue1 Then MsgBox "Het ingegeven artikelnummer is groter dan " & CellValue1 End If
Set found2 = Sheets("Producten_nieuw").Columns("A").Find(what:="3***", LookIn:=xlValues, lookat:=xlWhole)
rij2 = found2.Row - 1
CellValue2 = Range("A" & rij2).Value
If cboZoekProduct > CellValue2 Then MsgBox "Het ingegeven artikelnummer is groter dan " & CellValue2 End If
Set found3 = Sheets("Producten_nieuw").Columns("A").Find(what:="4***", LookIn:=xlValues, lookat:=xlWhole)
rij = found3.Row - 1
CellValue3 = Range("A" & rij3).Value
If cboZoekProduct > CellValue3 Then MsgBox "Het ingegeven artikelnummer is groter dan " & CellValue3 End If
Set found4 = Sheets("Producten_nieuw").Columns("A").Find(what:="5***", LookIn:=xlValues, lookat:=xlWhole)
rij = found4.Row - 1
CellValue4 = Range("A" & rij4).Value
If cboZoekProduct > CellValue4 Then MsgBox "Het ingegeven artikelnummer is groter dan " & CellValue4 End If
'If cboZoekProduct.Value > CellValue1 Then
' MsgBox "Het getal dat je ingegeven hebt werd niet gevonden!", vbCritical, "Niet gevonden!"
' Else
'Set found = Sheets("Producten_nieuw").Columns("A").Find(what:="2***", LookIn:=xlValues, lookat:=xlWhole)
' If found Is Nothing Then
' MsgBox "Not found"
'Else
' MsgBox "Found on row " & found.Row
' End If
'rij = found.Row - 1
'MsgBox (rij)
'CellValue11 = Range("A" & rij).Value
'MsgBox "Het laatste artikelnummer beginnend met 1 = " & CellValue
'If cboZoekProduct.Value > CellValue1 Then
' MsgBox "Het getal dat je ingegeven hebt werd niet gevonden!", vbCritical, "Niet gevonden!"
'
' End If
Dim LastPosition As Long
Const PatternFilter As String = "*[!0-9]*"
Const MaxLen As Long = 4
' If you do not want to restrict the number of characters,
' simply set the MaxLen to a huge number like (2^31-1)
Static LastText As String Static SecondTime As Boolean If Not SecondTime Then With cboZoekProduct If .Text Like PatternFilter Or Len(.Text) > MaxLen Then Beep MsgBox "Je kan niet meer dan 4 cijfers ingeven!", vbCritical, "Niet meer dan 4 cijfers!" SecondTime = True .Text = LastText .SelStart = LastPosition Else LastText = .Text End If End With End If SecondTime = False
'Place any other TextBox1_Change event code here
'************************************************************************************************
If cboZoekProduct.Value <> "" Then
' txtArtikelnummer.Value = [tabel3515].Find(cboZoekProduct, , xlValues).Offset(0, 0)
' txtArtikelbenaming.Value = [tabel3515].Find(cboZoekProduct, , xlValues).Offset(0, 1)
' txtAfdeling.Value = [tabel3515].Find(cboZoekProduct, , xlValues).Offset(0, 2)
' TextBox1.Value = [tabel3515].Find(cboZoekProduct, , xlValues).Offset(0, 0)
' lblAfdeling.Visible = True
End If
If txtAfdeling.Value = "Brood" Then optBrood.Value = True lblAfdeling.Caption = txtAfdeling.Value
ElseIf txtAfdeling.Value = "Brood diepvries" Then optBroodDiepvries.Value = True lblAfdeling.Caption = txtAfdeling.Value
ElseIf txtAfdeling.Value = "Boterkoeken" Then optBoterkoeken.Value = True lblAfdeling.Caption = txtAfdeling.Value
ElseIf txtAfdeling.Value = "Patisserie" Then optPatisserie.Value = True lblAfdeling.Caption = txtAfdeling.Value
ElseIf txtAfdeling.Value = "Traiteur" Then optTraiteur.Value = True lblAfdeling.Caption = txtAfdeling.Value
End If
End Sub
Display More
What is going wrong? Any help would be much appreciated!
Also the code is put in the afterupdate event, I would like it better in the change event, but I get an error after entering the first digit in the combobox.
Thx in advance