使用VBS将值放入单元格中

时间:2016-02-26 04:09:29

标签: excel vbscript

任何人都知道如何使用 VBS 在单元格中放置一个值?到目前为止我有这个代码:

option explicit
Dim objWorkbook, wb, ws,logname,user, objExcel,cell

Const xlUp = -4162

Set objWorkbook = CreateObject("Excel.Application")
set user = CreateObject("wscript.network")
logname = user.Username

objWorkbook.Visible = True

Set wb = objWorkbook.Workbooks.Open("C:\test\test.xlsx")
Set ws = wb.Worksheets(1)

With ws
  .Range("A" & .Rows.Count).End(xlUp).Row 'put the value of logname
  .Range("A" & .Rows.Count).End(xlUp).Row 'put the value of date()
  .Range("A" & .Rows.Count).End(xlUp).Row 'put the value of time()
End With

执行代码后,它应该递增,以便下次插入数据时,插入的数据将插入到下一行而没有数据。提前谢谢!

1 个答案:

答案 0 :(得分:1)

尝试以下内容。

option explicit
Dim objWorkbook, wb, ws,logname,user, objExcel,cell

Const xlUp = -4162

Set objWorkbook = CreateObject("Excel.Application")
set user = CreateObject("wscript.network")
logname = user.Username

objWorkbook.Visible = True

Set wb = objWorkbook.Workbooks.Open("C:\test\test.xlsx")
Set ws = wb.Worksheets(1)

With ws
  Set cell = .Range("A" & .Rows.Count).End(xlUp) 'Get last cell in A
  cell.Offset(1,0).Value = logname 'put the value of logname in column A after last row of data
  cell.Offset(1,1).Value = Date 'put the value of date() in column B
  cell.Offset(1,2).Value = Time 'put the value of time() in column C
End With