Method or data not found in simple VBA program - vba

I have been trying to get a simple task done in VBA; I am writing a GUI for an excel spreadsheet that is holding inventory. It enables users to input info into the GUI click ok and all of what they typed in is saved into the excel document.
However every time I run the below code I get a compiler error "Method or data not found".
Private Sub Label1_Click()
End Sub
Private Sub Label4_Click()
End Sub
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(lRow, 1).Value = Me.AssetType.Value
.Cells(lRow, 2).Value = Me.AssetNumber.Value
.Cells(lRow, 4).Value = Me.Description.Value
.Cells(lRow, 5).Value = Me.SerialNbr.Value
.Cells(lRow, 6).Value = Me.CurrentUse.Value
.Cells(lRow, 7).Value = Me.DateRec.Value
.Cells(lRow, 8).Value = Me.FundingSource.Value
.Cells(lRow, 9).Value = Me.Manufacturer.Value
.Cells(lRow, 10).Value = Me.Model.Value
.Cells(lRow, 11).Value = Me.Contract.Value
.Cells(lRow, 12).Value = Me.Status.Value
.Cells(lRow, 13).Value = Me.Room.Value
.Cells(lRow, 14).Value = Me.OfficeLocation.Value
.Cells(lRow, 19).Value = Me.Custodian.Value
.Cells(lRow, 20).Value = Me.ExcessedDate.Value
.Cells(lRow, 21).Value = Me.ExcessAuthorization.Value
.Cells(lRow, 22).Value = Me.Comments.Value
.Cells(lRow, 23).Value = Me.OutDate.Value
End With
'Clear input controls.
Me.AssetType.Value = ""
Me.AssetNumber.Value = ""
Me.Description.Value = ""
Me.SerialNbr.Value = ""
Me.CurrentUse.Value = ""
Me.DateRec.Value = ""
Me.FundingSource.Value = ""
Me.Manufacturer.Value = ""
Me.Model.Value = ""
Me.Contract.Value = ""
Me.Status.Value = ""
Me.Room.Value = ""
Me.OfficeLocation.Value ""
Me.Custodian.Value ""
Me.ExcessedDate.Value ""
Me.ExcessAuthorization.Value ""
Me.Comments.Value ""
Me.OutDate.Value ""
End Sub
Private Sub cmdClose_Click()
'Close UserForm.
Unload Me
End Sub
Private Sub Cmdbutton_add_Click()
End Sub
Private Sub Model_Click()
End Sub
Private Sub UserForm_Click()
End Sub

Get the value of a text box by the .text property of the text box.
If your text box is named AssetType it would be
.Cells(lRow, 1).Value = AssetType.Text
You may need to use the form name.
.Cells(lRow, 1).Value = UserForm1.AssetType.Text

Related

VBA userform only works once

I am Creating a Userform in Excel 2013 to make entering data into an inventory file easier at my workplace. The userform
(Userform and Code Picture) will run and I can enter multiple new items (Ignore the Update existing entry button I haven't started on that yet) into the inventory file, but once I close the form opening it again causes the error
"Run time error '-2147417848 (80010108)' Method 'Value" of 'Range' failed"
to appear and the Debug seems to highlight a different object each time I try it.
Private Sub New_Data_Click()
Dim lRow As Long
Dim lPart As Long
Dim ws As Worksheet
Set ws = Worksheets("Worksheet")
lRow = ws.Cells.Find(What:"*", SearchOrder:=xlRows,
SearchDirection:=xlPrevious, LookIn=xlValues).Row + 1
With ws
.Cells(lRow, 1).Value = Me.EnterChemical.Value
.Cells(lRow, 2).Value = Me.EnterNotes.Value
.Cells(lRow, 3).Value = Me.EnterLot.Value
.Cells(lRow, 4).Value = Me.EnterItem.Value
.Cells(lRow, 5).Value = Me.SpinButton1.Value
.Cells(lRow, 6).Value = Me.EnterManf.Value
.Cells(lRow, 7).Value = Me.EnterLoc.Value
.Cells(lRow, 8).Value = Me.EnterTare.Value
.Cells(lRow, 9).Value = Me.EnterGross.Value
.Cells(lRow, 11).Value = Me.EnterUnit.Value
.Cells(lRow, 12).Value = Me.EnterExp.Value
.Cells(lRow, 16).Value = Date
If Me.Expire.Value = True Then
.Cells(lRow, 13).Value = "Expire"
ElseIf Me.Retest.Value = True Then
.Cells(lRow, 13).Value = "Retest"
EndIf
If Me.API.Value = True Then
.Cells(lRow, 1).Value = "Yes"
EndIf
EndWith
EndSub
Private Sub Closefrm.Click()
Unload Me
End Sub
Private Sub SpinButton1_Change()
Reference_no.Caption = SpinButton1.Value
So this all works one time but I dont know why it fails the second. Do I need to have a different closing function on the button or I am completely missing something.

How do i get to next form in visual basic

I am trying to get to the next form for my excel macro
right now I have a login form that has user name and password
that works when I press login it goes to next form.
but when I go to my next form and type the information in it it closes and doesn't pull up the next forum.
Can someone please explain to me what I am doing wrong? why wont my next form pop up?
Private Sub cmdAdd_Click()
'Dim iRow As Long
'Dim ws As Worksheet
Set ws = Worksheets("D544 Back Panel")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a part number
If Trim(Me.txtPrd.Value) = "" Then
Me.txtPrd.SetFocus
MsgBox "Please enter a Production Number"
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.txtDate.Value
.Cells(iRow, 2).Value = Me.txtHrs.Value
.Cells(iRow, 3).Value = Me.txtPrd.Value
.Cells(iRow, 4).Value = Me.txtSrp.Value
.Cells(iRow, 5).Value = Me.txtOper.Value
' .Protect Password:="Password"
End With
'clear the data
Me.txtDate.Value = ""
Me.txtHrs.Value = ""
Me.txtPrd.Value = ""
Me.txtSrp.Value = ""
Me.txtOper.Value = ""
Me.txtPrd.SetFocus
Unload Me
End Sub
Private Sub cmdSubmit_Click()
'Dim iRow As Long
'Dim ws As Worksheet
Set ws = Worksheets("Scrap")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a part number
If Trim(Me.txtPress.Value) = "" Then
Me.txtPress.SetFocus
MsgBox "Please enter press scrap"
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.txtDelam.Value
.Cells(iRow, 2).Value = Me.txtCuts.Value
.Cells(iRow, 3).Value = Me.txtBurns.Value
.Cells(iRow, 4).Value = Me.txtDents.Value
.Cells(iRow, 5).Value = Me.txtStaple.Value
.Cells(iRow, 6).Value = Me.txtGlue.Value
.Cells(iRow, 7).Value = Me.txtPress.Value
' .Protect Password:="Password"
End With
'clear the data
Me.txtDelam.Value = ""
Me.txtCuts.Value = ""
Me.txtBurns.Value = ""
Me.txtDents.Value = ""
Me.txtStaple.Value = ""
Me.txtGlue.Value = ""
Me.txtPress.Value = ""
Me.txtPress.SetFocus
Unload Me
End Sub
I used
Formname1.Hide
Formname2.Show
This seemed to solve my issue. Thanks for the help guys. Sorry I am very new to this site.

Automatically check checkboxes when .Search vba in userform?

So I have the following code below: and what i want to focus on is the Private Sub Search_Click(). Currently when I search someone's surname it populate the textboxes automatically. Is it possible to have a search box that populates the checkboxes automatically? so for example, if the person belongs to 6/8 checkboxes, and i click search 6/8 checkboxes would be checkmarked? and is it possible to do the same thing with ListBox1_Click()? so when i click on person's name from listbox, it also popoulates the checkbocxes automatically depending on which checkbox that person belongs to?
EDIT
Private Sub Search_Click() 'only searches in master tab right now need to search from all worksheets
Dim Name As String
Dim f As Range
Dim r As Long
Dim ws As Worksheet
Dim s As Integer
Dim FirstAddress As String
Name = surname.Value
For Each ws In ActiveWorkbook.Sheets
With ws
Set f = Range("A:A").Find(what:=Name, LookIn:=xlValues)
If Not f Is Nothing Then
If cb.Name = ws.Name Then
cb.Value = True
End If
Next
With Me
firstname.Value = f.Offset(0, 1).Value
tod.Value = f.Offset(0, 2).Value
program.Value = f.Offset(0, 3).Value
email.Value = f.Offset(0, 4).Text
officenumber.Value = f.Offset(0, 5).Text
cellnumber.Value = f.Offset(0, 6).Text
Is this what it would look like...? I can't get it to work though?
Private Sub CommandButton1_Click()
MsgBox "Directorate has been added", vbOKOnly
Dim ctrl As control
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
'Pass this CheckBox to the subroutine below:
TransferValues ctrl
End If
Next
TransferMasterValue
End Sub
Sub TransferValues(cb As MSForms.CheckBox)
Dim ws As Worksheet
Dim emptyRow As Long
If cb Then
'Define the worksheet based on the CheckBox.Name property:
Set ws = Sheets(Left(cb.Name, 15))
emptyRow = WorksheetFunction.CountA(ws.range("A:A")) + 1
With ws
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 6).Value = officenumber.Value
.Cells(emptyRow, 7).Value = cellnumber.Value
End With
End If
End Sub
Sub TransferMasterValue()
Dim allChecks As String
Dim ws As Worksheet
'Iterate through the checkboxes concatenating a string of all names
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl Then
allChecks = allChecks & ctrl.Name & ""
End If
End If
Next
'If you have at least one transfer to the Master sheet
If Len(allChecks) > 0 Then
Set ws1 = Sheets("Master")
emptyRow = WorksheetFunction.CountA(range("A:A")) + 1
With ws1
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 7).Value = officenumber.Value
.Cells(emptyRow, 8).Value = cellnumber.Value
.Cells(emptyRow, 6).Value = Left(allChecks, Len(allChecks) - 1)
End With
End If
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
End Sub
Private Sub CommandButton3_Click()
surname.Value = ""
firstname.Value = ""
tod.Value = ""
program.Value = ""
email.Value = ""
officenumber.Value = ""
cellnumber.Value = ""
PACT.Value = False
PrinceRupert.Value = False
WPM.Value = False
Montreal.Value = False
TET.Value = False
TC.Value = False
US.Value = False
Other.Value = False
End Sub
Private Sub ListBox1_Click()
Dim r As Long
With Me.ListBox1
With Me
.surname.Value = .ListBox1.List(.ListBox1.ListIndex, 0)
.firstname.Value = .ListBox1.List(.ListBox1.ListIndex, 1)
.tod.Value = .ListBox1.List(.ListBox1.ListIndex, 2)
.program.Value = .ListBox1.List(.ListBox1.ListIndex, 3)
.email.Value = .ListBox1.List(.ListBox1.ListIndex, 4)
.officenumber.Value = .ListBox1.List(.ListBox1.ListIndex, 5)
.cellnumber.Value = .ListBox1.List(.ListBox1.ListIndex, 6)
End With
End With
End Sub
Private Sub Search_Click() 'only searches in master tab right now need to search from all worksheets
Dim Name As String
Dim f As range
Dim r As Long
Dim ws As Worksheet
Dim s As Integer
Dim FirstAddress As String
Name = surname.Value
With ws
Set f = range("A:A").Find(what:=Name, LookIn:=xlValues)
If Not f Is Nothing Then
With Me
firstname.Value = f.Offset(0, 1).Value
tod.Value = f.Offset(0, 2).Value
program.Value = f.Offset(0, 3).Value
email.Value = f.Offset(0, 4).Text
officenumber.Value = f.Offset(0, 5).Text
cellnumber.Value = f.Offset(0, 6).Text
End With
findnext
FirstAddress = f.Address
Do
s = s + 1
Set f = range("A:A").findnext(f)
Loop While Not f Is Nothing And f.Address <> FirstAddress
If s > 1 Then
Select Case MsgBox("There are " & s & " instances of " & Name, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
findnext
Case vbCancel
End Select
End If
Else: MsgBox Name & "Not Listed"
End If
End With
End Sub
Sub findnext()
Dim Name As String
Dim f As range
Dim ws As Worksheet
Dim s As Integer
Dim findnext As range
Name = surname.Value
Me.ListBox1.Clear
Set f = range("A:A").Find(what:=Name, LookIn:=xlValues)
Set findnext = f
With ListBox1
Do
Debug.Print findnext.Address
Set findnext = range("A:A").findnext(findnext)
.AddItem findnext.Value
.List(0, 1) = findnext.Offset(0, 1).Value
.List(0, 2) = findnext.Offset(0, 2).Value
.List(0, 3) = findnext.Offset(0, 3).Value
.List(0, 4) = findnext.Offset(0, 4).Value
.List(0, 5) = findnext.Offset(0, 5).Value
.List(0, 6) = findnext.Offset(0, 6).Value
.List(0, 7) = findnext.Offset(0, 6).Value
Loop While findnext.Address <> f.Address
End With
End Sub
It doesnt seem like a coding problem, I saw the code where you set the textboxes text. Basically, instead of setting textboxes text, you'd be setting the checkbox. the code to to that is ChkCheck.value = true.
leave a comment if theres a deeper issue and ill make an edit
edit
To search all sheets,
I would put a loop in there like
for each ws in ActiveWorkbook.Sheets
and then put your search in there.
and then, after the if f is not nothing then
loop through all controls, and check to see if the controls name = the sheets name. -
if ctrl.name = ws.name then
ctrl.value = true
end if
like that
so then each time the sheets loop runs, if it finds the specific name, the checkbox accosiated with the particular sheet will check.

how to use "For each" loop in vba?

Sub Findnext()
Dim Name As String
Dim f As range
Dim ws As Worksheet
Dim s As Integer
Name = surname.Value
'currently only searching one instance...doesn't loop and find the rest
Me.ListBox1.Clear
Set f = Cells.Find(what:=Name, LookIn:=xlValues)
Set findnext = f
With ListBox1
Do
Debug.Print findnext.Address
Set findnext = Cells.findnext(findnext)
.AddItem f.Value
.List(0, 1) = f.Offset(0, 1).Value
.List(0, 2) = f.Offset(0, 2).Value
.List(0, 3) = f.Offset(0, 3).Value
.List(0, 4) = f.Offset(0, 4).Value
.List(0, 5) = f.Offset(0, 5).Value
.List(0, 6) = f.Offset(0, 6).Value
Loop While findnext.Address <> f.Address
End With
End Sub
how do i make this code loop so that it would find multiple f values? essentailly, I have a search button and it promopts "There are 3 instances" and in the list box, it should list out the 3 instances (eg. same name).
I tried using For each f and next f in the code above, but it still only picks one f.value and doesn't pick any other cells with same name....
EDIT:
i've added the loop function but now in the list box, it only lists the person's name instead of listing all the offset values. is the offset not applied to the loop? or is it because it's only looking for f? which is the name it's looking for?
EDIT: The coding I have done so far...
Private Sub CommandButton1_Click()
MsgBox "Directorate has been added", vbOKOnly
Dim ctrl As control
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
'Pass this CheckBox to the subroutine below:
TransferValues ctrl
End If
Next
TransferMasterValue
End Sub
Sub TransferValues(cb As MSForms.CheckBox)
Dim ws As Worksheet
Dim emptyRow As Long
If cb Then
'Define the worksheet based on the CheckBox.Name property:
Set ws = Sheets(Left(cb.Name, 15))
emptyRow = WorksheetFunction.CountA(ws.range("A:A")) + 1
With ws
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 6).Value = officenumber.Value
.Cells(emptyRow, 7).Value = cellnumber.Value
End With
End If
End Sub
Sub TransferMasterValue()
Dim allChecks As String
Dim ws As Worksheet
'Iterate through the checkboxes concatenating a string of all names
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl Then
allChecks = allChecks & ctrl.Name & ""
End If
End If
Next
'If you have at least one transfer to the Master sheet
If Len(allChecks) > 0 Then
Set ws1 = Sheets("Master")
emptyRow = WorksheetFunction.CountA(range("A:A")) + 1
With ws1
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 7).Value = officenumber.Value
.Cells(emptyRow, 8).Value = cellnumber.Value
.Cells(emptyRow, 6).Value = Left(allChecks, Len(allChecks) - 1)
End With
End If
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
End Sub
Private Sub CommandButton3_Click()
surname.Value = ""
firstname.Value = ""
tod.Value = ""
program.Value = ""
email.Value = ""
officenumber.Value = ""
cellnumber.Value = ""
PACT.Value = False
PrinceRupert.Value = False
WPM.Value = False
Montreal.Value = False
TET.Value = False
TC.Value = False
US.Value = False
Other.Value = False
End Sub
Private Sub ListBox1_Click()
Dim r As Long
With Me.ListBox1
With Me
.surname.Value = .ListBox1.List(.ListBox1.ListIndex, 0)
.firstname.Value = .ListBox1.List(.ListBox1.ListIndex, 1)
.tod.Value = .ListBox1.List(.ListBox1.ListIndex, 2)
.program.Value = .ListBox1.List(.ListBox1.ListIndex, 3)
.email.Value = .ListBox1.List(.ListBox1.ListIndex, 4)
.officenumber.Value = .ListBox1.List(.ListBox1.ListIndex, 5)
.cellnumber.Value = .ListBox1.List(.ListBox1.ListIndex, 6)
End With
End With
End Sub
Private Sub Search_Click() 'only searches in master tab right now need to search from all worksheets
Dim Name As String
Dim f As range
Dim r As Long
Dim ws As Worksheet
Dim s As Integer
Dim FirstAddress As String
Name = surname.Value
With ws
Set f = range("A:A").Find(what:=Name, LookIn:=xlValues)
If Not f Is Nothing Then
With Me
firstname.Value = f.Offset(0, 1).Value
tod.Value = f.Offset(0, 2).Value
program.Value = f.Offset(0, 3).Value
email.Value = f.Offset(0, 4).Text
officenumber.Value = f.Offset(0, 5).Text
cellnumber.Value = f.Offset(0, 6).Text
End With
findnext
FirstAddress = f.Address
Do
s = s + 1
Set f = range("A:A").findnext(f)
Loop While Not f Is Nothing And f.Address <> FirstAddress
If s > 1 Then
Select Case MsgBox("There are " & s & " instances of " & Name, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
findnext
Case vbCancel
End Select
End If
Else: MsgBox Name & "Not Listed"
End If
End With
End Sub
Sub findnext()
Dim Name As String
Dim f As range
Dim ws As Worksheet
Dim s As Integer
Dim findnext As range
Name = surname.Value
Me.ListBox1.Clear
Set f = range("A:A").Find(what:=Name, LookIn:=xlValues)
Set findnext = f
With ListBox1
Do
Debug.Print findnext.Address
Set findnext = range("A:A").findnext(findnext)
.AddItem findnext.Value
.List(0, 1) = findnext.Offset(0, 1).Value
.List(0, 2) = findnext.Offset(0, 2).Value
.List(0, 3) = findnext.Offset(0, 3).Value
.List(0, 4) = findnext.Offset(0, 4).Value
.List(0, 5) = findnext.Offset(0, 5).Value
.List(0, 6) = findnext.Offset(0, 6).Value
.List(0, 7) = findnext.Offset(0, 6).Value
Loop While findnext.Address <> f.Address
End With
End Sub
You need to you Find then FindNext in a loop. You know you are done looping when your FindNext finds the very first thing you found again. It will cycle like that.
Dim firstFind As Range, subsequentFinds As Range
Set firstFind = Range("D3:D500").Find("search string", , xlValues)
Set subsequentFinds = firstFind
Do
Debug.Print subsequentFinds.Address
Set subsequentFinds = Cells.FindNext(subsequentFinds)
Loop While subsequentFinds.Address <> firstFind.Address

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