Update data in sheet cells from userform textbox - vba

I have this code that transfers data to a sheet and creates a data base of : Name, DOB, Phone number, Insurance name, Insurance ID.
Dim strDataRange As Range
Dim keyRange As Range
Set strDataRange = Range("A1:h5000")
Set keyRange = Range("A1:h5000")
strDataRange.Sort Key1:=keyRange, Header:=xlYes
Dim tr As Worksheet
Set tr = Worksheets("Sheet16")
iRow = tr.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
tr.Cells(iRow, 1).Value = Me.TextBox1.Value
tr.Cells(iRow, 2).Value = Me.TextBox2.Value
tr.Cells(iRow, 3).Value = Me.TextBox22.Value
tr.Cells(iRow, 4).Value = Me.TextBox23.Value
tr.Cells(iRow, 5).Value = Me.TextBox17.Value
tr.Cells(iRow, 6).Value = Me.ComboBox15.Value
tr.Cells(iRow, 7).Value = Me.TextBox21.Value
tr.Cells(iRow, 8).Value = Me.TextBox32.Value
Then on this code, I am able to call all that information enter before (above code) and its populates on textboxes:
Private Sub ComboBox13_Change()
On Error Resume Next
Me.TextBox1.Value = Me.ComboBox13.Column(0)
Me.TextBox2.Value = Me.ComboBox13.Column(1)
Me.TextBox22.Value = Me.ComboBox13.Column(2)
Me.TextBox23.Value = Me.ComboBox13.Column(3)
Me.TextBox17.Value = Me.ComboBox13.Column(4)
Me.ComboBox15.Value = Me.ComboBox13.Column(5)
Me.TextBox21.Value = Me.ComboBox13.Column(6)
Me.TextBox32.Value = Me.ComboBox13.Column(7)
On Error GoTo 0
End Sub
With ComboBox13
.ColumnCount = 1
.ColumnWidths = "120"
.ColumnHeads = False
.RowSource = "Sheet16!A2:h5200"
End With
What I can't do or I need to do is, If the data change on any of these
enter code heretext boxes and I need to update the information such as
: phone 'number or Insurance Id. How can I change it on those text boxes
and press a 'command button to update that new data enter?
Thanks a lot

Related

Dynamic range through name manager on userform listbox

I have a userform with listbox. In the userform the user required to add values in several textboxs, the values automatically are added to an excel table, and the listbox supposed to show the specific values which were added by the user. I have tried to use with Dynamic range through the manager name, and set the rowsource of the listbox to contain the dynamic range, but the listbox is empty and doesn't show any value.
Please your hlp to understand what i'm doing wrong?
The dynamic range is:
Dyn_CurrentCA= =OFFSET(CA_list!$F$4,lists!$V$10,0,lists!$V$9,6)
This is my code:
Public Dep_CA As Integer
Public Target_CA As Integer
Private Sub CB_Add_Click()
Target_CA = Sheets("lists").Range("V8").Value + 1
If T_AuditDate.Value = "" Or CB_Grade.Value = "" Or
T_CAnum.Value = "" Or CB_Subject.Value = "" Or
T_Findings.Value = "" Then
MsgBox "Please fill Audit Date and Audit Result!",
vbRetryCancel + vbCritical, "Data is missing"
Else
Sheets("CA_list").Range("CA_Start").Offset(Target_CA, 0).Value = Target_CA
Sheets("CA_list").Range("CA_Start").Offset(Target_CA, 1).Value = L_Dep.Caption
Sheets("CA_list").Range("CA_Start").Offset(Target_CA, 2).Value = T_AuditDate.Value
Sheets("CA_list").Range("CA_Start").Offset(Target_CA, 3).Value = L_Contact.Caption
Sheets("CA_list").Range("CA_Start").Offset(Target_CA, 4).Value = L_Manager.Caption
Sheets("CA_list").Range("CA_Start").Offset(Target_CA, 5).Value = T_CAnum.Value
Sheets("CA_list").Range("CA_Start").Offset(Target_CA, 6).Value = CB_Subject.Value
Sheets("CA_list").Range("CA_Start").Offset(Target_CA, 7).Value = CB_SubSubject.Value
Sheets("CA_list").Range("CA_Start").Offset(Target_CA, 8).Value = T_Findings.Value
Sheets("CA_list").Range("CA_Start").Offset(Target_CA, 9).Value = T_DD.Value
Sheets("CA_list").Range("CA_Start").Offset(Target_CA, 10).Value = CB_Status.Value
Call clear_CA
Dep_CA = Dep_CA + 1
Sheets("lists").Range("V9").Value = Dep_CA
ListBox1.RowSource = Dyn_CurrentCA
End If
End Sub
Private Sub UserForm_Initialize()
Dep_CA = 0 'initialize no. of lines to 0
Sheets("lists").Range("V9").Value = Dep_CA
CurrentRaw = Sheets("lists").Range("V3").Value
Sheets("lists").Range("V10").Value = Sheets("lists").Range("V8").Value + 1
L_Dep.Caption = Sheets("lists").Range("V5").Value
L_Site.Caption = Sheets("Internal_Plan").Range("A_Start").Offset(CurrentRaw, 4).Value
L_PQ.Caption = Sheets("Internal_Plan").Range("A_Start").Offset(CurrentRaw, 5).Value
L_PYear.Caption = Sheets("Internal_Plan").Range("A_Start").Offset(CurrentRaw, 6).Value
L_Auditor.Caption = Sheets("Internal_Plan").Range("A_Start").Offset(CurrentRaw, 7).Value
L_Contact.Caption = Sheets("Internal_Plan").Range("A_Start").Offset(CurrentRaw, 2).Value
L_Manager.Caption = Sheets("Internal_Plan").Range("A_Start").Offset(CurrentRaw, 3).Value
Call clear_CA
With ListBox1
.ColumnWidths = "40;60;60;260;50;40"
.ColumnCount = 6
.RowSource = Dyn_CurrentCA
.ColumnHeads = True
End With
End Sub
Sub clear_CA()
With Update_Results 'name of userform
CB_Subject.Value = ""
CB_SubSubject.Value = ""
T_CAnum.Value = ""
T_DD.Value = ""
CB_Status.Value = "Open"
T_Findings.Value = ""
End With
End Sub
This is the userform with listbox:
Try with using the address of the dynamic range. You need to add the sheet name as well.
ListBox1.RowSource = Worksheets("CA_list").Range("Dyn_CurrentCA").Address(external:=True)
For ActiveX
ListBox1.ListFillRange = Worksheets("CA_list").Range("Dyn_CurrentCA").Address(external:=True)

VBA UserForm - Run Time Error 1004 'Application Defined or Object Defined Error'

I get
Run Time Error 1004 'Application Defined or Object Defined Error'
on the first Cells Value line ( .Cells(last, 1).Value = Val(TextBox1) ) when I want to run my Userform.
The Userform is triggered with a button located on another sheet and the data typed in the userform shall be delivered to the first empty row on my worksheet "MBE_Statistik". Because my data on this sheet is formatted as a table I first want to activate autofill and expand for tables (as the archive will be opened on different computers with probably different settings).
Private Sub CommandButton1_Click()
Dim last As Long
With Application.AutoCorrect
.AutoExpandListRange = True
.AutoFillFormulasInLists = True
End With
last = ThisWorkbook.Worksheets("MBE_Statistik").Cells(Rows.Count, 1).End(xlUp).Row + 1
With ThisWorkbook.Worksheets("MBE_Statistik")
'KW
.Cells(last, 1).Value = Val(TextBox1)
'Center Code
.Cells(last, 3).Value = Val(TextBox2)
'Ort
.Cells(last, 4).Value = Val(TextBox3)
'Projekt
If OptionButton1.Value = True Then .Cells(last, 5).Value = "Neustarter"
If OptionButton2.Value = True Then .Cells(last, 5).Value = "ISB"
If OptionButton3.Value = True Then .Cells(last, 5).Value = "Leadgenerierung"
If OptionButton4.Value = True Then .Cells(last, 5).Value = "Center Flights"
'Flight-Nr
.Cells(last, 6).Value = Val(TextBox5)
'Ansprechpartner
.Cells(last, 7).Value = Val(TextBox4)
'Agent
.Cells(last, 10).Value = Val(TextBox6)
'Ist-Stunden
.Cells(last, 8).Value = Val(TextBox13)
'Ist-Stunden
.Cells(last, 9).Value = Val(TextBox7)
'Wählversuche
.Cells(last, 11).Value = Val(TextBox8)
'Erreicht AP/Entscheider
.Cells(last, 12).Value = Val(TextBox9)
'Kein Interesse
.Cells(last, 13).Value = Val(TextBox16)
.
.
.
End With
End Sub
There are some more textboxes which I left out at the moment as the error is highlighted on the first.
Can somebody help me find the problem?

Add data in specific rows in vba

After I click the command button, I want my excel to do:
Input what I type in text boxes / select in combo boxes in specific columns without deleting the one I previously entered
But at this moment, it does not work as I expected or enter any of input from text boxes and combo boxes.
The script I wrote is:
Private Sub
If TextBox1.Value = "" Or TextBox2.Value = "" Or TextBox3.Value = "" Then
If MsgBox ("There might one or more empty cells,
do you want to continue to proceed?", vbQuestion + vbYesNo) <> vbYes Then
Exit Sub
End If
End If
Dim invsheet As Worksheet
Dim pacsheet As Worksheet
Set invsheet = ThisWorkbook.Sheets("INV")
Set pacsheet = ThisWorkbook.Sheets("PAC")
invsheet.Range("A1").Value = TextBox6.Text
invsheet.Range("I5").Value = TextBox7.Text
invsheet.Range("A21").Value = TextBox5.Text
invsheet.Range("A25").Value = ComboBox1.Value
inv_nr = invsheet.Cells(Row.Count, 1).End(xlUp).Row +1
invsheet.Cells(inv_nr, 5).Value = Me.TextBox1
invsheet.Cells(inv_nr, 4).Value = Me.ComboBox2
pac_nr = pacsheet.Cells(Row.Count, 1).End(xlUp).Row +1
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox2
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox3
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox4
Problem:
inv_nr = invsheet.Cells(Row.Count, 1).End(xlUp).Row +1
invsheet.Cells(inv_nr, 5).Value = Me.TextBox1
invsheet.Cells(inv_nr, 4).Value = Me.ComboBox2
pac_nr = pacsheet.Cells(Row.Count, 1).End(xlUp).Row +1
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox2
pacsheet.Cells(pac_nr, 7).Value = Me.TextBox3 'mistyped it. supposed to be 7
pacsheet.Cells(pac_nr, 9).Value = Me.TextBox4 'mistyped it. supposed to be 9
This block of code does not work and create any output on the worksheet.
I will really appreciate your help.
Thank you!
You're not placing anything in column A (except A1, A21, and A25 of invsheet), so it's not a good idea to set your inv_nr and pac_nr variables based on the last used cell in column A.
Try basing it on one of the columns you are populating with data, e.g. column 5:
'Always qualify "Rows" (and don't mistype it as "Row")
inv_nr = invsheet.Cells(invsheet.Rows.Count, 5).End(xlUp).Row + 1
invsheet.Cells(inv_nr, 5).Value = Me.TextBox1
invsheet.Cells(inv_nr, 4).Value = Me.ComboBox2
'Always qualify "Rows" (and don't mistype it as "Row")
pac_nr = pacsheet.Cells(pacsheet.Rows.Count, 5).End(xlUp).Row + 1
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox2 'Note: This is pointless because the next line overwrites it
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox3 'Note: This is pointless because the next line overwrites it
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox4

How to check for different worksheet names in excel and add new in case it doesn't exist

I'm exporting my data from MS Project to MS Excel (single pre-defined file with a given name all the time, for e.g. XYZ.xlsx) and want to have different worksheet in the excel for every workstream in the project. And number of workstreams can increase in future, thus I've to keep it dynamic.
As of now my code does the export, but I also want it to check if the workstream already exists,
- if yes, delete all the data in that worksheet and paste the new data in XYZ file.
- if no, create a new worksheet in the XYZ file and paste the data into it.
Can anyone please help as I'm on a deadline to finish it.
Code that I'm using it,
Set tsks = ThisProject.Tasks
For Each t In tsks
If Not t Is Nothing Then
If t.OutlineLevel > 1 Then
If t.OutlineLevel = 2 Then
If ExcelRowCounter > 2 Then
'Finish formatting the sheet we just finished
For i = 1 To 7
xlSheet.Columns(i).AutoFit
Next i
End If
'Add Excel sheet, name it and define column headers
AppActivate ExcelAppTitle
Set xlSheet = xlBook.Worksheets.Add
ExcelSheetName = Left(Replace(t.Name, "&", "and"), 30)
xlSheet.Name = ExcelSheetName
xlSheet.Cells(1, 1).Value = "Task Name"
xlSheet.Cells(1, 2).Value = "Duration (days)"
xlSheet.Cells(1, 3).Value = "Start Date"
xlSheet.Cells(1, 4).Value = "Finish Date"
xlSheet.Cells(1, 5).Value = "Workstream Group"
xlSheet.Cells(1, 6).Value = "% Complete"
xlSheet.Cells(1, 7).Value = "Status"
xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 7)).Font.Bold = True
ExcelRowCounter = 2
End If
xlSheet.Cells(ExcelRowCounter, 1).Value = t.Name
xlSheet.Cells(ExcelRowCounter, 2).Value = t.Duration / (8 * 60)
xlSheet.Cells(ExcelRowCounter, 3).Value = Format(t.Start, "mm/dd/yyyy")
xlSheet.Cells(ExcelRowCounter, 4).Value = Format(t.Finish, "mm/dd/yyyy")
xlSheet.Cells(ExcelRowCounter, 5).Value = t.Text1
xlSheet.Cells(ExcelRowCounter, 6).Value = t.PercentComplete
xlSheet.Cells(ExcelRowCounter, 7).Value = t.Number1
xlSheet.Cells(ExcelRowCounter, 1).IndentLevel = 2 * (t.OutlineLevel - 2)
If t.Summary = "True" Then
xlSheet.Range(xlSheet.Cells(ExcelRowCounter, 1), xlSheet.Cells(ExcelRowCounter, 6)).Font.Bold = True
End If
ExcelRowCounter = ExcelRowCounter + 1
End If
End If
Next t
For i = 1 To 7
xlSheet.Columns(i).AutoFit
Next i
Here's as simple method:
Function AddOrGetWorksheet(withName As String) As Worksheet
Dim found As Boolean
found = False
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
If (LCase(ws.Name) = LCase(withName)) Then
found = True
Set AddOrGetWorksheet = ws
Exit For
End If
Next
If (Not found) Then
Set AddOrGetWorksheet = ActiveWorkbook.Sheets.Add()
AddOrGetWorksheet.Name = withName
End If
End Function

how to insert textbox text to existing excel file? vb.net

i want to make a program that saves the text in textbox to excel file using loop because i want to insert multiple text to excel. i found codes but it only overwrites data in cells. i want the program to find the last row and insert new data to the next row. im stuck here, please someone help me how to do that in vb.net. here is my code:
Excel = CreateObject("Excel.Application")
Excel.screenupdating = True
Excel.Visible = True
'fieldnames
Dim xlWorkSheet As Object = Excel.workbooks.add
Excel.workbooks(1).worksheets(1).cells(1, 1).value = "TITLE"
Excel.workbooks(1).worksheets(1).cells(1, 2).value = "AUTHOR"
Excel.workbooks(1).worksheets(1).cells(1, 3).value = "EDITION"
Excel.workbooks(1).worksheets(1).cells(1, 4).value = "PUBLISHER"
Excel.workbooks(1).worksheets(1).cells(1, 5).value = "ISBN"
'i want to loop here the data in textboxes
Excel.workbooks(1).worksheets(1).cells(2, 1).value = txtTitle.Text
Excel.workbooks(1).worksheets(1).cells(2, 2).value = txtAuthor.Text
Excel.workbooks(1).worksheets(1).cells(2, 3).value = txtEdition.Text
Excel.workbooks(1).worksheets(1).cells(2, 4).value = txtPublisher.Text
Excel.workbooks(1).worksheets(1).cells(2, 5).value = txtISBN.Text
xlWorkSheet.SaveAs(FileName)
Excel.quit()
Excel = Nothing
you're going to need to dynamically set the value for the rowindex and the aplha column.
so something like this
dim currRow as integer = 0
Excel = CreateObject("Excel.Application")
Excel.screenupdating = True
Excel.Visible = True
'fieldnames
Dim xlWorkSheet As Object = Excel.workbooks.add
Excel.workbooks(1).worksheets(1).cells((currRow+1), 1).value = "TITLE"
Excel.workbooks(1).worksheets(1).cells((currRow+1), 2).value = "AUTHOR"
Excel.workbooks(1).worksheets(1).cells((currRow+1), 3).value = "EDITION"
Excel.workbooks(1).worksheets(1).cells((currRow+1), 4).value = "PUBLISHER"
Excel.workbooks(1).worksheets(1).cells((currRow+1), 5).value = "ISBN"
currRow += 1
for i as integer = currRow to 5
'i want to loop here the data in textboxes
Excel.workbooks(1).worksheets(1).cells((currRow + i), 1).value = txtTitle.Text
Excel.workbooks(1).worksheets(1).cells((currRow + i), 2).value = txtAuthor.Text
Excel.workbooks(1).worksheets(1).cells((currRow + i), 3).value = txtEdition.Text
Excel.workbooks(1).worksheets(1).cells((currRow + i), 4).value = txtPublisher.Text
Excel.workbooks(1).worksheets(1).cells((currRow + i), 5).value = txtISBN.Text
next
currRow += 1
xlWorkSheet.SaveAs(FileName)
Excel.quit()
Excel = Nothing
i don't know if this works but this is something to get you started.