简体   繁体   English

用texbox填充Excel

[英]populating excel with texboxes

I am trying to create a small userform call log. 我正在尝试创建一个小的userform呼叫日志。 The user takes details and on button click, the specified excel sheet will be populated with the text boxes. 用户获取详细信息并单击按钮,将在文本框中填充指定的excel工作表。 I'm relatively new to VBA and cannot seem to populate columns on the current row (which should be empty) with the specified text boxes (eg column A = tb1.text, column b = tb2.text) I have the "find empty row" working properly. 我是VBA的新手,似乎无法用指定的文本框(例如A列= tb1.text,b列= tb2.text)填充当前行(应为空)中的列。行”正常工作。 I have deleted the failed attempts at properly populating columns but need to keep the same format as this is being written in Visual Studio. 我已经删除了无法正确填充列的尝试,但是需要保持与Visual Studio中编写的格式相同的格式。

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles btn_submit.Click
    On Error Resume Next
    Dim objExcel As New Microsoft.Office.Interop.Excel.Application
    Dim objWorkbook As Microsoft.Office.Interop.Excel.Workbook
    Dim objWorksheet As Microsoft.Office.Interop.Excel.Worksheet
    objWorkbook = objExcel.Workbooks.Open("C:\_Private Data - NO BACKUP\Test OM Sheet.xlsx")
    objWorksheet = CType(objWorkbook.Worksheets.Item("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet)

    Dim lastrow As Integer
    lastrow = objWorksheet.Cells(objWorksheet.Rows.Count, 1).End(Excel.XlDirection.xlUp).Row + 1
    Dim emptyrow As Integer = lastrow + 0,1
    objWorksheet.Cells(emptyrow, 0) = tb_date_time.Text
    objWorksheet.Cells(lastrow, 1) = tb_user_name.Text

    objWorksheet.Cells(emptyrow, 1) = tb_date_time.Text
    objWorksheet.Columns.AutoFit()
    objWorkbook.Save()
    objWorkbook.Application.DisplayAlerts = False
    objWorkbook.SaveAs("C:\_Private Data - NO BACKUP\Test OM Sheet Backup.xlsx")
    objWorkbook.Application.DisplayAlerts = True
    objExcel.Quit()


    Call reset_form()

End Sub

I have the "find empty row" working properly 我的“查找空行”正常工作

Actually, I think that is where your issue is. 实际上,我认为这就是您的问题所在。 This code: 这段代码:

Dim lastrow As Integer = objWorksheet.Cells.Rows.Count
Dim emptyrow As Integer = lastrow + 1

Will return 1 + the last possible row in the spreadsheet, which will through an error in 将返回1 +电子表格中最后一个可能的行,这将通过

objWorksheet.Cells(emptyrow, 1) = tb_date_time.Text

because it will look for a cell row number that does not exist. 因为它将查找不存在的单元格行号。

Try this instead. 试试这个吧。

Dim lastrow As Integer
lastrow = objWorksheet.Cells(objWorksheet.Rows.Count,1).End(xlup).Row + 1
Dim emptyrow As Integer = lastrow + 1
objWorksheet.Cells(emptyrow, 1) = tb_date_time.Text

I am not well versed in VSTO, so my syntax may be a bit off, but conceptually that should work. 我不太精通VSTO,因此我的语法可能有点不正确,但从概念上讲应该可以。

Well, it's really messy but this is what I ended up with. 好吧,这确实很乱,但这就是我最终得到的结果。 I'm sure there's a better way to do it but I have moved on to the next part of the code. 我敢肯定有更好的方法可以做到这一点,但是我已经进入了代码的下一部分。

    objWorksheet.Cells(lastrow, 1) = tb_date_time.Text
    objWorksheet.Cells(lastrow, 2) = tb_user_name.Text
    If rb_bs.Checked = True Then
        objWorksheet.Cells(lastrow, 3) = rb_bs.Text
    ElseIf rb_wm.Checked = True Then
        objWorksheet.Cells(lastrow, 3) = rb_wm.Text
    ElseIf rb_gw.Checked = True Then
        objWorksheet.Cells(lastrow, 3) = rb_gw.Text
    End If
    objWorksheet.Cells(lastrow, 4) = tb_caller_name.Text
    objWorksheet.Cells(lastrow, 5) = cb_company.Text
    objWorksheet.Cells(lastrow, 6) = cb_transfer.Text
    objWorksheet.Cells(lastrow, 7) = tb_call_notes.Text
    objWorksheet.Cells(lastrow, 8) = tb_solution.Text
    If chkb_resolved.Checked = True Then
        objWorksheet.Cells(lastrow, 9) = chkb_resolved.Text
    Else
        objWorksheet.Cells(lastrow, 9) = Text("")
    End If
    objWorksheet.Cells(lastrow, 10) = DateTime.Now.ToString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM