0

我对 VBA 编码相当陌生,我想知道是否可以执行以下操作?

我有一个可重用的输入工作表,其中包含一个包含 24 列和 10 行的表等元素。我希望能够将许多行添加到另一个工作表上的数据库中。我设法找到了一些允许单行更新但多行没有成功的代码。

任何建议将不胜感激。

谢谢

伊恩

代码如下所示:

子 UpdateLogWorksheet()

Dim historyWks As Worksheet
Dim inputWks As Worksheet

Dim nextRow As Long
Dim oCol As Long

Dim myCopy As Range
Dim myTest As Range

Dim lRsp As Long

Set inputWks = Worksheets("Input")
Set historyWks = Worksheets("PartsData")
oCol = 3 'order info is pasted on data sheet, starting in this column

'check for duplicate order ID in database
If inputWks.Range("CheckID") = True Then
  lRsp = MsgBox("Order ID already in database. Update record?", vbQuestion + vbYesNo, "Duplicate ID")
  If lRsp = vbYes Then
    UpdateLogRecord
  Else
    MsgBox "Please change Order ID to a unique number."
  End If

Else

  'cells to copy from Input sheet - some contain formulas
  Set myCopy = inputWks.Range("OrderEntry")

  With historyWks
      nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
  End With

  With inputWks
      'mandatory fields are tested in hidden column
      Set myTest = myCopy.Offset(0, 2)

      If Application.Count(myTest) > 0 Then
          MsgBox "Please fill in all the cells!"
          Exit Sub
      End If
  End With

  With historyWks
      'enter date and time stamp in record
      With .Cells(nextRow, "A")
          .Value = Now
          .NumberFormat = "mm/dd/yyyy hh:mm:ss"
      End With
      'enter user name in column B
      .Cells(nextRow, "B").Value = Application.UserName
      'copy the order data and paste onto data sheet
      myCopy.Copy
      .Cells(nextRow, oCol).PasteSpecial Paste:=xlPasteValues, Transpose:=True
      Application.CutCopyMode = False
  End With

  'clear input cells that contain constants
  ClearDataEntry

万一

结束子

并附上输入表在此处输入图像描述

4

1 回答 1

0

响应评论的示例代码:

Dim lr as long, lc as long, lr2 as long
with ThisWorkbook.Sheets("Source")
    lr = .cells(.rows.count,1).end(xlup).row
    lc = .cells(1,.columns.count).end(xltoleft).column
    .range(.cells(2,1),.cells(lr,lc)).copy 
end with
with Workbooks("Database").Sheets("Dest")
    lr2 = .cells(.rows.count,1).end(xlup).row
    .cells(lr2+1,1).pastespecial xlvalues
end with
于 2018-07-24T13:58:52.863 回答