I have a code snippet which tells me in which row a specific value is
With Tabelle1
Set pzrow = .Cells.Find(What:=pznr, LookIn:=xlValues, searchorder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, searchformat:=False) ' sucht die Nummer der Zeile raus
End With
The value can differ between 1 and 29000.
Just for cosmetic reasons I want to display the row in a Label caption:
pzrow1.Caption = "Eintrag Nr. " & pzrow.Row
and this should show "Eintrag Nr. 00001" for example, not "Eintrag Nr. 1"
Some Ideas?
You can use Format$() to specify how many digits:
pzrow1.Caption = "Eintrag Nr. " & Format$(pzrow.Row, "00000")
Related
I have a macro written to convert customer names and titles to proper case if they aren't already. The problem with that is I sometimes have a list with abbreviations or acronyms (ie "High School" = "HS" ; Limited Partnership = "LP").
The issue is that my macro will proper case what should remain in upper case. To work around this I have a few exceptions written into my macro. Here's an example:
Dim fndList As Variant
Dim rplcList As Variant
Dim F As Long
fndList = Array("'S", "Xdock", "Llc", "Us ", "Urs", "Lc ", "Bbq", "Dq")
rplcList = Array("'s", "XDock", "LLC", "US ", "URS", "LC ", "BBQ", "DQ")
For F = 0 To UBound(fndList)
Selection.Replace What:=fndList(F), Replacement:=rplcList(F), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Is there a way to write a rule that recognizes if the term is not a word then it defaults to upper case?
The other issue I have with my temporary solution is that some words containing the array will revert to upper case (ie: "Famous" = "FamoUS" ; "Alcohol" = "ALCohol" ; "Yourselves" = `YoURSelves").
Appreciate any help, pleae let me know if you require more info on my part.
I have been trying to fix a inventory log for my work that has been around for years and has always had this bug. The script is supposed to look at a provided tag number, figure out if it has been returned or not and then mark the item as returned if the name matches. For tag numbers 1501 through 2040 it works perfectly fine, however once you attempt to search tag numbers 2041 to 2300 it will immediately throw a Code 5 error and crash the script. Debug says the range = nothing
here is the main code:
Dim AmbRng As Range
Dim answer As VbMsgBoxResult
TheDate = Format(Date, "mm/dd/yy")
TheTime = Format(Time, "Medium Time")
findstring = ByItemNumberTextBox.Value
If Trim(findstring) <> "" And Trim(findstring) >= 1501 And Trim(findstring) <= 2300 Then
With Sheets("AmbBay").Range("AmbBay_Ticket_Numbers")
Set AmbRng = .Find(What:=findstring, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
Application.Goto AmbRng, True
The debug always points to the last line and says AmbRng = nothing. I presume this is an integer overflow but I haven't been able to find a workaround through google or here. My VBA skills are rudimentary and self taught, so I assume I am missing something simple. Any help would be appreciated.
GMalc answered the question in the comments, I appreciate everyone's help.
I don't know how to explain this better, but I extract a report in xls format, that record the offer from companys during a bid. The report contains three fields:
Value / Company / Time
I want to extract the first offer from a company, but I'm facing the following problem:
The report don't consider days just time, so if a company send me an offer in 2019-01-28 at 10 PM and after this send me another offer in 2019-01-29 at 09 PM, in my report will appear the second offer first.
I've been doing this manually cuz it's impossible to use vlookup in this case.
I tried with VBA too, something like this To define the range where the data is:
tableRowi = tableI.Row 'Linha de início da Tabela de Lances
Set tableF = Range("A1:Z1000").Find(What:="Relatório de Chat do Leilão", LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)
Set tableF2 = Range("A1:Z1000").Find(What:="Usuários Conectados ao Monitor do Leilão", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)
If tableF Is Nothing Then
tableRowF = tableF2.Row
Else
tableRowF = tableF.Row
End If
and this to copy the data that i want:
Set propI = Range("A" & tableRowi - 2 & ":Z1000").Find(What:="Valor", LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 'Início da tabela de classificação
propCol = propI.Column 'Coluna com valores das propostas
Set empI = Range("A" & tableRowi - 2 & ":Z1000").Find(What:="Empresa", LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 'Início da tabela de classificação
empCol = empI.Column 'Coluna com nomes das empresas que deram lances
'Copiando os dados
Dim arrayLance() As Variant
ReDim arrayLance(0 To nEmpresas - 1) 'Propostas iniciais
For i = 0 To nEmpresas - 1
'Set Emp = Range("A1:Z1000").Find(What:=arrayEmp(i), LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 'Início da tabela de classificação
arrayLance(i) = WorksheetFunction.Index(Range("A1:Z1000"), tableRowi - 1 + WorksheetFunction.Match(arrayEmp(i), Range(Cells(tableRowi, empCol), Cells(tableRowF, empCol)), 0), propCol)
Next i
In this code I set the columns where the data is and copy them using arrays, but again I face the same problem, the code pickup the first information (value) from an Company which s is not necessarily your first offer.
I thought to use a loop but i cant structure this in my mind.
I have a With block that has a condition to see if a value exists in the drop down list.
With wb.Sheets("BudgetLines").DropDowns("Drop Down 22")
For Each c In refData.Range("G7:G" & LastRow_RefData).Cells
Set Findo = wb.Sheets("BudgetLines").Cells.Find(c.Value, LookIn:=xlValues, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
MatchCase:=False, SearchFormat:=False)
If Findo Is Nothing Then
Debug.Print "Name was not found."
'DropDowns("Drop Down 22").Clear
Else
Debug.Print "Name found in :" & Findo.Address
'Add title to drop down box
.AddItem c.Value
End If
Next
End With
However, I want to clear the dropdown list completely. I have tried .Clear but it doesn't seem to pick up as this is in the With block.
Is there a way to do this within the With block?
You need the RemoveAllItems function
Dim dd As DropDown
Set dd = wb.Sheets("BudgetLines").DropDowns("Drop Down 22")
With dd
.RemoveAllItems
End With
The trick to seeing this was declaring a variable Dim dd As DropDown, which let me see the RemoveAllItems function when I ..
hi why do i get the runtime error 13: type mismatch error
while running the following code
Application.Goto Reference:="R1C1:R232C221"
Selection.FormulaArray = "=ROUND(a(),0)"
Selection.Replace What:="a()", Replacement:="IF(IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)+IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)=2,0,IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)+IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0,Sheet4!A1:HM232+Sheet5!A1:HM232))", LookAt _
:=xlPart, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Range("I9").Select
1) i know that formulaarray should be in R1C1 style... but A1 style is not required, it also works without any problems in A1 style
http://msdn.microsoft.com/en-us/library/bb208529.aspx
2) i found this way of writing from
http://www.dailydoseofexcel.com/archives/2005/01/10/entering-long-array-formulas-in-vba/
Let's analyze your Replacement:
Replacement:="IF(IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)+IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)=2,0,IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)+IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0,Sheet4!A1:HM232+Sheet5!A1:HM232))"
(1) change Sheet4!A1:HM232+Sheet5!A1:HM232 to X:
Replacement:="IF(IF(X=2,0," & _
"X)+IF(X=2,0," & _
"X)=2,0,IF(X=2,0," & _
"X)+IF(X=2,0,X))"
(2) glue the broken pieces back together:
Replacement:="IF(IF(X=2,0,X)+IF(X=2,0,X)=2,0,IF(X=2,0,X)+IF(X=2,0,X))"
(3) change IF(X=2,0,X) to Y:
Replacement:="IF(Y+Y=2,0,Y+Y)"
(4) some evaluation:
If X is 2, Y is 0, and the result is 0.
If X is 1, Y is 1, and the result is
0.
If X is anything else, Y is X, and the
result is 2*X.
(5) so the formula is equivalent to:
Replacement:="IF(OR(X=2,X=1),0,2*X)"
(6) so the next step would be to replace X (step 1 in reverse) ...
I'll just leave you with a question or two: How did it become so messy? Have you heard of "DRY" (Don't Repeat Yourself)?
I believe the "replacement" argument is too long. You need to keep that string under 255 characters. Put more of the formula into the Selection.FormulaArray line so that the replacement is less than 255 characters.