how to insert textbox text to existing excel file? vb.net - 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.

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 Loop Not Advancing Despite Appearing It Should Be

My program appears in the VBA screen to be working, but on the Excel doc it's manipulating, not so much.
It is supposed to loop down a root column, populating off of it to other sheets, until the root column comes to a blank cell. When I run the loop, it does loop, and the plus 1 shows that it took effect for RW variable, when I hover over it in the VBA stepo through process. Yet it just keeps entering values from the same row over and over again as if RW isn't advancing when I can see that it is, and it keeps copying to the same row, when again, I can see the RW variable advancing by 1 in the VBA editor.
Here's the code.
Sub ExportData()
Dim RW As Integer
RW = 2
Dim CL As Integer
CL = 3
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "US"
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "CA"
End With
Dim SKUUS As Range
Set SKUUS = Sheets("US").Cells(RW - 1, CL - 2)
Dim SKUCA As Range
Set SKUCA = Sheets("CA").Cells(RW - 1, CL - 2)
Dim RootSKU As Variant
Set RootSKU = Sheets("tblTEMP").Cells(RW, CL)
AvailDate = InputBox("What is Available Date?")
PromoCode = InputBox("What is the Promo Code?")
'HeadersUS
SKUUS.Value = "Item #"
SKUUS.Offset(0, 1).Value = "MAX QTY"
SKUUS.Offset(0, 2).Value = "PROMO Price"
SKUUS.Offset(0, 3).Value = "AvailableDate"
SKUUS.Offset(0, 4).Value = "VendorNumOverride"
SKUUS.Offset(0, 5).Value = "PromoListPrice"
SKUUS.Offset(0, 6).Value = "BOGO ITEM #"
SKUUS.Offset(0, 7).Value = "BOGO QTY"
SKUUS.Offset(0, 8).Value = "ProgramCode"
SKUUS.Offset(0, 9).Value = "PromoCode"
'HeadersCA
SKUCA.Value = "Item #"
SKUCA.Offset(0, 1).Value = "MAX QTY"
SKUCA.Offset(0, 2).Value = "PROMO Price"
SKUCA.Offset(0, 3).Value = "AvailableDate"
SKUCA.Offset(0, 4).Value = "VendorNumOverride"
SKUCA.Offset(0, 5).Value = "PromoListPrice"
SKUCA.Offset(0, 6).Value = "BOGO ITEM #"
SKUCA.Offset(0, 7).Value = "BOGO QTY"
SKUCA.Offset(0, 8).Value = "ProgramCode"
SKUCA.Offset(0, 9).Value = "PromoCode"
Do
'Populate First Row in US
SKUUS.Offset(1, 0) = RootSKU.Value
SKUUS.Offset(1, 1) = RootSKU.Offset(0, 12).Value * 0.8
SKUUS.Offset(1, 1) = Math.Round(SKUUS.Offset(1, 1).Value, 0)
SKUUS.Offset(1, 2) = RootSKU.Offset(0, 7).Value
SKUUS.Offset(1, 3) = AvailDate
SKUUS.Offset(1, 5) = RootSKU.Offset(0, 6).Value
SKUUS.Offset(1, 9) = PromoCode
'Populate First Row in US
SKUCA.Offset(1, 0) = RootSKU.Value
SKUCA.Offset(1, 1) = RootSKU.Offset(0, 12).Value * 0.2
SKUCA.Offset(1, 1) = Math.Round(SKUCA.Offset(1, 1).Value, 0)
SKUCA.Offset(1, 2) = RootSKU.Offset(0, 10).Value
SKUCA.Offset(1, 3) = AvailDate
SKUCA.Offset(1, 5) = RootSKU.Offset(0, 9).Value
SKUCA.Offset(1, 9) = PromoCode
RW = RW + 1
Loop Until RootSKU = ""
End Sub
I moved the variable to inside the loop, but still get the same result. After playing around with it a bit on the premise, I was able to get it to work like this. Thanks for the assist.
Sub ExportData()
Dim RW As Long
Dim CL As Long
Dim SKUUS As Range
Dim SKUCA As Range
Dim RootSKU As Variant
RW = 2
CL = 1
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "US"
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "CA"
End With
AvailDate = InputBox("What is Available Date?")
PromoCode = InputBox("What is the Promo Code?")
'HeadersUS
Sheets("US").Range("A1") = "Item #"
Sheets("US").Range("B1") = "MAX QTY"
Sheets("US").Range("C1") = "PROMO Price"
Sheets("US").Range("D1") = "AvailableDate"
Sheets("US").Range("E1") = "VendorNumOverride"
Sheets("US").Range("F1") = "PromoListPrice"
Sheets("US").Range("G1") = "BOGO ITEM #"
Sheets("US").Range("H1") = "BOGO QTY"
Sheets("US").Range("I1") = "ProgramCode"
Sheets("US").Range("J1") = "PromoCode"
'HeadersCA
Sheets("CA").Range("A1") = "Item #"
Sheets("CA").Range("B1") = "MAX QTY"
Sheets("CA").Range("C1") = "PROMO Price"
Sheets("CA").Range("D1") = "AvailableDate"
Sheets("CA").Range("E1") = "VendorNumOverride"
Sheets("CA").Range("F1") = "PromoListPrice"
Sheets("CA").Range("G1") = "BOGO ITEM #"
Sheets("CA").Range("H1") = "BOGO QTY"
Sheets("CA").Range("I1") = "ProgramCode"
Sheets("CA").Range("J1") = "PromoCode"
Do
Set SKUUS = Sheets("US").Cells(RW, CL)
Set SKUCA = Sheets("CA").Cells(RW, CL)
Set RootSKU = Sheets("tblTEMP").Cells(RW, CL + 2)
'Populate First Row in US
SKUUS = RootSKU.Value
SKUUS.Offset(0, 1) = RootSKU.Offset(0, 12).Value * 0.8
SKUUS.Offset(0, 1) = Math.Round(SKUUS.Offset(0, 1).Value, 0)
SKUUS.Offset(0, 2) = RootSKU.Offset(0, 7).Value
SKUUS.Offset(0, 3) = AvailDate
SKUUS.Offset(0, 5) = RootSKU.Offset(0, 6).Value
SKUUS.Offset(0, 9) = PromoCode
'Populate First Row in US
SKUCA = RootSKU.Value
SKUCA.Offset(0, 1) = RootSKU.Offset(0, 12).Value * 0.2
SKUCA.Offset(0, 1) = Math.Round(SKUCA.Offset(0, 1).Value, 0)
SKUCA.Offset(0, 2) = RootSKU.Offset(0, 10).Value
SKUCA.Offset(0, 3) = AvailDate
SKUCA.Offset(0, 5) = RootSKU.Offset(0, 9).Value
SKUCA.Offset(0, 9) = PromoCode
RW = RW + 1
Loop Until RootSKU.Offset(1, 0) = ""
End Sub

Update data in sheet cells from userform textbox

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

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