I have a userform that enters data into columns I to S but sometimes all data points does not need to be entered. My probably is I only have the last row counting up on column I so if I have data in J through S, they would get replaced with the next set of data that has data in column I.
What I need help is coding for the last row of all columns or the next blank row of all columns. Thanks.
My code:
Private Sub cmd_EnterData_Click()
Dim iRow As Long
Dim Lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("FirstShift")
Lastrow = ws.Range("i101").End(xlUp).Row
'find first empty row in database
For iRow = 16 To Lastrow
If ws.Cells(iRow, 9) = "" And ws.Cells(iRow, 10) = "" Then
ws.Cells(iRow, 9).Value = Me.textbox_Lane1.Value
ws.Cells(iRow, 10).Value = Me.textbox_Lane2.Value
ws.Cells(iRow, 11).Value = Me.textbox_Lane3.Value
ws.Cells(iRow, 12).Value = Me.textbox_Lane4.Value
ws.Cells(iRow, 13).Value = Me.textbox_Lane5.Value
ws.Cells(iRow, 14).Value = Me.textbox_Lane6.Value
ws.Cells(iRow, 15).Value = Me.textbox_Lane7.Value
ws.Cells(iRow, 16).Value = Me.textbox_Length.Value
ws.Cells(iRow, 17).Value = Me.textbox_SheetCount.Value
ws.Cells(iRow, 18).Value = Me.cbchecktype.Value
ws.Cells(iRow, 19).Value = Me.cbchecktype1.Value
End If
Next iRow
If checkbox_Retest.Value = False And Me.textbox_Lane1.Value = "" Then
'do nothing
Me.textbox_Lane1.SetFocus
MsgBox "ENTER LANE 1 WIDTH!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.textbox_Length.Value = "" Then
'do nothing
Me.textbox_Length.SetFocus
MsgBox "ENTER YOUR LENGTH!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.textbox_SheetCount.Value = "" Then
'do nothing
Me.textbox_SheetCount.SetFocus
MsgBox "ENTER THE SHEETCOUNT!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.cbchecktype.Value = "" Then
'do nothing
Me.cbchecktype.SetFocus
MsgBox "ENTER 'PASS' OR 'FAIL' FOR PERF CHECK!!"
Exit Sub
Select Case checktype
Case Trim(Me.cbchecktype.Value) = "PASS"
checktype = "PASS"
Case Trim(Me.cbchecktype.Value) = "FAIL"
checktype = "FAIL"
End Select
End If
If checkbox_Retest.Value = False And Me.cbchecktype1.Value = "" Then
'do nothing
Me.cbchecktype1.SetFocus
MsgBox "ENTER 'PASS' OR 'FAIL' FOR SLITHER CHECK!!"
Exit Sub
Select Case checktype1
Case Trim(Me.cbchecktype1.Value) = "PASS"
checktype1 = "PASS"
Case Trim(Me.cbchecktype1.Value) = "FAIL"
checktype1 = "FAIL"
End Select
End If
With ws
.Unprotect Password:="password"
.Cells(iRow, 9).Value = Me.textbox_Lane1.Value
.Cells(iRow, 10).Value = Me.textbox_Lane2.Value
.Cells(iRow, 11).Value = Me.textbox_Lane3.Value
.Cells(iRow, 12).Value = Me.textbox_Lane4.Value
.Cells(iRow, 13).Value = Me.textbox_Lane5.Value
.Cells(iRow, 14).Value = Me.textbox_Lane6.Value
.Cells(iRow, 15).Value = Me.textbox_Lane7.Value
.Cells(iRow, 16).Value = Me.textbox_Length.Value
.Cells(iRow, 17).Value = Me.textbox_SheetCount.Value
.Cells(iRow, 18).Value = Me.cbchecktype.Value
.Cells(iRow, 19).Value = Me.cbchecktype1.Value
.Protect Password:="password"
End With
'clear the data
Me.textbox_Lane1.Value = ""
Me.textbox_Lane2.Value = ""
Me.textbox_Lane3.Value = ""
Me.textbox_Lane4.Value = ""
Me.textbox_Lane5.Value = ""
Me.textbox_Lane6.Value = ""
Me.textbox_Lane7.Value = ""
Me.textbox_Length.Value = ""
Me.textbox_SheetCount.Value = ""
Me.cbchecktype.Value = ""
Me.cbchecktype1.Value = ""
Me.checkbox_Retest.Value = False
Me.Hide
End Sub
A.S.H, I've tried your code and same thing is happening BUT I don't think its the code. Please see pictures of before and after. I think the problem is my IF STATEMENT:
"If ws.Cells (iRow, 9) = "" And ws.Cells(iRow, 10) = "" Then
As you can see, the function works fine when I have an item in columns 9 (I) and 10 (J), but when I put no data in those two columns then it gets replaced with whatever data I've entered on my userform as long as it includes data in columns 9 or 10..Thoughts on correcting this?
The following gets you the last non-empty row considering all columns:
Lastrow = ws.UsedRange.Find("*", , , , xlByRows, xlPrevious).Row
You can also restrict it to a set of columns, by replacing .UsedRange to the specific columns range, i.e. the following gets you the last non-empty row in columns G to AB:
Lastrow = ws.Range("G:AB").Find("*", , , , xlByRows, xlPrevious).Row
Add these two dim statements.
Dim ColumnCount As Integer
Dim x As Long
Change your code:
Lastrow = ws.Range("i101").End(xlUp).Row
to:
Lastrow = 0
For ColumnCount = 0 To 10
x = ws.Range("I101").Offset(0, ColumnCount).End(xlUp).Row
If x > Lastrow Then Lastrow = x
Next ColumnCount
edit:
This for loop always start on 16. Is it maybe supposed to start on Lastrow+1?
For iRow = 16 To Lastrow
Related
I've got a problem with my code when I am doing a logical statement with 2 textboxes.
I just want to check before I close my userform if Textbox4's value is smaller then the absolute of textbox6's value.
If Me.TextBox4.Value <= Abs(Me.TextBox6.Value) Then
MyInput = MsgBox("Warning. The absolute max or min signal is bigger then Full Scale. Do you want to continue anyway?", vbYesNo)
(When testing the code the msgbox does not activate when textbox4.value is smaller right now.)
Am I missing something? Is this not the correct way to write it?
Thanks for any help.
Here is the full Code:
Private Sub selectcmd1_Click()
Dim MyInput
Dim ws As Worksheet
Set ws = Worksheets("InputS&T")
If Me.TextBox4.Value <= Abs(Me.TextBox6.Value) Then
MyInput = MsgBox("Warning. The absolute max or min signal is bigger then Full Scale. Do you want to continue anyway?", vbYesNo)
If MyInput = vbYes Then
'find first empty row in database---------------------------------
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, searchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
ws.Cells(iRow, 3).Value = Me.ComboBox1.Value
ws.Cells(iRow, 4).Value = Me.TextBox1.Value
ws.Cells(iRow, 5).Value = Me.TextBox2.Value
ws.Cells(iRow, 6).Value = Me.TextBox3.Value
ws.Cells(iRow, 7).Value = Me.TextBox4.Value
ws.Cells(iRow, 8).Value = Me.TextBox5.Value
ws.Cells(iRow, 9).Value = Me.TextBox6.Value
ws.Cells(iRow, 10).Value = Me.TextBox7.Value
ws.Cells(iRow, 11).Value = Me.TextBox8.Value
Unload Me
BeginRow = 13
EndRow = 40
ChkCol = 3
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = "" Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
'-------------------------------------------------------------
Else
End If
Else
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, searchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
ws.Cells(iRow, 3).Value = Me.ComboBox1.Value
ws.Cells(iRow, 4).Value = Me.TextBox1.Value
ws.Cells(iRow, 5).Value = Me.TextBox2.Value
ws.Cells(iRow, 6).Value = Me.TextBox3.Value
ws.Cells(iRow, 7).Value = Me.TextBox4.Value
ws.Cells(iRow, 8).Value = Me.TextBox5.Value
ws.Cells(iRow, 9).Value = Me.TextBox6.Value
ws.Cells(iRow, 10).Value = Me.TextBox7.Value
ws.Cells(iRow, 11).Value = Me.TextBox8.Value
Unload Me
BeginRow = 13
EndRow = 40
ChkCol = 3
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = "" Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
End If
End Sub
You should convert the values to Double and then do the check:
Dim dblVar1 As Double
Dim dblVar2 As Double
dblVar1 = CDbl(Me.TextBox4.Value)
dblVar2 = CDbl(Me.TextBox6.Value)
If dblVar1 <= Abs(dblVar2) Then
MyInput = MsgBox("Warning. The absolute max or min signal is bigger then Full Scale. Do you want to continue anyway?", vbYesNo)
End If
Reason is that whilst you can do something like this in VBA:
If "10" < "6" Then
'...
End If
It won't give the expected result because "10" < "6" is True because it is a text comparison.
Use
If CLng(Me.TextBox1.Value) <= CLng(Abs(Me.TextBox2.Value)) Then
instead of
If Me.TextBox4.Value <= Abs(Me.TextBox6.Value) Then
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim myinput As Variant
If Me.TextBox4.Value <= Abs(Me.TextBox6.Value) Then
myinput = MsgBox("Warning. The absolute max or min signal is bigger then Full Scale. Do you want to continue anyway?", vbYesNo)
End If
If myinput <> vbYes Then Cancel = True
End Sub
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
So currently I am trying to come up with a if statement. Basically if A3 has any text value I want it to equal awesome. I want to loop this command with the last column in mind.
Sub Criteria
If Range("A2") = "Feedback" And Range("A3") = "**" Then
Range("A1") = "Awesome"
Else
Range("A1") = ""
End If
End sub
(This is the code I came up with can someone help me make it cleaner/faster)
Sub Status()
lastrow = Rows(Rows.Count).End(xlUp).Row
For i = 2 To lastrow
If Cells(i, 1) = "Onsite" And Not IsEmpty(Cells(i, 2)) Then
Cells(i, 3) = "Feedback"
Else
If Cells(i, 1) = "Phone" And Not IsEmpty(Cells(i, 2)) Then
Cells(i, 3) = "Feedback"
Else
If Cells(i, 1) = "Phone" And IsEmpty(Cells(i, 2)) Then
Cells(i, 3) = "Pending Next Step"
Else
If Cells(i, 1) = "Onsite" And IsEmpty(Cells(i, 2)) Then
Cells(i, 3) = "Pending Decision"
End If
End If
End If
End If
Next i
End Sub
Try using Option Explicit also set your worksheet so your not running the code on wrong sheet or to avoid a error
Option Explicit
Public Sub Status()
Dim Sht As Worksheet
Dim rng As Range
Set Sht = ThisWorkbook.Sheets("Sheet1")
For Each rng In Sht.Range("A2", Sht.Range("A9999").End(xlUp))
Debug.Print rng.Address ' print on immed win
DoEvents ' For Debuging
If rng.Value = "Onsite" And rng.Offset(0, 1).Value > 0 Then
rng.Offset(0, 2).Value = "Feedback"
ElseIf rng.Value = "Onsite" And rng.Offset(0, 1).Value = "" Then
rng.Offset(0, 2).Value = "Pending Decision"
End If
If rng.Value = "Phone" And rng.Offset(0, 1).Value > 0 Then
rng.Offset(0, 2).Value = "Feedback"
ElseIf rng.Value = "Phone" And rng.Offset(0, 1).Value = "" Then
rng.Offset(0, 2).Value = "Pending Next Step"
End If
Next
Set Sht = Nothing
Set rng = Nothing
End Sub
Range.Offset Property (Excel)
Syntax: expression.Offset(RowOffset, ColumnOffset)
Returns a Range object that represents a range that?s offset from the specified range.
I'm a beginner when it comes to VB and i'm having a bit of trouble developing a form. What i'm trying to achieve is for the form, on click,to:
Validate four text boxes and nine combo boxes, ensuring they all have values before being submitted to an MS Excel sheet
If there are null fields, a message box telling the user "Text box(es) and/or drop down box(es) must contain data" (or something to that effect) should appear
Assuming all fields have values, the workbook must be unprotected
Data must then be input into the Excel sheet (each form submission equals one row of data, which must not be overwritten by subsequent entries).
The workbook must be protected again
The form must be hidden once all actions are complete.
Here's my code so far. I'm sure its very simplistic and can be made more efficient -- any suggestions welcome. Thank you in advance for your help.
Private Sub CommandButton1_Click()
Sheet2.Unprotect
Dim LastRow As Object
Set LastRow = Sheet2.Range("a65536").End(xlUp)
LastRow.Offset(1, 0).Value = ExpRecDrop.Text
Set LastRow = Sheet2.Range("b65536").End(xlUp)
LastRow.Offset(1, 0).Value = CPName.Text
Set LastRow = Sheet2.Range("c65536").End(xlUp)
LastRow.Offset(1, 0).Value = ConEntDrop.Text
Set LastRow = Sheet2.Range("d65536").End(xlUp)
LastRow.Offset(1, 0).Value = ResTypDrop.Text
Set LastRow = Sheet2.Range("e65536").End(xlUp)
LastRow.Offset(1, 0).Value = LangDrop.Text
Set LastRow = Sheet2.Range("f65536").End(xlUp)
LastRow.Offset(1, 0).Value = WritDrop.Text
Set LastRow = Sheet2.Range("g65536").End(xlUp)
LastRow.Offset(1, 0).Value = OwnerDrop.Text
Set LastRow = Sheet2.Range("i65536").End(xlUp)
LastRow.Offset(1, 0).Value = BiRiDrop.Text
Set LastRow = Sheet2.Range("j65536").End(xlUp)
LastRow.Offset(1, 0).Value = ERTextBox.Text
Set LastRow = Sheet2.Range("k65536").End(xlUp)
LastRow.Offset(1, 0).Value = DueDatDrop.Text
Set LastRow = Sheet2.Range("l65536").End(xlUp)
LastRow.Offset(1, 0).Value = SubTypDrop.Text
Set LastRow = Sheet2.Range("o65536").End(xlUp)
LastRow.Offset(1, 0).Value = CommText.Text
Sheet2.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Me.Hide
MsgBox "One record written to the 2014 tab"
End Sub
This is my first post so please let me know if you need any more information.
You could try this. I've created a Boolean (True/False) variable to determine if the update should happen, if any of your controls is blank it will change to false and skip the update. The other change I've made is instead of repeatedly working out the next available cell is to work out the next free row number once and use that as a reference to the row to populate.
Private Sub CommandButton1_Click()
Dim LastRow As Long
Dim isOk As Boolean
sheet2.Unprotect
' Create boolean variable as true and check controls for data, if any data missing, change to false
isOk = True
If ExpRecDrop.Text = "" Then isOk = False
If CPName.Text = "" Then isOk = False
If ConEntDrop.Text = "" Then isOk = False
If ResTypDrop.Text = "" Then isOk = False
If LangDrop.Text = "" Then isOk = False
If WritDrop.Text = "" Then isOk = False
If OwnerDrop.Text = "" Then isOk = False
If BiRiDrop.Text = "" Then isOk = False
If ERTextBox.Text = "" Then isOk = False
If DueDatDrop.Text = "" Then isOk = False
If SubTypDrop.Text = "" Then isOk = False
If CommText.Text = "" Then isOk = False
' if boolean value is still true, update the sheet, else display error message
If Not isOk Then
With sheet2
' get number of the next free row
Set LastRow = .Range("a65536").End(xlUp).Row + 1
' Populate the sheet
.Cells(LastRow, 1).Value = ExpRecDrop.Text
.Cells(LastRow, 2).Value = CPName.Text
.Cells(LastRow, 3).Value = ConEntDrop.Text
.Cells(LastRow, 4).Value = ResTypDrop.Text
.Cells(LastRow, 5).Value = LangDrop.Text
.Cells(LastRow, 6).Value = WritDrop.Text
.Cells(LastRow, 7).Value = OwnerDrop.Text
.Cells(LastRow, 8).Value = BiRiDrop.Text
.Cells(LastRow, 9).Value = ERTextBox.Text
.Cells(LastRow, 10).Value = DueDatDrop.Text
.Cells(LastRow, 11).Value = SubTypDrop.Text
.Cells(LastRow, 12).Value = CommText.Text
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
' Hide userform and confirm
Me.Hide
MsgBox "One record written to the 2014 tab"
Else
' Error message
MsgBox "Some data missing"
End If
End Sub
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