Generate as many documents as cell value - vba

I have a label generator code, to generate pdf's, but I need to generate as many "labels" inside one pdf file as "packets" in the variable qtypackets exists.
So here is the code, how I can do this? I have an idea with a "for each" but don't know too good, how to implement it inside the code.
Sub LabelGenerator()
Dim invoiceRng As Range
Dim pdfile As String
Dim strFileName As String
Dim strFileExists As String
Dim tracking As Range
Dim GoogleBook As Workbook
Dim strtrackingnumber As String
Dim Thisbook As Workbook
Dim displaylabels As String
' -- Setting and open the information book from Gsheet
Set GoogleBook = Workbooks.Open("C:\Users\peter\OneDrive\Escritorio\B2GO\Excels\GoogleConnection.xlsx")
' -- Refreshing the book
GoogleBook.RefreshAll
' -- Select the right sheet
Worksheets("Planilla Envios").Select
' -- Starting the Loop
For Each tracking In Range("A2:A" & Cells(Rows.Count, 2).End(xlUp).Row)
' -- Validation
If tracking <> "" Then
If IsNumeric(tracking) = True Then
' -- Mandatory ID
trackingnumber = tracking.Value
strFileName = "C:\Users\peter\OneDrive\Escritorio\B2GO\Excels\Generated Labels" & "\" & "Label " & trackingnumber & ".pdf"
strFile = Dir(strFileName)
If strFile = "" Then
' -- Searching the data variables
idcompany = tracking.Offset(0, 2).Value
idcustomer = tracking.Offset(0, 12).Value
companyname = tracking.Offset(0, 3).Value
companycontact = tracking.Offset(0, 4).Value
companystreetaddress = tracking.Offset(0, 5).Value
companystreetnumberaddress = tracking.Offset(0, 6).Value
companycommuneaddress = tracking.Offset(0, 7).Value
companyhousedepartment = tracking.Offset(0, 8).Value
companyphone = tracking.Offset(0, 9).Value
companyemail = tracking.Offset(0, 10).Value
qtypackets = tracking.Offset(0, 11).Value
customername = tracking.Offset(0, 13).Value
customerstreetaddress = tracking.Offset(0, 14).Value
customerstreetnumberaddress = tracking.Offset(0, 15).Value
customercommuneaddress = tracking.Offset(0, 16).Value
customerhousedepartment = tracking.Offset(0, 17).Value
customerphone = tracking.Offset(0, 19).Value
customeremail = tracking.Offset(0, 20).Value
reference = tracking.Offset(0, 18).Value
rate = tracking.Offset(0, 21).Value
' -- Select the right book to place the data
Set Thisbook = Workbooks.Open("C:\Users\peter\OneDrive\Escritorio\B2GO\Excels\New Shipment And Labels.xlsm")
Worksheets("Label format").Select
' -- placing the data in each cell
Worksheets("Label format").Range("D6").Value = companyname
Worksheets("Label format").Range("D7").Value = companycontact
Worksheets("Label format").Range("D8").Value = companyphone
Worksheets("Label format").Range("D10").Value = customername
Worksheets("Label format").Range("D11").Value = idcustomer
Worksheets("Label format").Range("D12").Value = customerphone
Worksheets("Label format").Range("I6").Value = rate
Worksheets("Label format").Range("I7").Value = "NORM"
Worksheets("Label format").Range("H11").Value = "1 de " & qtypackets
Worksheets("Label format").Range("C17").Value = customerstreetaddress
Worksheets("Label format").Range("H17").Value = customerstreetnumberaddress
Worksheets("Label format").Range("C21").Value = customercommuneaddress
Worksheets("Label format").Range("F21").Value = customerhousedepartment
Worksheets("Label format").Range("H21").Value = reference
Worksheets("Label format").Range("C25").Value = trackingnumber
' -- Setting range to be printed
Set invoiceRng = Range("A1:J31")
' -- setting file name with a time stamp.
pdfile = "Label " & trackingnumber & ".pdf"
' -- setting the fulli qualified name. The resultent pdf will be saved where the main file exists.
invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\Users\peter\OneDrive\Escritorio\B2GO\Excels\Generated labels" & "\" & pdfile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
' -- Cleaning up the label format
Worksheets("Label format").Range("D6").Value = ""
Worksheets("Label format").Range("D7").Value = ""
Worksheets("Label format").Range("D8").Value = ""
Worksheets("Label format").Range("D10").Value = ""
Worksheets("Label format").Range("D11").Value = ""
Worksheets("Label format").Range("D12").Value = ""
Worksheets("Label format").Range("I6").Value = ""
Worksheets("Label format").Range("I7").Value = ""
Worksheets("Label format").Range("H11").Value = ""
Worksheets("Label format").Range("C17").Value = ""
Worksheets("Label format").Range("H17").Value = ""
Worksheets("Label format").Range("C21").Value = ""
Worksheets("Label format").Range("F21").Value = ""
Worksheets("Label format").Range("H21").Value = ""
' -- end
Worksheets("New request").Select
End If
End If
End If
Next tracking End Sub
Here are an image example of what Im trying to do.

Related

Loop through different controls on a user form and read/write the value VBA

I would like to somehow get the value from the different controls on the user form and then write them on the sheet after that if the user form is closed down and re opened if a name is selected in the combobox then load all data in the form back ready to change values. I have 13 rows that a user can use on the user form.
In my code the writing the data to the sheet will write all item selected i want but it takes too long because all of the loops and ifs. Is there a better way to achieve what i want?
Private Sub FillingInForm()
Dim i As Long
Dim WS As Worksheet
Dim ctl As MSForms.Control
Dim lbl As MSForms.Label
Dim cmb As MSForms.ComboBox
Dim txtbox As MSForms.TextBox
Dim optbtn As MSForms.OptionButton
Set WS = ActiveSheet
With WS
For i = 1 To ItemsListFrame.Controls.Count
For Each ctl In ItemsListFrame.Controls
If TypeName(ctl) = "Label" Then
If ctl.Tag = "GroupItem" & i Then
Set lbl = ctl
If Controls("Item" & i).Value <> vbNullString Then
.Range("A" & i + 6).Offset(0, 0).Value = Me.OrderNo.Value
.Range("A" & i + 6).Offset(0, 1).Value = Me.NextCollectionDate.Text
.Range("A" & i + 6).Offset(0, 1).Value = Format(.Range("A" & i + 6).Offset(0, 1).Value, "dd/mm/yyyy")
.Range("A" & i + 6).Offset(0, 8).Value = Me.DateReturnBy.Value
.Range("A" & i + 6).Offset(0, 8).Value = Format(.Range("A" & i + 6).Offset(0, 8).Value, "dd/mm/yyyy")
Controls("OrderLbl" & i).Enabled = True
End If
End If
ElseIf TypeName(ctl) = "ComboBox" Then
If ctl.Tag = "GroupItem" & i Then
Set cmb = ctl
If Controls("Item" & i).Value <> vbNullString Then
Controls("Item" & i).Enabled = True
End If
If Controls("Item" & i).Value <> vbNullString Then
.Range("A" & i + 6).Offset(0, 2).Value = Controls("Item" & i).Text
End If
End If
ElseIf TypeName(ctl) = "TextBox" Then
If ctl.Tag = "GroupItem" & i Then
Set txtbox = ctl
If Controls("Item" & i).Value <> vbNullString Then
.Range("A" & i + 6).Offset(0, 3).Value = Controls("Qty" & i).Value
.Range("A" & i + 6).Offset(0, 4).Value = Controls("UnitPrice" & i).Value
.Range("A" & i + 6).Offset(0, 5).Value = Controls("SubTotal" & i).Value
.Range("A" & i + 6).Offset(0, 7).Value = Controls("Comments" & i).Value
Controls("Qty" & i).Enabled = True
Controls("UnitPrice" & i).Enabled = True
Controls("SubTotal" & i).Enabled = True
Controls("Comments" & i).Enabled = True
End If
End If
ElseIf TypeName(ctl) = "OptionButton" Then
If ctl.Tag = "GroupItem" & i Or ctl.Tag = "InOut" & i Then
Set optbtn = ctl
If Controls("Item" & i).Value <> vbNullString Then
.Range("A" & i + 6).Offset(0, 6).Value = Controls("OptionOut" & i).Value
Controls("OptionIn" & i).Enabled = True
Controls("OptionOut" & i).Enabled = True
End If
End If
End If
Next ctl
Next i
End With
End Sub

Dynamic excel hyperlink

Hello at the moment I currently have code that generates a seried of hyperlinks in a table that when clicked reference back to a cell on a different sheet contained in the workbook book. See code below.
Report.Hyperlinks.Add Anchor:=Report.Cells(LineNum, 1), Address:="", SubAddress:="Data!A" & (Counter - 1), TextToDisplay:="Link to Data"
The variable counter is the cell in which the data I am linking to is located and Data is the sheet it is contained in. Report is the worksheet to which I am writing the hyperlinks too.
The issue that I am encountering is that when data is either removed or added in the "Data" sheet the hyperlink in the "report" sheet will then link to the wrong cell thus rendering it useless. So to conclude is their a way to generate a dynamic hyperlink that changes based on edits (only line removals not column removal) to endure the hyperlinks link back to the correct data? Thank you for you help.
Full while loop as requested
DaysInCombo = 0
DaysInYear = DateSerial(YearA + 1, 1, 1) - DateSerial(YearA, 1, 1)
ExtraColNumber = Data.UsedRange.Columns.Count + 1
UltimateCount = Data.UsedRange.Rows.Count
Do While Data.Cells(Counter, 4).Value <> ""
'Check if at new position the address or meter number has changed
If Data.Cells(Counter, 4).Value <> CurrentAddress Or Data.Cells(Counter, 11).Value <> CurrentMeterNumber Then
'check num days to determine if there is an exception to be considered
If DaysInCombo = DaysInYear Then
Debug.Print "Good: " & CurrentAddress
Else
Debug.Print "Bad: " & CurrentAddress & " - " & DaysInCombo & " days - SN: " & CurrentMeterNumber
'compare meter number against the known lists
'if meter exists within the known lists then make note and place into a string
ExceptionStr = ""
ReasonStr = ""
TimeRangeStr = ""
'Data.Cells(Counter, 36).Value
'Compare against Meter Removal List
CheckCounter = 2
Do While MeterRemoval.Cells(CheckCounter, 1).Value <> ""
If CurrentMeterNumber = MeterRemoval.Cells(CheckCounter, 10).Value Or _
Right(CurrentMeterNumber, 8) = MeterInstall.Cells(CheckCounter, 10).Value Or _
(InStr(1, CurrentAddress, MeterRemoval.Cells(CheckCounter, 6).Value, vbTextCompare) = 1 And _
MeterRemoval.Cells(CheckCounter, 6).Value <> "") Then
Debug.Print "Success"
ExceptionStr = ExceptionStr & vbCrLf & "Meter Found on the Meter Removal list"
ReasonStr = ReasonStr & "Removed meter"
End If
CheckCounter = CheckCounter + 1
Loop
'Compare against Meter Install List
CheckCounter = 2
Do While MeterInstall.Cells(CheckCounter, 4).Value <> ""
If CurrentMeterNumber = MeterInstall.Cells(CheckCounter, 4).Value Or _
Right(CurrentMeterNumber, 8) = MeterInstall.Cells(CheckCounter, 4).Value Or _
InStr(1, CurrentAddress, MeterInstall.Cells(CheckCounter, 3).Value & " " & MeterInstall.Cells(CheckCounter, 2).Value, vbTextCompare) = 1 Then
Debug.Print "Success"
ExceptionStr = ExceptionStr & vbCrLf & "New Meter Installation"
ReasonStr = ReasonStr & "New meter"
End If
CheckCounter = CheckCounter + 1
Loop
'Compare against Meter Replace List
CheckCounter = 2
Do While MeterReplace.Cells(CheckCounter, 4).Value <> ""
If CurrentMeterNumber = MeterReplace.Cells(CheckCounter, 4).Value Or _
Right(CurrentMeterNumber, 8) = MeterReplace.Cells(CheckCounter, 4).Value Or _
CurrentMeterNumber = MeterReplace.Cells(CheckCounter, 5).Value Or _
Right(CurrentMeterNumber, 8) = MeterReplace.Cells(CheckCounter, 5).Value Or _
InStr(1, CurrentAddress, MeterReplace.Cells(CheckCounter, 3).Value & " " & MeterReplace.Cells(CheckCounter, 2).Value, vbTextCompare) = 1 Then
Debug.Print "Success"
ExceptionStr = ExceptionStr & vbCrLf & "Replaced Meter"
ReasonStr = ReasonStr & "Replaced meter"
End If
CheckCounter = CheckCounter + 1
Loop
'Compare Address Against the Address change list
CheckCounter = 2
'needs work
Do While AddressChange.Cells(CheckCounter, 1).Value <> ""
If InStr(1, CurrentAddress, AddressChange.Cells(CheckCounter, 1).Value, vbTextCompare) = 1 Or _
(InStr(1, CurrentAddress, AddressChange.Cells(CheckCounter, 2).Value, vbTextCompare) = 1 And _
AddressChange.Cells(CheckCounter, 2).Value <> "") Then
Debug.Print CurrentAddress
Debug.Print AddressChange.Cells(CheckCounter, 1).Value
Debug.Print AddressChange.Cells(CheckCounter, 2).Value
Debug.Print "Success"
ExceptionStr = ExceptionStr & vbCrLf & "The address was changed"
ReasonStr = ReasonStr & "Address change"
End If
CheckCounter = CheckCounter + 1
Loop
'Meter Replace NMOG
CheckCounter = 2
Do While MeterReplaceNMOG.Cells(CheckCounter, 4).Value <> ""
If CurrentMeterNumber = MeterReplaceNMOG.Cells(CheckCounter, 4).Value Or _
Right(CurrentMeterNumber, 8) = MeterReplaceNMOG.Cells(CheckCounter, 4).Value Or _
InStr(1, CurrentAddress, MeterReplaceNMOG.Cells(CheckCounter, 3).Value & " " & MeterReplaceNMOG.Cells(CheckCounter, 2).Value, vbTextCompare) = 1 Then
Debug.Print "Success"
ExceptionStr = ExceptionStr & vbCrLf & "Replaced Meter NMOG"
ReasonStr = ReasonStr & "Replaced meter NMOG"
End If
CheckCounter = CheckCounter + 1
Loop
'Check if an exception was found
ExceptionFound = True
If ExceptionStr = "" Or ReasonStr = "" Then
ExceptionStr = "No Exception reason has been found automatically"
ExceptionFound = False
End If
If DateValue(FirstDateRead) > DateValue(Format(DateSerial(YearA, 1, 1))) Then
ExceptionStr = ExceptionStr & vbCrLf & "Meter Recording Started Mid Year"
TimeRangeStr = TimeRangeStr & "Started Mid Year"
End If
If DateValue(LastDateRead) < DateValue(Format(DateSerial(YearA + 1, 1, 1))) Then
ExceptionStr = ExceptionStr & vbCrLf & "Meter Recording Ended Mid Year"
If TimeRangeStr <> "" Then
TimeRangeStr = TimeRangeStr & " - "
End If
TimeRangeStr = TimeRangeStr & "Ended Mid Year"
End If
ExceptionStr = DaysInCombo & " days: " & vbCrLf & ExceptionStr
'The counter is decremented by 1 due to the logic within the loop
Data.Cells(Counter - 1, ExtraColNumber).Value = ExceptionStr
'Make report
'if expection found, then use one report, else other
If ExceptionFound = True Then
Set Report = Report_Auto
Else
Set Report = Report_Manual
End If
'get last line of report sheet
LineNum = Report.UsedRange.Rows.Count + 1
'copy some relevant details of the location to the report and give it a link back to the location in the data.
Application.ScreenUpdating = False
'Link
Report.Hyperlinks.Add Anchor:=Report.Cells(LineNum, 1), Address:="", SubAddress:="Data!A" & (Counter - 1), TextToDisplay:="Link to Data"
'Address
Report.Cells(LineNum, 2).Value = CurrentAddress
'MeterSN
Report.Cells(LineNum, 3).Value = CurrentMeterNumber
'MeterInstall
Report.Cells(LineNum, 4).Value = Data.Cells(Counter - 1, 13).Value
'FirstDate
Report.Cells(LineNum, 5).NumberFormat = "dd-mmm-yy"
Report.Cells(LineNum, 5).Value = Format(FirstDateBill, "dd-MMM-yy")
'LastDate
Report.Cells(LineNum, 6).NumberFormat = "dd-mmm-yy"
Report.Cells(LineNum, 6).Value = Format(LastDateBill, "dd-MMM-yy")
'FirstDate
Report.Cells(LineNum, 7).NumberFormat = "dd-mmm-yy"
Report.Cells(LineNum, 7).Value = Format(FirstDateRead, "dd-MMM-yy")
'LastDate
Report.Cells(LineNum, 8).NumberFormat = "dd-mmm-yy"
Report.Cells(LineNum, 8).Value = Format(LastDateRead, "dd-MMM-yy")
'ProratedDays
Report.Cells(LineNum, 9).Value = DaysInCombo
'RangeText
Report.Cells(LineNum, 10).Value = TimeRangeStr
'ExceptionText
Report.Cells(LineNum, 11).Value = ReasonStr
Application.ScreenUpdating = True
'clear the report value for the next iteration
Set Report = Nothing
Loop

Add new row with data entered to specific table on worksheet if certain listbox item selected

Using Excel 2013 VBA
I have a bit of code where I am looking for a new row of data to be added to the bottom of the tables of a worksheet. The worksheet that needs to be populated is Sheet3 ("Data Sheet") or just Data Sheet on the tab.
On the worksheet there are two tables (Table3 and Table4) and depending on the category selected from the listbox (called StatusListBox), the row of data is added to the bottom of either one of the two tables.
If the user selects "Live", "Secured" or "Completed" then Table3 should be populated but if the user selects "Tender (Pipeline"), Tender (Negotiated)" or "Tender (Favourable)" then Table 4 should be populated.
With the current code I have a new row of data is added to the bottom of BOTH Table3 and Table4.
Private Sub AddNewButton_Click()
Dim the_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Set the_sheet = Sheets("Data Sheet")
Set table_list_objectA = the_sheet.ListObjects("Table3")
Set table_list_objectB = the_sheet.ListObjects("Table4")
Set table_object_rowA = table_list_objectA.ListRows.Add
Set table_object_rowB = table_list_objectB.ListRows.Add
If Me.StatusListBox.ListIndex = "Secured" Then
table_list_objectA.ListRows.Add
ElseIf Me.StatusListBox.ListIndex = "Live" Then
table_list_objectA.ListRows.Add
ElseIf Me.StatusListBox.ListIndex = "Completed" Then
table_list_objectA.ListRows.Add
ElseIf Me.StatusListBox.ListIndex = "Tender (Pipeline)" Then
table_list_objectB.ListRows.Add
ElseIf Me.StatusListBox.ListIndex = "Tender (Negotiated)" Then
table_list_objectB.ListRows.Add
ElseIf Me.StatusListBox.ListIndex = "Tender (Favourable)" Then
table_list_objectB.ListRows.Add
End If
table_object_rowA.Range(1, 1).Value = ProjectNameTextBox.Value
table_object_rowB.Range(1, 1).Value = ProjectNameTextBox.Value
last_row_with_data = the_sheet.Range("A65536").End(xlUp).Row
last_row_with_data = last_row_with_data
the_sheet.Range("B" & last_row_with_data) = ClientTextBox.Value
the_sheet.Range("C" & last_row_with_data) = SectorListBox.Value
the_sheet.Range("D" & last_row_with_data) = StatusListBox.Value
the_sheet.Range("E" & last_row_with_data) = ContractValueTextBox.Value
the_sheet.Range("F" & last_row_with_data) = AFATextBox.Value
the_sheet.Range("G" & last_row_with_data) = RTPTextBox.Value
the_sheet.Range("H" & last_row_with_data) = TwentyFifteenTextBox.Value
the_sheet.Range("I" & last_row_with_data) = TwentySixteenTextBox.Value
the_sheet.Range("J" & last_row_with_data) = TwentySeventeenTextBox.Value
the_sheet.Range("K" & last_row_with_data) = TwentyEighteenTextBox.Value
the_sheet.Range("L" & last_row_with_data) = TwentyNineteenTextBox.Value
the_sheet.Range("M" & last_row_with_data) = DisciplineListBox.Value
the_sheet.Range("N" & last_row_with_data) = BoardDirectorListBox.Value
the_sheet.Range("O" & last_row_with_data) = AssociateDirectorTextBox.Value
the_sheet.Range("P" & last_row_with_data) = CommercialManagerTextBox.Value
the_sheet.Range("Q" & last_row_with_data) = ProjectManagerTextBox.Value
the_sheet.Range("R" & last_row_with_data) = QuantitySurveyorTextBox.Value
the_sheet.Range("S" & last_row_with_data) = PreConTextBox.Value
the_sheet.Range("T" & last_row_with_data) = ActualTextBox.Value
the_sheet.Range("U" & last_row_with_data) = DPStartTextBox.Value
the_sheet.Range("V" & last_row_with_data) = DPEndTextBox.Value
If Me.ProjectNameTextBox.Value = "" Then
MsgBox "Please enter Project Name.", vbExclamation, "Project Tracker Template"
Me.ProjectNameTextBox.SetFocus
End If
End Sub
Would appreciate any help.
ListIndex property returns the item position inside the listbox list, while .Value property returns its selected value (if any) or an error (if no item selected)
So you may need something like follows:
Option Explicit
Private Sub AddNewButton_Click()
Dim the_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Dim last_row_with_data As Long
Dim tableName As String, errMsg As String
If Me.ProjectNameTextBox.Value = "" Then
MsgBox "Please enter Project Name.", vbExclamation, "Project Tracker Template"
Me.ProjectNameTextBox.SetFocus
End If
With Me.StatusListBox
If .ListIndex <> -1 Then
Select Case .Value
Case "Secured", "Live", "Completed"
tableName = "Table3"
Case "Tender (Pipeline)", "Tender (Negotiated)", "Tender (Favourable)"
tableName = "Table4"
Case Else
errMsg = "No valid item selected!"
End Select
Else
errMsg = "No item selected!"
End If
End With
If tableName = "" Then
MsgBox errMsg, vbCritical
Exit Sub
End If
Set the_sheet = Sheets("Data Sheet")
With the_sheet
Set table_list_object = .ListObjects("Table3")
Set table_object_row = table_list_object.ListRows.Add
table_object_row.Range(1, 1).Value = ProjectNameTextBox.Value
last_row_with_data = .Range(.Rows.Count, 1).End(xlUp).row + 1
.Range("B" & last_row_with_data) = ClientTextBox.Value
.Range("C" & last_row_with_data) = SectorListBox.Value
.Range("D" & last_row_with_data) = StatusListBox.Value
.Range("E" & last_row_with_data) = ContractValueTextBox.Value
.Range("F" & last_row_with_data) = AFATextBox.Value
.Range("G" & last_row_with_data) = RTPTextBox.Value
.Range("H" & last_row_with_data) = TwentyFifteenTextBox.Value
.Range("I" & last_row_with_data) = TwentySixteenTextBox.Value
.Range("J" & last_row_with_data) = TwentySeventeenTextBox.Value
.Range("K" & last_row_with_data) = TwentyEighteenTextBox.Value
.Range("L" & last_row_with_data) = TwentyNineteenTextBox.Value
.Range("M" & last_row_with_data) = DisciplineListBox.Value
.Range("N" & last_row_with_data) = BoardDirectorListBox.Value
.Range("O" & last_row_with_data) = AssociateDirectorTextBox.Value
.Range("P" & last_row_with_data) = CommercialManagerTextBox.Value
.Range("Q" & last_row_with_data) = ProjectManagerTextBox.Value
.Range("R" & last_row_with_data) = QuantitySurveyorTextBox.Value
.Range("S" & last_row_with_data) = PreConTextBox.Value
.Range("T" & last_row_with_data) = ActualTextBox.Value
.Range("U" & last_row_with_data) = DPStartTextBox.Value
.Range("V" & last_row_with_data) = DPEndTextBox.Value
End With
End Sub
I have managed to solve the issue with thanks to those that contributed.
I replaced .ListObjects("Table3") with .ListObjects(tableName)
This is the code that now works:
Option Explicit
Private Sub AddNewButton_Click()
Dim the_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Dim last_row_with_data As Long
Dim tableName As String, errMsg As String
If Me.ProjectNameTextBox.Value = "" Then
MsgBox "Please enter Project Name.", vbExclamation, "Project Tracker Template"
Me.ProjectNameTextBox.SetFocus
End If
With Me.StatusListBox
If .ListIndex <> -1 Then
Select Case .Value
Case "Secured", "Live", "Completed"
tableName = "Table3"
Case "Tender (Pipeline)", "Tender (Negotiated)", "Tender (Favourable)"
tableName = "Table4"
Case Else
errMsg = "No valid item selected!"
End Select
Else
errMsg = "No item selected!"
End If
End With
If tableName = "" Then
MsgBox errMsg, vbCritical
Exit Sub
End If
Set the_sheet = Sheets("Data Sheet")
With the_sheet
Set table_list_object = .ListObjects(tableName)
Set table_object_row = table_list_object.ListRows.Add
last_row_with_data = the_sheet.Range("A65536").End(xlUp).Row
table_object_row.Range(1, 1).Value = ProjectNameTextBox.Value
table_object_row.Range(1, 2).Value = ClientTextBox.Value
table_object_row.Range(1, 3).Value = SectorListBox.Value
table_object_row.Range(1, 4).Value = StatusListBox.Value
table_object_row.Range(1, 5).Value = ContractValueTextBox.Value
table_object_row.Range(1, 6).Value = AFATextBox.Value
table_object_row.Range(1, 7).Value = RTPTextBox.Value
table_object_row.Range(1, 8).Value = TwentyFifteenTextBox.Value
table_object_row.Range(1, 9).Value = TwentySixteenTextBox.Value
table_object_row.Range(1, 10).Value = TwentySeventeenTextBox.Value
table_object_row.Range(1, 11).Value = TwentyEighteenTextBox.Value
table_object_row.Range(1, 12).Value = TwentyNineteenTextBox.Value
table_object_row.Range(1, 13).Value = DisciplineListBox.Value
table_object_row.Range(1, 14).Value = BoardDirectorListBox.Value
table_object_row.Range(1, 15).Value = AssociateDirectorTextBox.Value
table_object_row.Range(1, 16).Value = CommercialManagerTextBox.Value
table_object_row.Range(1, 17).Value = ProjectManagerTextBox.Value
table_object_row.Range(1, 18).Value = QuantitySurveyorTextBox.Value
table_object_row.Range(1, 19).Value = PreConTextBox.Value
table_object_row.Range(1, 20).Value = ActualTextBox.Value
table_object_row.Range(1, 21).Value = DPStartTextBox.Value
table_object_row.Range(1, 22).Value = DPEndTextBox.Value
End With
End Sub
Many thanks.

Excel form date format

I have a form a text box to enter the Month and year in this format Dec-15, but it actually returns the value 15/12/2016 in the VBA code.
How can I make it 01/12/2015?
sMonthYear = Trim(Form.txtMonthYear.Value)
report heading = Dec-15
Sheets("Report").Cells(2, 2).Value = "" & sMonthYear & ""
find Folder = 2015
sFolderYear = Year(sMonthYear)
strSavePath = sFilePath + sFolderYear + "\"
File name = Dec-15
sFileName = sMonthYear
wbDest.SaveAs strSavePath & sFileName & ".xls"
Thanks
Try this...
Sheets("Sheet1").Cells(2, 2).Value = "01" & "" & sMonthYear & ""
Or Format The Cell
Option Explicit
Sub DateFormat()
Dim sMonthYear As String
sMonthYear = "Dec-15"
Sheets("Sheet1").Cells(2, 2).NumberFormat = "DD/MM/YYYY"
Sheets("Sheet1").Cells(2, 2).Value = "01" & "" & sMonthYear & ""
End Sub

Vb.net connection timeout while generating excel report

I am trying to generate an excel report using the data from MSSQL. It is perfectly working on my pc(server) and all clients except those PC that has windows 8 installed(MS Office 2010 installed). I tested the program in WinxP/win7 client PC's and both are able to generate the excel report(MS office 2007) installed. Is it possible that the problem is the different version of the MS Office? Here a sample of my code. Again it is working perfectly fine on my server PC and other client PC except that win8 machine.
Private Sub Report_JSIADGTAMKOR()
On Error Resume Next
Dim consigneeName As String
If cmboAccount.Text = "AMKOR" Then
consigneeName = "AMKOR"
ElseIf cmboAccount.Text = "JSI-CIRTEK" Then
consigneeName = "JSI-CIRTEK"
ElseIf cmboAccount.Text = "JSI-LATTICE" Then
consigneeName = "JSI-LATTICE"
Else
consigneeName = "ANALOG DEVICES GEN. TRIAS - JSI"
End If
Wbook = createExcel.Workbooks.Add
Wsheet = Wbook.Worksheets(1)
If Format(dteFrom.Value, "MM-dd-yyyy") = Format(dteTo.Value, "MM-dd-yyyy") Then
DateRange = Format(dteTo.Value, "MM-dd-yyyy")
Else
DateRange = Format(dteFrom.Value, "MM-dd") & " To " & Format(dteTo.Value, "MM-dd-yy")
End If
Wbook.Worksheets(1).Name = cmboAccount.Text & "-SR " + DateRange.ToString
Wsheet.Cells(1, 1).Value = "MAKATI TRANSFORWARDERS CORP."
Wsheet.Cells(1, 1).Font.Bold = True
Wsheet.Cells(1, 1).Font.Size = 10
Wsheet.Cells(3, 1).Value = "Daily Status Report - " & DateRange.ToString
Wsheet.Cells(3, 1).Font.Bold = True
Wsheet.Cells(3, 1).Font.Size = 10
Wsheet.Cells(5, 1).Value = consigneeName
Wsheet.Cells(5, 1).Font.Bold = True
Wsheet.Cells(5, 1).Font.Size = 10
Wsheet.Cells(7, 1).Value = "Prepared by: Jasmin"
Wsheet.Cells(7, 1).Font.Bold = True
Wsheet.Cells(7, 1).Font.Size = 10
Wsheet.Cells(9, 1).Value = "HAWB"
Wsheet.Cells(9, 2).Value = "INVOICE VALUE"
Wsheet.Cells(9, 3).Value = "DUTIABLE VALUE"
Wsheet.Cells(9, 4).Value = "CUSTOMS DUTY"
Wsheet.Cells(9, 5).Value = "LANDED COST"
Wsheet.Cells(9, 6).Value = "ENTRY NO"
Wsheet.Cells(9, 7).Value = "IP NO"
Wsheet.Cells(9, 8).Value = "DESCRIPTION"
Wsheet.Cells(9, 9).Value = "REMARKS"
For Me.itemCounter = 1 To 9
Wsheet.Cells(9, itemCounter).Font.Bold = True
Next itemCounter
rs = New ADODB.Recordset
sql = "Select * From tblDocEntry Where CONVERT(datetime, DateEntry) Between '" & Format(dteFrom.Value, "MM/dd/yyyy") & "' AND '" & Format(dteTo.Value, "MM/dd/yyyy") & "' and ClientCode='" & Trim(cmboAccount.Text) & "' order by DateEntry"
With rs
.Open(sql, cn, CursorTypeEnum.adOpenDynamic, LockTypeEnum.adLockOptimistic)
rowCounter = 10
itemCounter = 1
While .EOF = False
Dim chkBLHAWBno As String
chkBLHAWBno = .Fields("BLHAWBNo").Value
Dim subHAWB As String
subHAWB = chkBLHAWBno.ToString.Substring(0, 3)
If IsExist("tblOmit", "OmitHAWB", subHAWB) Then
Dim len As String = chkBLHAWBno.ToString.Trim.Length
Wsheet.Cells(rowCounter, 3).Value = chkBLHAWBno.ToString.Substring(4, len - 4)
Else
Wsheet.Cells(rowCounter, 3).Value = .Fields("BLHAWBNo").Value
End If
Wsheet.Cells(rowCounter, 1).NumberFormat = "0000"
Wsheet.Cells(rowCounter, 2).Value = .Fields("FMVUSD").Value
Wsheet.Cells(rowCounter, 2).NumberFormat = "0.00"
Wsheet.Cells(rowCounter, 3).Value = .Fields("PESOVALUE").Value
Wsheet.Cells(rowCounter, 3).NumberFormat = "0.00"
Wsheet.Cells(rowCounter, 4).Value = .Fields("DUTYAMOUNT").Value
Wsheet.Cells(rowCounter, 4).NumberFormat = "0.00"
Wsheet.Cells(rowCounter, 5).Value = .Fields("LANDEDCOST").Value
Wsheet.Cells(rowCounter, 5).NumberFormat = "0.00"
Wsheet.Cells(rowCounter, 6).Value = .Fields("ENTRYNO").Value
Wsheet.Cells(rowCounter, 7).Value = .Fields("IPNO").Value
Wsheet.Cells(rowCounter, 8).Value = Trim(.Fields("GoodDesc").Value)
Wsheet.Cells(rowCounter, 9).Value = .Fields("DOCREMARKS").Value
rowCounter = rowCounter + 2
itemCounter = itemCounter + 1
.MoveNext()
End While
.Close()
End With
rs = Nothing
Wsheet.Cells(rowCounter + 1, 1).Value = "Total Number of Entry/ies: " & (itemCounter - 1)
Wsheet.Cells(rowCounter + 2, 1).Value = "Date Printed: " + Format(Now, "MM/dd/yyyy") + " - " + Format(Now, "hh:mm:ss tt")
Wsheet.Cells(rowCounter + 1, 1).Font.Bold = True
Wsheet.Cells(rowCounter + 2, 1).Font.Bold = True
Wsheet.Columns.AutoFit()
Wsheet.Rows.AutoFit()
'Wsheet.Cells.NumberFormat = "0000"
filesavepath = AppPath & "\Status Reports\" & Trim(cmboAccount.Text) & " - Status Report " + DateRange.ToString + ".xls"
Wbook.SaveAs(filesavepath)
Wbook.Close(True)
System.Diagnostics.Process.Start(filesavepath)
Wbook = Nothing
Wsheet = Nothing
MsgBox("Report Successfully Generated", vbOKOnly + vbInformation, "System Alert")
End Sub
The error thrown is something like these:
"Connection Timeout Expired: The timeout period elapsed during the post-login phase. The connection could have timed out while waiting for server to complete the login process and respond. Or it could have timed out while attempting to create multiple active connections."
Any help regarding my problem would be much appreciated
No one can give you exact answer why it happens unless trying it in the hard way.
Generally i suggest you to take more actions to debug it through, to identify:
is it a sql connection problem?
Could it be due to the Win 8 box blocked the sql connection get through? firewall?
what's the size of the expected data from sql? Could it be the Win 8 machine have some anti-virus software scanning the incoming data that slows down the process?
is it due to excel writing process taking too long?
I can see you update excel file cell by cell, actually there are better ways to speed it up significantly, you could ask google for fast writing to excel through interop
data size - could it happen that there are too many rows to write into the file and it takes ages?
You may put more breakpoints or some logging statements to dig out the actual cause, and then work around that.