I would like to solve the following testing problem. If there is an error i.e. (=1/0) in a cell, the additional tests will not run after that. I would like to know how to solve this issue in the testing primary task part.
Private Sub ts_NoBlank(TsCn, STST, LsSt, LsIn, TsRw, MsCo, MsIC, MsSt)
Dim TsCl, StRw, LsRw, TsSh
TsCl = ColNrOfField(TsCn)
StRw = FsRwOfField(TsCn) + 1
LsRw = LsRwOfField(TsCn)
TsSh = SheetOfField(TsCn)
' Setting up Status Updates
PLcSt = -1 'Starting previous local status is set to -100% to show eitherways
rws = LsRw - StRw
'this part has to be copied to whenever showing the status (Value of LcSt has to be added each time by function)
LcSt = 0
If LcSt - PLcSt >= LsIn Then
Call ShowStatus(MsSt & " (" & Application.WorksheetFunction.Text(LcSt, "0%") & ")", ThisWorkbook.Worksheets("Calculations Running").Range("Started"), STST + (LsSt - STST) * LcSt)
PLcSt = LcSt
End If
' Testing primary task
ErrNr = 0
For Rw = StRw To LsRw 'ToDo speed up with fromrow torow
If Len(ThisWorkbook.Sheets(TsSh).Cells(Rw, TsCl)) = 0 Then
ThisWorkbook.Sheets(TsSh).Cells(Rw, TsCl).Interior.ColorIndex = 46
ErrNr = ErrNr + 1
End If
'Showing Local Status
LcSt = 0.1 + ((Rw / LsRw) * 0.75)
If LcSt - PLcSt >= LsIn Then
Call ShowStatus(MsSt & " (" & Application.WorksheetFunction.Text(LcSt, "0%") & ")", ThisWorkbook.Worksheets("Calculations Running").Range("Started"), STST + (LsSt - STST) * LcSt)
PLcSt = LcSt
End If
Next Rw
' Updating Test Results on 'Testing' Page
TRRg = "test" & TsRw & "_results"
If ErrNr = 0 Then 'If Results are Positive
ThisWorkbook.Worksheets("Testing").Range(TRRg) = MsCo
ThisWorkbook.Worksheets("Testing").Range(TRRg).Interior.ColorIndex = 51
Else 'If Errors are found
ErrMS = NumberizeMessage(MsIC, "SP_textvers", "S_textvers", "P_textvers", ErrNr)
ThisWorkbook.Worksheets("Testing").Range(TRRg) = ErrMS
ThisWorkbook.Worksheets("Testing").Range(TRRg).Interior.ColorIndex = 3
End If
'Showing Local Status
LcSt = 0.1
If LcSt - PLcSt >= LsIn Then
Call ShowStatus(MsSt & " (" & Application.WorksheetFunction.Text(LcSt, "0%") & ")", ThisWorkbook.Worksheets("Calculations Running").Range("Started"), STST + (LsSt - STST) * LcSt)
PLcSt = LcSt
End If
' Highlighting erroneous cells (if not done during testing)
If ErrNr > 0 Then
End If
'Showing Local Status 100%
LcSt = 1
If LcSt - PLcSt >= LsIn Then
Call ShowStatus(MsSt & " (" & Application.WorksheetFunction.Text(LcSt, "0%") & ")", ThisWorkbook.Worksheets("Calculations Running").Range("Started"), STST + (LsSt - STST) * LcSt)
PLcSt = LcSt
End If
End Sub
Since you indicate that the ' Testing primary task section seems to work, but nothing after that seems to execute, I'd suggest that when you get here:
'Showing Local Status
LcSt = 0.1
If LcSt - PLcSt >= LsIn Then
this condition is false, so the code is never executed.
Then you get here:
' Highlighting erroneous cells (if not done during testing)
If ErrNr > 0 Then
End If
and there is no code to execute, no matter the value of ErrNr.
Then you get here:
'Showing Local Status 100%
LcSt = 1
If LcSt - PLcSt >= LsIn Then
and it's still evaluating False, so again it never executes.
Have you gone through in the debugger to look at the values and ensure everything is as you expect it to be?
Related
I have an Excel file that contains some datas that I want to export into an Access db. In the C column I've got a field called 'Description'. Usually this field occupy just one cell, but it can happens that is more long.
In this case, for example, AP.01 has got 5 rows of description. How can update the first row with the next rows?
Public Sub updateDB(ByVal PathDB As String, str As String, id As Integer)
Dim db As New cDB
Dim v As New cVoce
Dim rs As ADODB.Recordset = db.RecordSet
v.Description = str
db.connetti_DB(PathDB)
db.get_rs("UPDATE Voice SET Description = '" + v.Description + "' WHERE id= '"+id+"'")
End Sub
Public Function get_rs(ByVal query As String) As ADODB.Recordset
If db Is Nothing Then rs = Nothing : Return rs
rs = New ADODB.Recordset
rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic
rs.LockType = ADODB.LockTypeEnum.adLockOptimistic
rs.Open(query, db)
Return rs
End Function
This code doesn't work because I update my current row, for this reason is useless the UPDATE instruction. How can I fix my code?
EDIT I post here the For loop
For r = 2 To grid.RowCount - 1
vett = Split(grid(r, 1).Text)
total = UBound(Split(grid(r, 1).Text, "."))
If grid(r, 1).Text <> "" Then
Select Case total
Case 0
Dim chapter As New cChapter
flag = 1
id = id + 1
chapter.Cod = grid(r, 1).Text.Substring(0, 1)
chapter.Description = grid(r, 3).Text
If Left(vett(0), 1) >= Chr(65) And Left(vett(0), 1) <= Chr(90) Then
chapter.Cod = Left(vett(0), 1)
oldChap = chap.Cod
If chapter.Cod <> oldCap Then
chapters.Add(chapter)
End If
End If
chapters.Add(chapter)
stringChap = chap.Description
Dim par As New cParagraph
If Left(vett(0), 2) >= Chr(65) And Left(vett(0), 2) <= Chr(90) Then
par.Cod = Left(vett(0), 2)
par.Cod_Chapter = Left(vett(0), 1)
oldPar = par.Cod
If par.Cod <> oldPar Then
paragraphs.Add(par)
End If
End If
If grid(r, 3).Text.Length > 255 Then
par.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
par.Description = grid(r, 3).Text.ToString
End If
paragraphs.Add(par)
stringPar = par.Description
Case 1
flag = 2
id = id + 1
c_Voc = voc.Cod_Chapter
p_Voc = voc.Cod_Paragraph
voc.Cod_Chapter = grid(r, 1).Text.Substring(0, 1)
voc.Cod_Paragraph = grid(r, 1).Text.Split(".")(0)
voc.Cod_Voice = Right(vett(0), 2)
If grid(r, 3).Text.Length > 255 Then
voc.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
voc.Description = grid(r, 3).Text.ToString
If voc.Description.EndsWith("-") Then
a = Replace(voc.Description, "-", "")
voc.Description = a
End If
End If
stringVoice = voc.Description
voices.Add(voc)
voices.Save_DB(dbDest)
Case 2
flag = 3
id = id + 1
sVoice = New cVoice
oldSvoice = voice.Cod_SVoice
sVoice.Cod_SVoice = Left(vett(0), 2)
If sVoice.Cod_SVoce <> oldSvoice Then
voices.Add(sVoice)
voices.Save_DB(dbDest)
End If
If grid(r, 3).Text.Length > 255 Then
sVoice.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
sVoice.Description = grid(r, 3).Text
End If
stringSvoice = sVoice.Description
sVoice.Cod_Voce = Left(vett(0), 5)
sVoice.Price1 = grid(r, 12).Text
sVoice.Price2 = sVoice.Price1
sVoice.UniMi = grid(r, 11).Text
sVoce.Sep = "."
voices.Add(sVoce)
voices.Save_DB(dbDest)
End Select
Else
If flag = 1 Then
stringChap = grid(r, 3).Text
chap.Description = stringChap & grid(r, 3).Text
stringPar = grid(r, 3).Text
paragraph.Description = stringPar & grid(r, 3).Text
End If
If flag = 2 Then
stringVoice = grid(r, 3).Text
voc.Description = voc.Description & stringVoice
voices.updateDB(dbDest, stringVoice, id)
voices.Add(voc)
End If
If flag = 3 Then
stringSvoice = grid(r, 3).Text
sVoice.Description = stringSvoice & grid(r, 3).Text
voices.Add(sVoice)
End If
chapter.Save_DB(dbDest)
paragraph.Save_DB(dbDest)
voice.Save_DB(dbDest)
End If
Next
EDIT2 I declared id As Integer and when Code column has a value then id=id+1. In this way I always know which row I have to modify. I modified also updateDB (now I'm using 3 parameters) and I added a WHERE condition into my query. Despite the update, nothing has changed
In database you cannot store records without PrimaryKey (actually you can, but it is bad idea). Since in your solution id is in fact Excel row number (sorry if I'm not correct but it looks like from code) it could be very hard to maintain it in future (in case someone add or remove description row). It would be better to change id column to text and use code as PK.
Storing description then could be solved in 2 ways:
1) Concatenate all rows containing description into 1 variable adding vbNewLine in between and store it in description field.
2) More relational but also more complex - create 2nd table for description with PK as i.e. autonumber, ForeignKey Code referring to main table. Maintenance will be here very complex. Not really worth effort.
Amount of changes in code is quite big, so sorry I'll not provide fixed code but I hope idea is clear.
BTW: The reason why description is not updated is described in your post. You are increasing id only when code is present, so every description field from first group have id = 1. The most simple fix in your code would be to create 2 update statements - One for rows with code
UPDATE Voice SET Description = '" + v.Description + "' WHERE id= '"+id+"'
Second one for rows without code:
UPDATE Voice SET Description = Description + CHAR(13) + CHAR(10) + '" + v.Description + "' WHERE id= '"+id+"'
I was forced by college to do a application in Visual Studio (Basic) and I never had expierence with this language before so I'm confused. I did If statement which is working and then I've added EleseIf which is also working but after I've added Second ElseIf it doesn't work. It seems like only If statement and the first ElseIf is working for me but I need more than just 1 ElseIf statement.
Public Class receipt
Private Sub receipt_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim outputValue As Decimal = My.Settings.outputamount.Remove(4)
Dim calculation As Decimal = outputValue * My.Settings.inputamount
Dim totalwithoutcharge As String = calculation.ToString
customername.Text = "Name: " + My.Settings.Username
Label6.Text = "Entered Money: " + My.Settings.inputamount + " " + My.Settings.currency
Label7.Text = "Converted To: " + totalwithoutcharge + " " + My.Settings.outputcurrency
If calculation < 100 Then
Label8.Text = "Charge: 0%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 100 Then
Label8.Text = "Charge: 1%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 500 Then
Label8.Text = "Charge: 2%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 1000 Then
Label8.Text = "Charge: 3%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
Else
Label8.Text = "Something went wrong"
End If
End Sub
End Class
I've entered many values I've add over 600 and it was still showing 'Charge: 1%' where it should show 2% instead. The same happen with values higher than 1000 it will still show the 1%. But when the value is below 100 then is OK and it is showing 0%.
Over 100 works fine too
Over 500 doesn't work
I've also tried to do it with two conditions where it would look at range but it didn't work too.
I've tried 'ElseIf calculation > 100 And calculation > 500 Then' but there is no difference.
First of all use & instead of + when concentating strings.
Second if calculation is > 500 it is also greater than 100, 200 etc. So you need to implement a range. So think about the logic.
Based on my comment
If calculation >= 1000 Then
ElseIf calculation >= 500 Then
ElseIf calculation >= 100 Then
Else
'lass than 100
End If
I have a problem with calculating difference between 2 dates where first is older than second.
For example: I want to find difference between
5.5.2015 and 1.11.2014
I used function
=IF((A(DATEDIF(B12,$W$3,"M")<=12,RANK(Q12,Q:Q)<=11)),Q12;0)
but the function is limited only to situations where the second date is higher than the first one.
I want to know whether B12 is within last 12 months from given date. If it is, then I want to calculate with it.
Is there any way to calculate backwards in excel or VBA?
Thank you.
I know this is an old post already but for anyone who needs this...
Function FindDateDiff(myDate1 As Date, myDate2 As Date) As String
Dim myYears As Long, myMonths As Long, myDays As Long
Dim yearString As String, monthString As String, dayString As String, FinalString As String
If myDate1 > myDate2 Then
myYears = Year(myDate1) - Year(myDate2)
myMonths = Month(myDate1) - Month(myDate2)
myDays = Day(myDate1) - Day(myDate2)
If myDays < 0 Then
myMonths = myMonths - 1
myDays = Day(WorksheetFunction.EoMonth(myDate1, 0)) - Abs(myDays) - 1
End If
Else
myYears = Year(myDate2) - Year(myDate1)
myMonths = Month(myDate2) - Month(myDate1)
myDays = Day(myDate2) - Day(myDate1)
If myDays < 0 Then
myMonths = myMonths - 1
myDays = Day(WorksheetFunction.EoMonth(myDate2, 0)) - Abs(myDays) - 1
End If
End If
If myMonths < 0 Then
myYears = myYears - 1
myMonths = 12 - Abs(myMonths)
End If
If myYears = 0 Then
yearString = ""
ElseIf myYears = 1 Then
yearString = myYears & " year, "
ElseIf myYears > 1 Then
yearString = myYears & " years, "
End If
If myMonths = 0 Then
monthString = ""
ElseIf myMonths = 1 Then
monthString = myMonths & " month, "
ElseIf myMonths > 1 Then
monthString = myMonths & " months, "
End If
If myDays = 0 Then
dayString = ""
ElseIf myDays = 1 Then
dayString = myDays & " day"
ElseIf myDays > 1 Then
dayString = myDays & " days"
End If
FinalString = yearString & monthString & dayString
If Right(FinalString, 2) = ", " Then FinalString = Left(FinalString, Len(FinalString) - 2)
FindDateDiff= FinalString
End Function
Just paste this function in a new module in the workbook and you can start calling this function. '=FindDateDiff(A1,B1)'
This function only require 2 dates as arguments and the order doesn't matter.
I've tested this function with both UK and US format, both works exactly the same.
I used DateDiff before, but the calculation for days and months returns an incorrect value and could be very confuse sometimes.
In VBA use the same function.
NoOfDays = DateDiff("D", DATE1, DATE2)
NoOfDays returns either positive or negative value depending on the dates
I have it solved by using ISERROR
=IF(ISERROR(DATEDIF(RC[-16],R3C23,""M"")<=12),0,RC[-1])
I am currently programming a sheet which visualizes data sets in graphs. Because the user of this sheet will not need all the graphs, I would like to let them choose the ones needed through a UserForm. Since the amount of data sets is variable, the UserForm will have the same amount of checkboxes as there are datasets.
The Userform code is as follows.
Private Sub UserForm_Initialize()
Dim chkBoxA As MSForms.CheckBox
Dim chkBoxB As MSForms.CheckBox
Dim lblBox As MSForms.Label
Dim cnt As Control
Amount = Sheet4.Range("C4").Value 'Amount of datasets
For i = 1 To Amount
Set lblBox = Me.Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = Me.Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = Me.Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
CommandButton1.Left = 20
CommandButton1.Top = 40 + ((Amount - 1) * 40)
CommandButton1.TabIndex = Amount * 3 + 1
Me.Height = 220
Me.ScrollBars = fmScrollBarsVertical
Me.ScrollWidth = Me.InsideWidth * 9
For Each cnt In Me.Controls
If cnt.Top + cnt.Height > Me.ScrollHeight Then
Me.ScrollHeight = cnt.Top + cnt.Height + 5
End If
Next
End Sub
When the UserForm is filled in (graphs are chosen by clicking on the options), the user will press CommandButton1. An event should then be run to show the correct graph, but for the simplicity I am first testing if a MsgBox will show up. Unfortunately the MsgBox does not show up.
Private Sub CommandButton1_Click()
'Will fix this with a loop
If A1 = True Then
MsgBox ("TestA1")
End If
If B1 = True then
MsgBox ("TestB1")
End If
If A2 = True then
MsgBox ("TestA2")
End If
Unload Me
End Sub
I am stuck on this part. The checkboxes do show up on the UserForm and they are clickable, but the commandbutton only shuts down the sub (Unload Me). I would like to see the MsgBox show up when I select the corresponding option and click the commandbutton. Any help on getting this to work is appreciated!
You are referencing 'A1' in the sub, but that variable does not exitst at compile time, because you add them dynamically. What you need to do is loop the controls, to check the names. Best practice is to put the checkboxes in a frame, to be able to group them.
Add a frame to the userform and name it 'checkboxframe'
And then instead of:
For i = 1 To Amount
Set lblBox = Me.Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = Me.Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = Me.Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
you would need to do:
With Me.checkboxframe
For i = 1 To Amount
Set lblBox = .Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = .Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = .Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
End With
And to add the checkboxes to the frame, use something like:
For Each ctr In UserForm1.frame("checkboxframe").Controls
If TypeName(ctr) = "CheckBox" Then
If ctr.Value = True Then
'do something usefull here
msgbox ctr.name
End If
End If
Next ctr
The reason nothing appears is because there is no object "A1" manually defined as a variable.
To get the value of the box you Dynamically named "A1" you would have to refer to it as such:
If Me.Controls.Item("A1").Value = True then
Hope this helps!
I'm trying to convert the numerical result into words.
The sample I found works well for English words, but as I change them to my language, bunch of superficial problems came out.
What I need help in particular with is building the same code for 100 as its for 10. The reason I am doing this is because in my language we don't have One standing in front of every Hundred/Thousand
For example: Let's say the first number in Ones is called Taff
As it is, the program will make Taff Hundred where it should make make something like Taffss Hundred
So I created the Hundreds() which includes the correct callings.
Ones() contains the words from 1 - 9
Teens() contains the words from
11 - 19
Tens() contains the words from 10 - 90 (10,20,30 etc)
Hundreds() contains the words from 100-900 (100,200,300 etc)
Here is my code :
intAmount = eAmount
Dim nTousands As Integer = intAmount \ 1000 : intAmount = intAmount Mod 1000
Dim nHundred As Integer = intAmount \ 100 : intAmount = intAmount Mod 100
Dim nTen As Integer = intAmount \ 10 : intAmount = intAmount Mod 10
Dim nOne As Integer = intAmount \ 1
If nTen > 0 Then
If nTen = 1 And nOne > 0 Then
wAmount = wAmount & Teens(nOne) & " "
Else
wAmount = wAmount & Tens(nTen) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
End If
ElseIf nOne > 0 Then
wAmount = wAmount & Ones(nOne) & " "
wAmount = wAmount & HMBT(nSet) & " "
wAmount = AmountInWords(CStr(CLng(nAmount) - _
(eAmount * multiplier)).Trim & tempDecValue, wAmount, nSet - 1)
Being the noob I am, I figured that by copy pasting the code for 10s, and changing the values to 100s will make things work, but it only works for 100 200 300 etc, not for the numbers in-between.
ElseIf nHundred > 0 Then
If nHundred = 1 And nOne > 0 Then
'Don't know how to properly add things here.
Else
wAmount = wAmount & Hundreds(nHundred) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
Update
I made some changes, this time it partially works...
It shows 100s, 200s, 300s etc etc.
But it only shows from 100 to 109 when it "increments" If it goes above 109, the 100s aren't show, and it goes back to showing 10s, 11s, 12s etc.
ElseIf nHundred > 0 Then
If nHundred = 1 And nTen > 0 And nOne > 0 Then
wAmount = wAmount & Teens(nTen) & " "
Else
wAmount = wAmount & Hundreds(nHundred) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
Made some changes... Is this closer to what you're after?
Private list_ones() As String = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
Private list_teens() As String = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
Private list_tens() As String = {"ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"}
Private list_hundreds() As String = {"ones", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
Private Function get_string_from_numeric(ByVal curr_number As Integer) As String
Dim to_return As String = ""
Try
Select Case curr_number
Case Is < 10
to_return = list_ones(curr_number)
Case 10 To 19
to_return = list_teens(curr_number - 10)
Case 20 To 99
to_return = get_return_value(curr_number, 10, list_tens)
Case 100 To 999
to_return = get_return_value(curr_number, 100, list_hundreds, "hundred")
Case 1000 To 9999
to_return = get_return_value(curr_number, 1000, list_hundreds, "thousand")
Case Is > 9999
to_return = "out of range"
End Select
Catch
Finally
End Try
Return to_return
End Function
Private Function get_return_value(ByVal curr_number As Integer, ByVal curr_pace_holder As Integer, ByVal curr_array_list As String(), Optional ByVal str_term As String = "") As String
Dim to_return As String = ""
Dim curr_iter As Integer = Math.Floor(curr_number / curr_pace_holder)
Dim curr_remainder As Integer = curr_number - curr_iter * curr_pace_holder
If (str_term <> "") Then str_term = " " & str_term
If (curr_remainder > 0) Then
to_return = curr_array_list(curr_iter - 1) & str_term & " " & get_string_from_numeric(curr_remainder)
Else
to_return = curr_array_list(curr_iter - 1) & str_term
End If
Return to_return
End Function
Private Sub cmd_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_submit.Click
Try
If (IsNumeric(txt_input.Text)) Then
txt_output.Text = get_string_from_numeric(CInt(txt_input.Text))
Else
MsgBox("Invalid input.")
End If
Catch
Finally
End Try
End Sub