Excelmacros将数据插入下一行
我似乎无法弄清楚如何将信息抵消下一行。
我想要做的是每次执行这个macros时在下一行插入相同的信息。 我使用它作为学习pipe理系统的便宜,以跟踪电子学习课程的完成,所以每当用户执行macros时,它将列出date,课程和用户名。
.Cells(1, 1)
中的信息不正确,但我只是用它来确保macros的其余部分正在工作。 在这一点上,我只需要弄清楚如何在macros执行的时候在逻辑中build立一个向下的行。
在此先感谢您的帮助!
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
这应该为你解决。 在objWorkbook.Worksheets(1).Activate
之后添加这个权限
Dim lastrow as Long lastrow = objExcel.Worksheets(1).Range("A" & objExcel.Worksheets(1).Rows.Count).End(xlup).Row + 1
然后改变接下来的三行:
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
更新
由于它看起来像是在Excel本身内部运行这个代码,所以我将向您展示如何真正清理这些代码,并让它运行得更快,更容易解密。 请参阅下面的代码:
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
谢谢Scott Holtzman
我也有类似的问题,虽然我不得不改变一些设置,但几天后,你来我的救援。 非常感谢您的帮助。
这是我实施的代码,你的回复帮助了我。
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