How to handle second condition in If Else statement - vb.net

How to handle second condition in If Else statement, My if statement is working but the second condition is not working(ElseIF), Is there something wrong with my condition declaration?
Try
If Val(TextBox7.Text.Trim.Split(".")(1)) >= 60 Then
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) + 1 & "." & Val(TextBox7.Text.Trim.Split(".")(1) - 60) & " " & Format(Now, "MM/dd/yyyy")
TextBox3.Text = Val(TextBox3.Text.Trim.Split(".")(0)) & "." & Val(TextBox3.Text.Trim.Split(".")(1) - 60) & " " & Format(Now, "MM/dd/yyyy")
ElseIf Val(TextBox7.Text.Trim.Split(".")(1)) >= 100 Then
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) - 1 & "." & Val(TextBox7.Text.Trim.Split(".")(1) - 45) & " " & Format(Now, "MM/dd/yyyy")
TextBox3.Text = Val(TextBox3.Text.Trim.Split(".")(0)) - 1 & "." & Val(TextBox3.Text.Trim.Split(".")(1) - 45) & " " & Format(Now, "MM/dd/yyyy")
Else
TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") & Strings.Right(time.Text.Trim, 11)
TextBox3.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") - 1 & Strings.Right(time.Text.Trim, 11)
'TextBox3.Text = Format(Val(TextBox3.Text.Trim.Split(".")(0) - 1) & "." & Val(TextBox3.Text.Trim.Split(".")(1)) & " " & Format(Now, "MM/dd/yyyy"))
End If
Catch
TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") & Strings.Right(time.Text.Trim, 11)
End Try

>= 100 should be the first condition, since it's the most restrictive.
>= 60 should be the second condition, since all values >=100 are also >=60.

What #James said + I'd suggest you rewrite to Select Case - should be easier to follow:
Select Case Val(TextBox7.Text.Trim.Split(".")(1))
Case Is >= 100
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) - 1 & "." & Val(TextBox7.Text.Trim.Split(".")(1) - 45) & " " & Format(Now, "MM/dd/yyyy")
TextBox3.Text = Val(TextBox3.Text.Trim.Split(".")(0)) - 1 & "." & Val(TextBox3.Text.Trim.Split(".")(1) - 45) & " " & Format(Now, "MM/dd/yyyy")
Case Is >= 60
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) + 1 & "." & Val(TextBox7.Text.Trim.Split(".")(1) - 60) & " " & Format(Now, "MM/dd/yyyy")
TextBox3.Text = Val(TextBox3.Text.Trim.Split(".")(0)) & "." & Val(TextBox3.Text.Trim.Split(".")(1) - 60) & " " & Format(Now, "MM/dd/yyyy")
Case Else
TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") & Strings.Right(time.Text.Trim, 11)
TextBox3.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") - 1 & Strings.Right(time.Text.Trim, 11)
End Select
Also several points of interest:
Don't use Val, use Convert class instead, for example, Convert.ToInt32.
You can wrap TextBox3.Text.Trim.Split(".") into a function, and reuse.
You read the date twice, it's better to read a date once before Select, then do dateVar.ToString("MM/dd/yyyy").
Favour .NET syntax instead of VB6. Val, Format, Strings.Left are all VB6. .NET equivalents are Convert.*, ToString with format string, and Substring.
Control naming - TextBox3 and TextBox7 don't mean anything. Try to come up with a better name for them.

Related

Excel VBA: Date Comparison

So I'm currently trying to make a code to compare the current date to two other dates in order to determine the validity of information. For example, if the date is between the first quarter of the year and the second quarter, the information on the document is as of the first quarter date (March 31). Below is what I currently have and for some reason even though the current date is in July, the code keeps saying the information is valid as of March 31. Anyone have any suggestions?
crntDate = Date
q1End = CDate("Mar 31" & " " & Year(Date))
q2End = CDate("Jun 30" & " " & Year(Date))
q3End = CDate("Sep 30" & " " & Year(Date))
q4End = CDate("Dec 31" & " " & Year(Date))
If q1End <= crntDate <= q2End Then
quart = "Q1" & " " & Year(Date)
ElseIf q2End <= crntDate <= q3End Then
quart = "Q2" & " " & Year(Date)
ElseIf q3End <= crntDate <= q4End Then
quart = "Q3" & " " & Year(Date)
Else
quart = "Q4" & " " & Year(Date)
End If
shName = "Quarterly Reporting for" & " " & firstName & " " & lastName & " " & "(" & quart & ")"
With wdApp.ActiveDocument
.SaveAs2 "https://path/" & shName & ".docx"
.Close
End With
If you're trying to format dates as quarters, you don't need all of the end dates and comparisons, you can just use integer division \ in VBA.
Sub test()
Dim quart As String
quart = GetDateAsQuarterYear(VBA.Date)
shName = "Quarterly Reporting for" & " " & firstName & " " & lastName & " " & "(" & quart & ")"
With wdApp.ActiveDocument
.SaveAs2 "https://path/" & shName & ".docx"
.Close
End With
End Sub
Function GetDateAsQuarterYear(crntDate As Date) As String
Dim quarterEnd As Date
quarterEnd = DateSerial(Year(crntDate), 1 + 3 * (1 + (Month(crntDate) - 1) \ 3), 0)
GetDateAsQuarterYear = "Q" & 1 + (Month(crntDate) - 1) \ 3 & " (" & Format$(quarterEnd, "mmmm d, yyyy") & ")"
End Function
q1End <= crntDate <= q2End does not work in Excel it needs to be:
q1End <= crntDate and crntDate <= q2End
So
crntDate = Date
q1End = CDate("Mar 31" & " " & Year(Date))
q2End = CDate("Jun 30" & " " & Year(Date))
q3End = CDate("Sep 30" & " " & Year(Date))
q4End = CDate("Dec 31" & " " & Year(Date))
If q1End <= crntDate and crntDate <= q2End Then
quart = "Q2" & " " & Year(Date)
ElseIf q2End <= crntDate and crntDate <= q3End Then
quart = "Q3" & " " & Year(Date)
ElseIf q3End <= crntDate and crntDate <= q4End Then
quart = "Q4" & " " & Year(Date)
Else
quart = "Q1" & " " & Year(Date)
End If
shName = "Quarterly Reporting for" & " " & firstName & " " & lastName & " " & "(" & quart & ")"
With wdApp.ActiveDocument
.SaveAs2 "https://path/" & shName & ".docx"
.Close
End With

Issue with VBA .offset number

I have been searching for an hour or so and I can't seem to find anything on this issue. I may be wording it wrong so I'm not getting the results im looking for. I am having an issue with .offset (, 99). It seems that any time I put a like digit in there such as 99, 88, 11 or whatever combo there is, it raises runtime error 13 type mismatch.
But when I change it to 98 or any other non like combo it works fine. Is there some way that I need to program it if it has the same number multiple times?
Sub Scope()
Dim ws As Excel.Worksheet
Set ws = ThisWorkbook.Sheets("Future Ongoing Vetting") ' change to name of your sheet
Dim x As Long
x = 2
Do Until ws.Cells(x, 7) = ""
With ws.Cells(x, 5)
.Value = "• Customer name: " & .Offset(, 29) & _
Chr(10) & "• Customer Bus Org: " & .Offset(, 30) & _
Chr(10) & "• Internal Circuit ID: " & .Offset(, 2) & _
Chr(10) & "• Customer prem address: " & .Offset(, 12) & " " & .Offset(, 13) & " " & .Offset(, 14) & ", " & .Offset(, 15) & ", " & .Offset(, 16) & ", " & .Offset(, 17) & _
Chr(10) & "• Customer demarc: " & .Offset(, 18) & " " & .Offset(, 20) & ", " & .Offset(, 19) & " " & .Offset(, 21) & _
Chr(10) & "• MRR: " & .Offset(, 68) & _
Chr(10) & "• Current Off Net MRC: $" & .Offset(, 10) & _
Chr(10) & "• Margin Percent: " & .Offset(, 89) & _
Chr(10) & "• Bandwidth: " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb )" & _
Chr(10) & "• Customer term end date: ""TEXT(.Offset(, 32),""mmm-dd-yyyy"")" & _
Chr(10) & "• New Vendor: " & .Offset(, 106) & _
Chr(10) & "• New MRC: $" & .Offset(, 102) & _
Chr(10) & "• New NRC: $" & .Offset(, 103) & _
Chr(10) & "• New Install Interval: " & .Offset(, 105) & _
Chr(10) & "• New Term: " & .Offset(, 104) & _
Chr(10) & Chr(10) & "Planner Notes: This project is replacing the existing " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb ) based solution from ( " & .Offset(, 5) & " ) with a new " & .Offset(, 99) '& " ( " & .Offset(, 100) & "Mb ) based solution from ( " & .Offset(, 106) & " )."
' Chr(10) & "RFA # " & .Offset(, 107) & " install notes: " & _
' Chr(10) & "Please install ( " & .Offset(, 31) & " ) Ethernet " & .Offset(, 99) & " ( " & .Offset(, 100) & "Mb ) circuit with ( " & .Offset(, 106) & " ) from ( " & .Offset(, 101) & " ) to ( [Customer Prem] " & .Offset(, 12) & " " & .Offset(, 13) & " " & .Offset(, 14) & ", " & .Offset(, 15) & ", " & .Offset(, 17) & " )."
' Chr(10) & "This new circuit will be used to replace existing customer circuit ECCKT: ""&RC[1]&"", ""&RC[109]&"", ""&RC[110]&"", ICCKT: "" & RC[2] & ""." & _
' Chr(10) & "The customer prem address is ( ""&RC[12]&"" ""&RC[13]&"" ""&RC[14]&"", ""&RC[15]&"", ""&RC[16]&"", ""&RC[17]&"" ) and customer demarc is ( ""&RC[18]&"" ""&RC[20]&"", ""&RC[19]&"" ""&RC[21]&"" )."
End With
x = x + 1
Loop
End Sub
Your code is written in a way that makes it very difficult to debug. I would recommend replacing that extremely long statement by:
Dim s As String 'at the top of the module, and then in the loop ...
s = "• Customer name: " & .Offset(, 29)
s = s & Chr(10) & "• Customer Bus Org: " & .Offset(, 30)
s = s & Chr(10) & "• Internal Circuit ID: " & .Offset(, 2)
s = s & Chr(10) & "• Customer prem address: " & .Offset(, 12) & " " & .Offset(, 13) & " " & .Offset(, 14) & ", " & .Offset(, 15) & ", " & .Offset(, 16) & ", " & .Offset(, 17)
s = s & Chr(10) & "• Customer demarc: " & .Offset(, 18) & " " & .Offset(, 20) & ", " & .Offset(, 19) & " " & .Offset(, 21)
s = s & Chr(10) & "• MRR: " & .Offset(, 68)
s = s & Chr(10) & "• Current Off Net MRC: $" & .Offset(, 10)
s = s & Chr(10) & "• Margin Percent: " & .Offset(, 89)
s = s & Chr(10) & "• Bandwidth: " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb )"
s = s & Chr(10) & "• Customer term end date: ""TEXT(.Offset(, 32),""mmm-dd-yyyy"")"
s = s & Chr(10) & "• New Vendor: " & .Offset(, 106)
s = s & Chr(10) & "• New MRC: $" & .Offset(, 102)
s = s & Chr(10) & "• New NRC: $" & .Offset(, 103)
s = s & Chr(10) & "• New Install Interval: " & .Offset(, 105)
s = s & Chr(10) & "• New Term: " & .Offset(, 104)
s = s & Chr(10) & Chr(10) & "Planner Notes: This project is replacing the existing " & .Offset(, 6) & " ( " & .Offset(, 7) & "Mb ) based solution from ( " & .Offset(, 5) & " ) with a new " & .Offset(, 99) '& " ( " & .Offset(, 100) & "Mb ) based solution from ( " & .Offset(, 106) & " )."
.Value = s
Splitting the needlessly long statement into a series of shorter statements will enable you to better pinpoint where the type mismatch error is coming from.
I would prefer calling a function to build the string. You'll be able isolate the code and debug it in the Immediate Window without.
Note: I use ws.Rows(5).Cells to set a reference the cells in the row. In this way; I can reference each column by it's actual column number not the offset of Cells(RowNumber, 5). It also allows me to use a shorthand method of referring to cells on that row. e.g. r(1) refers to column 1 r(10) refers to column 10 of Rows(RowNumber).
Function getDescription(ws As Worksheet, RowNumber As Long) As String
Dim r As Range
Dim Data(14)
Set r = ws.Rows(5).Cells
Data(0) = "• Customer name: " & r(34)
Data(1) = "• Customer Bus Org: " & r(35)
Data(2) = "• Internal Circuit ID: " & r(7)
Data(3) = "• Customer prem address: " & r(17) & " " & r(18) & " " & r(19) & ", " & r(20) & ", " & r(21) & ", " & r(22)
Data(4) = "• Customer term end date: " & Chr(34) & Format(r(37), "mmm-dd-yyyy") & Chr(34)
Data(5) = "• Customer demarc: " & r(23) & " " & r(25) & ", " & r(24) & " " & r(26)
Data(6) = "• MRR: " & r(73)
Data(7) = "• Current Off Net MRC: $" & r(15)
Data(8) = "• Margin Percent: " & r(94)
Data(9) = "• Bandwidth: " & r(11) & " ( " & r(12) & "Mb )"
Data(10) = "• New Vendor: " & r(111)
Data(11) = "• New MRC: $" & r(107)
Data(12) = "• New NRC: $" & r(108)
Data(13) = "• New Install Interval: " & r(110)
Data(14) = "• New Term: " & r(109)
getDescription = Join(Data, Chr(10))
End Function

Copying data without opening workbook using Excel VBA

I'm intending to copy data from one workbook to another through a macro. However, I'm using an Add-In my company has created, which prohibits more than one workbook to be open at once.
Application.ScreenUpdating = False
CurrentYear = Year(Date)
CurrentMonth = Month(Date)
StartDate = DateAdd("m", MonthOffset, Date)
MonthNo = Month(StartDate)
YearNo = Year(StartDate)
path2 = IIf(MonthNo >= 10, Dir("C:\path\filename " & YearNo & "-" & MonthNo & ".xlsx"), Dir("C:\path\filename " & YearNo & "-0" & MonthNo & ".xlsx"))
p2file = "C:\path"
Varrr = IIf(MonthNo >= 10, p2file & path2 & "[filename.xlsx " & YearNo & "-" & MonthNo & ".xlsx]Sheetname!", p2file & path2 & "[filename.xlsx " & YearNo & "-" & MonthNo & ".xlsx]Sheetname!")
i = 0
Do While Len(path2) > 0
Varrrcell = Cells(3,4+i).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Varrr = Varrr & Varrrcell
currentWb.Sheets("Blad1").Cells(27, 2 + i + 12 * (YearNo Mod 2015)).Formula= "=Varrr"
i = IIf(i > 12, 1, i + 1)
YearNo = IIf(i > 12, YearNo + 1, YearNo)
path2 = IIf(i >= 10, Dir("C:\path\filename " & YearNo & "-" & i & ".xlsx"), Dir("C:\path\filename" & YearNo & "-0" & i & ".xlsx"))
Varrr = IIf(i >= 10, p2file & path2 & "[filename.xlsx " & YearNo & "-" & MonthNo & ".xlsx]Sheetname!", p2file & path2 & "[filename.xlsx " & YearNo & "-" & MonthNo & ".xlsx]Sheetname!")
Loop
Application.ScreenUpdating = True
EDIT: I've recoded my work to reference to the workbook directly using formula. This solution actually returns the correct path, file and cell I want to copy so that works as intended. However, it returns "Varrr" in each cell. How can I make it return the value instead of the variable name?
EDIT2: I've also contatenated "='" before path2 & p2file & ".." when creating and updating Varrr, and simply saying that
currentWb.Sheets("Blad1").Cells(27, 2 + i + 12 * (YearNo Mod 2015)).Formula= Varrr
but this causes error 1004
EDIT3: I've also added a "closing" ' before the name of the sheet. It did not help.
EDIT4: I've also tried to omit the equal signs, and contenated Varrr = Varrr & Varrcell
and subsequentially put:
currentWb.Sheets("Blad1").Cells(27, 2 + i + 12 * (YearNo Mod 2015)).Formula = "=" & varrr
but it gives code 1004 in the same code snippet. "Application-defined or Object-defined error"
EDIT 5:
Trying to incorporate ExecuteExcel4Macro I've tried the following solution, where Dim ReturnedValue as String:
Varrr = "'" & p2file & "[filename.xlsx " & YearNo & "-" & MonthNo & ".xlsx]Sheetname!'"
ReturnedValue = Varrr & Range("D3").Adress(True,True,-4150)
MsgBox ExecuteExcel4Macro(ReturnedValue)
where the MsgBox gives function error 1004. Here I have trimmed it down a bit to omit the "IIf" statement and the Do loop as I Believe these are redundant in the context.

filter a date range using vba access 2007

I've got the following problem.
In my table is a field named "datum" This field is from a date type. this field is european
format (dd/mm/yyyy)
I also have a popupform, with two fields named "begindatum" and "einddatum"
When I fill in the dates in the popupform, all I want see the are records in the subform I selected in the popupform
Here is my code till so far,
CODE
Private Sub cmdFilterAan_Click()
Dim strFilter As String
If begindatum <> "" Then
strFilter = strFilter & " And begindatum=#" & Format(CDate(Me.begindatum), "dd/mm/yyyy") & "#"
End If
If einddatum <> "" Then
strFilter = strFilter & " And einddatum=#" & Format(CDate(Me.einddatum), "dd/mm/yyyy") & "#"
End If
If naam <> "" Then
strFilter = strFilter & " And naam=" & Me.naam
End If
If beroep <> "" Then
strFilter = strFilter & " And beroep=" & Me.beroep
End If
If strFilter <> "" Then
strFilter = Mid(strFilter, 6)
Forms!frmData.FilterOn = True
Forms!frmData.Filter = strFilter
End If
End Sub
The strFilter for "naam" and "beroep" are working fine, but for the date fields its not.
I think I'am looking for a BETWEEN statement in VBA code, how to do so I do not know.
When I fill in the dates in my popupform nothing happens, as a matter of fact the records
in the subform all disapears
Can anyone help me to solve thus problem.
thx in advance
greetings, ahc59
Try passing your dates in mm/dd/yyyy format. Filter seems particular about using that format. So, for instance:
If begindatum <> "" Then
strFilter = strFilter & " And begindatum=#" & Format(CDate(Me.begindatum), "mm/dd/yyyy") & "#"
End If
If einddatum <> "" Then
strFilter = strFilter & " And einddatum=#" & Format(CDate(Me.einddatum), "mm/dd/yyyy") & "#"
End If
If you want to do between the dates, you can do:
If begindatum <> "" And einddatum <> "" Then
strFilter = strFilter & " And begindatum >= #" & Format(CDate(Me.begindatum), "mm/dd/yyyy") & "# And einddatum <= #" & Format(CDate(Me.einddatum), "mm/dd/yyyy") & "#"
End If
You can't use BETWEEN here because the dates are in two different fields.
Unfortunately in the core acces vba works with US format ("mm/dd/yyyy"), i used on my form to filter dates this code, where [Data] is a field from a table :
Function Today()
Dim dDate As String
dDate = Format(Date, "mm/dd/yyyy")
strFilter2 = "[Data] = #" & dDate & "#"
Forms!MainForm!SubForm.Form.Filter = strFilter2
Forms!MainForm!SubForm.Form.FilterOn = True
End Function
Function ThisWeek()
dDate = Format(Date - 2, "mm/dd/yyyy")
dDate2 = Weekday(dDate)
dDate3 = Date - dDate2
dDate4 = Format(Date - 1, "mm/dd/yyyy")
dDate5 = Weekday(dDate4)
dDate6 = 7 - dDate5
dDate7 = Date + dDate6
strFilter3 = "[Data] between #" & dDate3 & "# AND #" & dDate7 & "#"
Me.Refresh
Forms!MainForm!SubForm.Form.Filter = strFilter3
Forms!MainForm!SubForm.Form.FilterOn = True
End Function
Function ThisMonth()
dDate = Date - Day(Date) + 1
dDate2 = DateSerial(Year(Date), Month(Date) + 1, 0)
dDate3 = Format(dDate, "mm/dd/yyyy")
dDate4 = Format(dDate2, "mm/dd/yyyy")
strFilter3 = "[Data] between #" & dDate3 & "# AND #" & dDate4 & "#"
Me.Refresh
Forms!MainForm!SubForm.Form.Filter = strFilter3
Forms!MainForm!SubForm.Form.FilterOn = True
End Function
Function ThisYear()
dDate = "1/1/" & Year(Date)
dDate2 = "31/12/ " & Year(Date)
dDate3 = Format(dDate, "mm/dd/yyyy")
dDate4 = Format(dDate2, "mm/dd/yyyy")
strFilter3 = "[Data] between #" & dDate3 & "# AND #" & dDate4 & "#"
Me.Refresh
Forms!MainForm!SubForm.Form.Filter = strFilter3
Forms!MainForm!SubForm.Form.FilterOn = True
End Function
Function All()
'If you have default to show them all
Forms!MainForm!SubForm.Form.FilterOn = False
End Function

Use add operation in two textbox with a specific integer and specific date?

Is it possible or do I need to convert it first? This is my code but the output is coming wrong:
TextBox7.Text = TextBox6.Text + time.Text.Substring(4)
This will be the sample run:
TextBox6.Text = "5.12"
time.Text = "9.51 AM 3/3/2014"
This is the result:
Textbox7.Text = "14.63 AM 3/3/2014"
Expected Result :
Textbox7.Text = "15.03 AM 3/3/2014"
try Like This
TextBox7.Text = Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim,4)) &
Val(Strings.Right(time.Text.Trim,(time.Text.Trim.Length-4)))
EDIT
TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 4)),
"##.##") & Strings.Right(time.Text.Trim, 12)
EDIT NEW REQ:
TextBox3.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00")
If Val(TextBox6.Text.Trim.Split(".")(1)) >= 60 Then
TextBox6.Text = Val(TextBox6.Text.Trim.Split(".")(0)) + 1 & "." &
Val(TextBox6.Text.Trim.Split(".")(1) - 60)
End If
TextBox7.Text = Format(Val(TextBox6.Text), "##.##") & Strings.Right(time.Text.Trim, 12)
EDIT NEW REQ
TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)),
"#0.00")
Dim xDate As Date = Format(CDate(Strings.Right(time.Text.Trim, 12)), "tt
MM/dd/yyyy")
If Val(TextBox7.Text.Trim.Split(".")(1)) >= 60 Then
TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) + 1 & "." &
Val(TextBox7.Text.Trim.Split(".")(1) - 60)
End If
If Val(TextBox7.Text) > 24 Then
TextBox7.Text = Format(Val(TextBox7.Text - 24), "##.##") & " " &
DateAdd(DateInterval.Day, 1, xDate)
End If