Run-time error 1004 with vba adding hyperlink - vba

Can someone please help me in my time of need. I have created a userform which is entering in a hyperlink dependent on dropdowns from a listbox.
Despite the hyperlink actually going in when the submit button is pressed, I am still receiving the error message of
Run-Time error 1004. Application-defined or object defined error.
When I debug ws.cells(iRow, 4) is the line highlighted
Private Sub Comm1_Click()
Dim iRow As Long
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim rng As Range
Set ws = Worksheets("QttOutlay")
Set ws2 = Worksheets("LookupVals")
iRow = ws.Cells.Find(what:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
Set rng = ws.Cells(iRow)
ws.Cells(iRow, 2).Value = RmRef.Value
ws.Cells(iRow, 3).Value = RetMod.Value
ws.Cells(iRow, 4).Value = rng.Parent.Hyperlinks.Add(Anchor:=rng, Address:=WorksheetFunction.VLookup(RetMod.Value, ws2.Range("A:B"), 2, False), TextToDisplay:="Info")
ws.Cells(iRow, 5).Value = OrdCod.Value
ws.Cells(iRow, 6).Value = hmm.Value
ws.Cells(iRow, 7).Value = lmm.Value
ws.Cells(iRow, 8).Value = rdtype.Value
ws.Cells(iRow, 9).Value = dtt.Value
ws.Cells(iRow, 10).Value = Wtt.Value
ws.Cells(iRow, 11).Value = Qt.Value
ws.Cells(iRow, 12).Value = LPc.Value
ws.Cells(iRow, 13).Value = Dt.Value
ws.Cells(iRow, 14).Value = (LPc.Value * Dt.Value) * Qt.Value
End Sub

The Hyperlinks.Add Method returns a hyperlink object which you are trying to assign to a cells value: ws.Cells(iRow, 4).Value = rng.Parent.Hyperlinks.Add(…). That won't work.
I guess that ws.Cells(iRow, 4) is meant to be the anchor of the hyperlink like: Anchor:=ws.Cells(iRow, 4)
So instead of
ws.Cells(iRow, 4).Value = rng.Parent.Hyperlinks.Add(Anchor:=rng, Address:=WorksheetFunction.VLookup(RetMod.Value, ws2.Range("A:B"), 2, False), TextToDisplay:="Info")
you should replace the whole line with something like this
ws.Hyperlinks.Add Anchor:=ws.Cells(iRow, 4), Address:=WorksheetFunction.VLookup(RetMod.Value, ws2.Range("A:B"), 2, False), TextToDisplay:="Info"

Related

Formatting a line with VBA Userform

I have created a userform and I am have a small conundrum. How do I set the text to go a certain color if a value in the userform has been selected? What I am wanting to do is, if the SP.Value in the combo box is "Yes" then I want the whole iRow text to be Red, if the ST.Value is Yes I want the whole iRow to be blue. I hope this makes sense? The SP.Value and ST.Value are both combo boxes within the userform with just options of "Yes / No"
I am getting the error With Object must be user-defined type, Object or Variant
Private Sub NL_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sp Br")
iRow = ws.Cells.Find(what:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
If SP.Value = "Yes" Then
With iRow
.colour = -16776961
.TintAndShade = 0
Sheets("Spec Break").Range("B2").Value = Customer.Value
Sheets("Spec Break").Range("B3").Value = Project.Value
Sheets("Spec Break").Range("B4").Value = Format(Now, ["DD/MM/YYYY"])
Sheets("Spec Break").Range("B5").Value = RSM.Value
ws.Cells(iRow, 1).Value = Cf.Value
ws.Cells(iRow, 2).Value = RT.Value
ws.Cells(iRow, 3).Value = MEqu.Value
ws.Cells(iRow, 4).Value = hmm.Value
ws.Cells(iRow, 5).Value = wmm.Value
ws.Cells(iRow, 6).Value = Opt.Value
ws.Cells(iRow, 7).Value = Tap.Value
ws.Cells(iRow, 8).Value = Fing.Value
ws.Cells(iRow, 9).Value = col.Value
ws.Cells(iRow, 10).Value = Pr.Value
ws.Cells(iRow, 11).Value = Qt.Value
End With
End If
'Insert a row beneath the data to push down footer image
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
ActiveCell.EntireRow.Copy
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
'clear form values
CustRef.Value = ""
RadType.Value = ""
MysonEquiv.Value = ""
heightmm.Value = ""
widthmm.Value = ""
Output.Value = ""
Tapping.Value = ""
Fixing.Value = ""
colour.Value = ""
Price.Value = ""
Qty.Value = ""
End Sub
As SJR pointed out your iRow holds a long numerical value, 12345578 etc so you can't really do anything 'with' it (well, you could but that's beside the point). You're already there with your ws.cells code; iRow holds the row number and you specify a column. So, remove your with block and use cells and rows references for the first few lines:
If SP.Value = "Yes" Then
Rows(iRow).colour = -16776961
Rows(iRow).TintAndShade = 0
Sheets("Spec Break").Range("B2").Value = Customer.Value
Sheets("Spec Break").Range("B3").Value = Project.Value
Sheets("Spec Break").Range("B4").Value = Format(Now, ["DD/MM/YYYY"])
Sheets("Spec Break").Range("B5").Value = RSM.Value
ws.Cells(iRow, 1).Value = Cf.Value
' etc
sorry I didn't mean to click down on that... I have upped the answer. Thanks for sending me in the right direction, sadly the solution provided still yielded back an error or 2. After consulting the color pallet and MSDN I found that changing my code to the below has now worked.
Private Sub NL_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Spec Break")
iRow = ws.Cells.Find(what:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
If Specials.Value = "Yes" Then
With Rows(iRow)
.Font.Color = RGB(255, 0, 0)
Sheets("Spec Break").Range("B2").Value = Customer.Value
Sheets("Spec Break").Range("B3").Value = Project.Value
Sheets("Spec Break").Range("B4").Value = Format(Now, ["DD/MM/YYYY"])
Sheets("Spec Break").Range("B5").Value = RSM.Value
ws.Cells(iRow, 1).Value = Cf.Value
ws.Cells(iRow, 2).Value = RT.Value
ws.Cells(iRow, 3).Value = MEqu.Value
ws.Cells(iRow, 4).Value = hmm.Value
ws.Cells(iRow, 5).Value = wmm.Value
ws.Cells(iRow, 6).Value = Opt.Value
ws.Cells(iRow, 7).Value = Tap.Value
ws.Cells(iRow, 8).Value = Fix.Value
ws.Cells(iRow, 9).Value = col.Value
ws.Cells(iRow, 10).Value = Pr.Value
ws.Cells(iRow, 11).Value = Qt.Value
End With
End If
End Sub

type mismatch in vlookup

I am attempting to do the following:
Look at the first row's column B (Event) from entiresalespipeline
Match Column B to a table (Events_and_Activities) that links a date to each event
If that date is in the future, copy the entire row into the next empty row in a third worksheet (CurrentSalesPipeline)
Repeat this process until there are no more filled rows in the first spreadsheet.
I have created the following code, which when I mouse-over the text appears to give correct data, but which gives me the following error:
Run-time error '13': Type mismatch
Sub ShowUpcoming_Click()
Dim rCell As Range
Dim ws As Worksheet
Dim DateConf As Long
For Each rCell In Sheet1.Range("B3:B5000")
Set ws = Worksheets("CurrentSalesPipeline")
DateConf = Application.VLookup(rCell, Worksheets("Events_and_Activities").Range("A2:B12"), 2, False)
Range("A1").Value = DateConf
If CDate(DateConf) >= CDate((Date)) Then
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
.Cells(iRow, 1).Value = Worksheets("entiresalespipeline").Range("A3:A3").Value
.Cells(iRow, 2).Value = Worksheets("entiresalespipeline").Range("B3:B3").Value
.Cells(iRow, 3).Value = Worksheets("entiresalespipeline").Range("C3:C3").Value
.Cells(iRow, 4).Value = Worksheets("entiresalespipeline").Range("D3:D3").Value
.Cells(iRow, 5).Value = Worksheets("entiresalespipeline").Range("E3:E3").Value
.Cells(iRow, 6).Value = Worksheets("entiresalespipeline").Range("F3:F3").Value
.Cells(iRow, 7).Value = Worksheets("entiresalespipeline").Range("G3:G3").Value
.Cells(iRow, 8).Value = Worksheets("entiresalespipeline").Range("H3:H3").Value
.Cells(iRow, 9).Value = Worksheets("entiresalespipeline").Range("I3:I3").Value
.Cells(iRow, 10).Value = Worksheets("entiresalespipeline").Range("J3:J3").Value
.Cells(iRow, 11).Value = Worksheets("entiresalespipeline").Range("K3:K3").Value
.Cells(iRow, 12).Value = Worksheets("entiresalespipeline").Range("L3:L3").Value
.Cells(iRow, 13).Value = Worksheets("entiresalespipeline").Range("M3:M3").Value
.Cells(iRow, 14).Value = Worksheets("entiresalespipeline").Range("N3:N3").Value
.Cells(iRow, 15).Value = Worksheets("entiresalespipeline").Range("O3:O3").Value
.Cells(iRow, 16).Value = Worksheets("entiresalespipeline").Range("P3:P3").Value
.Cells(iRow, 17).Value = Worksheets("entiresalespipeline").Range("Q3:Q3").Value
.Cells(iRow, 18).Value = Worksheets("entiresalespipeline").Range("R3:R3").Value
.Cells(iRow, 19).Value = Worksheets("entiresalespipeline").Range("S3:S3").Value
.Cells(iRow, 20).Value = Worksheets("entiresalespipeline").Range("T3:T3").Value
End With
End If
Next rCell
End Sub
You're declaring DateConf as a Long:
Dim DateConf As Long
And then assigning it the result of the VLookup call:
DateConf = Application.VLookup(rCell, Worksheets("Events_and_Activities").Range("A2:B12"), 2, False)
That's a lot of assumptions to make: you're relying on VBA to perform an implicit conversion of the returned value to a Long, without knowing if the returned value will be a valid numeric.
What if the VLookup returns an empty string? What if it returned an #N/A error value?
Dim lookupResult As Variant
lookupResult = Application.VLookup(rCell, Worksheets("Events_and_Activities").Range("A2:B12"), 2, False)
If IsNumeric(lookupResult) Then
DateConf = CLng(lookupResult)
...
End If
Reduce the number of assumptions you're making, you'll reduce the number of potential issues by as much.
Thank you everyone for your very thoughtful and extremely helpful input!
After some tinkering, I managed to come up with the following, which does everything I need it to do.
Mat's Mug - your suggestion was perfect!
Tim Williams - I tried your single line solution to the copy/pasting, but it only copied the first line (headings) over every line in the range, so I stuck with what I had.
Sub ShowUpcoming_Click()
Dim lastrow As Long
Dim ws As Worksheet
Dim DateConf As Long
Dim r As Long
Set ws = Worksheets("CurrentSalesPipeline")
Dim lookupresult As Variant
'find last completed row of entire spreadsheet
lastrow = Worksheets("EntireSalesPipeline").Range("B" & Rows.Count).End(xlUp).Row
'From the first completed line (row 3) to last completed row
For r = 3 To lastrow
'lookup conference date from events/activities spreadsheet
lookupresult = Application.VLookup(Worksheets("EntireSalesPipeline").Cells(r, 2).Value, Worksheets("Events_and_activities").Range("A2:B13"), 2, False)
If IsNumeric(lookupresult) Then
DateConf = CLng(lookupresult)
End If
'If vlookup finds a date, then check to make sure it is in the future from when the button was hit.
If CDate(DateConf) >= CDate((Date)) Then
'If it is a future event, then copy that data into the current spreadsheet
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
.Cells(iRow, 1).Value = Worksheets("entiresalespipeline").Cells(r, 1).Value
.Cells(iRow, 2).Value = Worksheets("entiresalespipeline").Cells(r, 2).Value
.Cells(iRow, 3).Value = Worksheets("entiresalespipeline").Cells(r, 3).Value
.Cells(iRow, 4).Value = Worksheets("entiresalespipeline").Cells(r, 4).Value
.Cells(iRow, 5).Value = Worksheets("entiresalespipeline").Cells(r, 5).Value
.Cells(iRow, 6).Value = Worksheets("entiresalespipeline").Cells(r, 6).Value
.Cells(iRow, 7).Value = Worksheets("entiresalespipeline").Cells(r, 7).Value
.Cells(iRow, 8).Value = Worksheets("entiresalespipeline").Cells(r, 8).Value
.Cells(iRow, 9).Value = Worksheets("entiresalespipeline").Cells(r, 9).Value
.Cells(iRow, 10).Value = Worksheets("entiresalespipeline").Cells(r, 10).Value
.Cells(iRow, 11).Value = Worksheets("entiresalespipeline").Cells(r, 11).Value
.Cells(iRow, 12).Value = Worksheets("entiresalespipeline").Cells(r, 12).Value
.Cells(iRow, 13).Value = Worksheets("entiresalespipeline").Cells(r, 13).Value
.Cells(iRow, 14).Value = Worksheets("entiresalespipeline").Cells(r, 14).Value
.Cells(iRow, 15).Value = Worksheets("entiresalespipeline").Cells(r, 15).Value
.Cells(iRow, 16).Value = Worksheets("entiresalespipeline").Cells(r, 16).Value
.Cells(iRow, 17).Value = Worksheets("entiresalespipeline").Cells(r, 17).Value
.Cells(iRow, 18).Value = Worksheets("entiresalespipeline").Cells(r, 18).Value
.Cells(iRow, 19).Value = Worksheets("entiresalespipeline").Cells(r, 19).Value
.Cells(iRow, 20).Value = Worksheets("entiresalespipeline").Cells(r, 20).Value
End With
End If
'Repeat for next line in existing
Next r
End Sub
This is a great community. Thank you again!
Sarah

How to fix Runtime Error "91" in Excel

I have created a userform to enter data into worksheets for me. I am trying to code it where it will put the information into a selected worksheet. I have a combobox where I can select the sheet I want it to put the info in. I am trying to set the variable ws to the combobox value and I get a runtime error '91' and it says "object variable or with block not set". The code I am having problems with is below.
Private Sub bttn_Submit_Click()
Dim iRow As Long
Dim ws As Worksheet
'set the variable for the worksheet
ws = ComboBox1.Value
'find first empty row in database
iRow = Columns("A").Find("*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a date
If Trim(Me.txt_Date.Value) = "" Then
Me.txt_Date.SetFocus
MsgBox "Please enter a Date"
Exit Sub
End If
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
' .Unprotect Password:="password"
.Cells(iRow, 1).Value = Me.txt_Date.Value
.Cells(iRow, 2).Value = Me.txt_Invoice.Value
.Cells(iRow, 3).Value = Me.txt_Mileage.Value
.Cells(iRow, 5).Value = Me.CheckBox_Oil_Filter
.Cells(iRow, 7).Value = Me.CheckBox_Air_Filter
.Cells(iRow, 9).Value = Me.CheckBox_Primary_Fuel_Filter
.Cells(iRow, 11).Value = Me.CheckBox_Secondary_Fuel_Filter
.Cells(iRow, 13).Value = Me.CheckBox_Transmission_Filter
.Cells(iRow, 15).Value = Me.CheckBox_Transmission_Service
.Cells(iRow, 17).Value = Me.CheckBox_Wiper_Blades
.Cells(iRow, 19).Value = Me.CheckBox_Rotation_Rotated
.Cells(iRow, 21).Value = Me.CheckBox_New_Tires
.Cells(iRow, 23).Value = Me.CheckBox_Differential_Service
.Cells(iRow, 25).Value = Me.CheckBox_Battery_Replacement
.Cells(iRow, 27).Value = Me.CheckBox_Belts
.Cells(iRow, 29).Value = Me.CheckBox_Lubricate_Chassis
.Cells(iRow, 30).Value = Me.CheckBox_Brake_Fluid
.Cells(iRow, 31).Value = Me.CheckBox_Coolant_Check
.Cells(iRow, 32).Value = Me.CheckBox_Power_Steering_Check
.Cells(iRow, 33).Value = Me.CheckBox_A_Transmission_Check
.Cells(iRow, 34).Value = Me.CheckBox_Washer_Fluid_Check
' .Protect Password:="password"
End With
'clear the data
Me.txt_Date.Value = ""
Me.txt_Invoice.Value = ""
Me.txt_Mileage.Value = ""
Me.txt_Date.SetFocus
End Sub
I take your piece of code and explain you the inconsistency:
Dim ws As Worksheet
ws = ComboBox1.Value
The first line of code is declaring ws as a Worksheet object. Objects in VBA need the keyword Set to be set, and of course the data type needs to match the declaration.
On the other hand, in the second statement you're setting ws as ComboBox1.Value, which is a type String. So:
1) The reason why you get the error is that ws is a variable that, because of its declaration, expects to get a Worksheet object but you're instead trying to cast a String in it;
2) What you probably want to do is to have in ws the Worksheet object whose name is the ComboBox1.Value; this, in terms of code, should be written as follows:
Set ws = ThisWorkbook.Sheets(ComboBox1.Value)
You only need to replace the line ws = ComboBox1.Value with the line above.
Pay attention, in general: if you declare a variable of type X, it will always be able to cast only a type X object, unless you don't declare it at all (very bad practice, you would get errors after).

If Conditional Loop Criteria

I am trying to get my macro to find a specific value (a datediff value) and based on that value create a workbook that shows all the rows that have the same policy number (the unique criteria) and the datediff value I specified. The trouble is, my macro doesn't do it for just one policy number, it does it for all with the datediff value I specified. I tried the code below but i keep getting error messages. So Any help would be absolutely amazing!
Sub Invoice()
Dim s As Integer
s = 2
Dim t As Integer
t = 21
Dim r As Integer
r = 2
Dim policy As String
Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Sheets("sheet1")
policy = Workbooks("Woorkbook2.xlsm").Sheets("sheet1").Range("A" & r).Value
If Cells(s, 1).Value = policy Then
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
mini = Cells(s, 21).Value
If mini = "2" Then
Dim Newbook As Workbook
Set Newbook = Workbooks.Add
Workbooks("Workbook2.xlsm").Sheets("Invoice Template (2)").Copy Before:=Newbook.Sheets(1)
ActiveSheet.Name = "Current Invoice"
Workbooks("Workbook2.xlsm").Sheets("Sheet1").Activate
Do Until IsEmpty(Cells(s, 1))
Newbook.Sheets("Current Invoice").Cells(t, 2).Value = Cells(s, 10).Value
Newbook.Sheets("Current Invoice").Cells(t, 3).Value = Cells(s, 8).Value
Newbook.Sheets("Current Invoice").Cells(t, 7).Value = Cells(s, 11).Value
Loop
s = s + 1
r = r + 1
End If
End IF
End Sub
I believe the loop should go after the increment of 's':
Do Until IsEmpty(Cells(s, 1))
Newbook.Sheets("Current Invoice").Cells(t, 2).Value = Cells(s, 10).Value
Newbook.Sheets("Current Invoice").Cells(t, 3).Value = Cells(s, 8).Value
Newbook.Sheets("Current Invoice").Cells(t, 7).Value = Cells(s, 11).Value
s = s + 1
Loop

Excel Macro to Insert Data into Next Row

I can't seem to figure out how to offset the information into the next row down.
What I'm trying to do is insert the same information on the next row down every time this macro is executed. I'm using it as a cheap for of Learning Management System to track completion of eLearning courses, so every time a user executes the macro it will list the date, course, and their username.
The information in .Cells(1, 1) is incorrect, but I just used that to ensure the rest of the macro was working. At this point I just need to figure out how build in the logic to move down one row each time the macro is executed.
Thanks in advance for your help!
Sub Test()
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
strUserName = objNetwork.UserName
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("G:\Training\GPL\Test.xlsx")
objExcel.Application.DisplayAlerts = False
objExcel.Application.Visible = False
objWorkbook.Worksheets(1).Activate
objWorkbook.Worksheets(1).Cells(1, 1).Value = "GPL Overview"
objWorkbook.Worksheets(1).Cells(1, 2).Value = strUserName
objWorkbook.Worksheets(1).Cells(1, 3).Value = Date
'objExcel.ActiveWorkbook.Save "G:\Training\GPL\Test.xlsx"
objExcel.ActiveWorkbook.SaveAs "G:\Training\GPL\Test.xlsx"
objExcel.ActiveWorkbook.Close
'objExcel.ActiveWorkbook.
'objExcel.Application.Quit
'WScript.Echo "Finished."
'WScript.Quit
objExcel.Application.Quit
End Sub
This should fix it for you. Add this right after objWorkbook.Worksheets(1).Activate
Dim lastrow as Long
lastrow = objExcel.Worksheets(1).Range("A" & objExcel.Worksheets(1).Rows.Count).End(xlup).Row + 1
And change the next three lines to this:
objWorkbook.Worksheets(1).Cells(lastrow, 1).Value = "GPL Overview"
objWorkbook.Worksheets(1).Cells(lastrow, 2).Value = strUserName
objWorkbook.Worksheets(1).Cells(lastrow, 3).Value = Date
Update
Since it looks like you are running this code inside Excel itself, I am going to show you how you can really clean this code up and allow it to run faster and be easier to decipher. See the code below:
Option Explicit
Sub Test()
Dim strUserName as String
strUserName = ENVIRON("username")
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
Dim objWorkbook as Workbook
Set objWorkbook = Workbooks.Open("G:\Training\GPL\Test.xlsx")
Dim wks as Worksheet
Set wks = objWorkbook.Sheets(1)
With wks
Dim lastrow as Long
lastrow = .Range("A" & .Rows.Count).End(xlup).Row + 1
.Cells(lastrow, 1).Value = "GPL Overview"
.Cells(lastrow, 2).Value = strUserName
.Cells(lastrow, 3).Value = Date
End WIth
objWorkbook.Close True
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub
Thanks Scott Holtzman
I had a similar problem although i had to change some settings but after few long days you came to my rescue. Thanks indeed for help.
Here is a code i implemented and your reply helped me.
Private Sub Btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Save.Click
Dim MyExcel As Microsoft.Office.Interop.Excel.Application
MyExcel = New Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
wb = MyExcel.Workbooks.Open("C:\Users\IMTIYAZ\Desktop\try")
Dim ws As Microsoft.Office.Interop.Excel.Worksheet
ws = wb.Sheets("sheet1")
With ws
Dim irow As Long
irow = ws.Range("A65536").End(Excel.XlDirection.xlUp).Offset(1, 0).Select
irow = ws.Range("A" & ws.Rows.Count).End(Excel.XlDirection.xlUp).Row + 1
ws.Cells(irow, 1).Value = Me.txtSn.Text
ws.Cells(irow, 2).Value = Me.txtNa.Text
ws.Cells(irow, 3).Value = Me.txtGpf.Text
ws.Cells(irow, 4).Value = Me.txtBa.Text
ws.Cells(irow, 5).Value = Me.txtBn.Text
ws.Cells(irow, 6).Value = Me.txtAp.Text
ws.Cells(irow, 7).Value = Me.txtBp.Text
ws.Cells(irow, 8).Value = Me.txtGp.Text
ws.Range(irow, 9).Formula = ("=$G$3+$H$3")
Me.Lbl_Tt.Text = ws.Cells(irow, 9).Value
ws.Cells(irow, 10).Value = Me.txtPp.Text
ws.Cells(irow, 11).Value = Me.txtDa.Text
ws.Cells(irow, 12).Value = Me.txtMa.Text
ws.Cells(irow, 13).Value = Me.txtRa.Text
ws.Cells(irow, 14).Value = Me.txtCa.Text
ws.Cells(irow, 15).Value = Me.txtOa.Text
ws.Cells(irow, 16).Formula = ("=i3+J3+K3+L3+M3+N3+O3")
Me.Lbl_Gt.Text = ws.Cells(irow, 16).Value
ws.Cells(irow, 17).Value = Me.txtFa.Text
ws.Cells(irow, 18).Formula = ("=P3-Q3")
Me.Lbl_Naf.Text = ws.Cells(irow, 18).Value
ws.Cells(irow, 19).Value = Me.txtSf.Text
ws.Cells(irow, 20).Value = Me.txtRf.Text
ws.Cells(irow, 21).Value = Me.txtSi1.Text
ws.Cells(irow, 22).Value = Me.txtSi2.Text
ws.Cells(irow, 23).Value = Me.txtSi3.Text
ws.Cells(irow, 24) = ("=S3+T3+V3+W3+U3")
Me.Lbl_Td.Text = ws.Cells(irow, 24).Value
ws.Cells(irow, 25).Formula = ("=R3-X3")
Me.Lbl_Nad.Text = ws.Cells(irow, 25).Value
ws.Cells(irow, 26).Value = Me.txtHl.Text
ws.Cells(irow, 27).Value = Me.txtCsc.Text
ws.Cells(irow, 28).Value = Me.txtMr.Text
ws.Cells(irow, 29).Value = Me.txtIt.Text
ws.Cells(irow, 30).Formula = ("=Y3-(AC3+Z3+AA3+AB3)")
Me.Lbl_Np.Text = ws.Cells(irow, 30).Value
MessageBox.Show("The last row in Col A of Sheet1 which has data is " & irow)
End With
MyExcel.Quit()
MyExcel = Nothing
Me.Update()
End Sub
End Class