如何多次复制两个特定的行和列?

时间:2019-01-28 06:28:40

标签: excel vba

我有一个用户窗体,其中包含2个文本框和1个组合框。

  1. TxtboxDate;
  2. ComboBoxName;
  3. TxtBoxNumber;和
  4. AddCommandButton

当我在用户表单中输入以下内容时,我的excel表如下所示:

(行)(列)        A B C D E 1日期单编号名称车牌编号状态 2 19年1月28日(已给定)汤姆·塔克斯(已有公式)(已有公式) 3
4 5 6 7 8 9 10

我只想要的是,当我在用户窗体的textboxNumber中输入数字/值(例如“ 3”),然后点击AddCommandButton时,它将复制我在TxtboxDate和ComboBoxName中输入的值并将其粘贴到3次。

我该怎么做?

我在下面粘贴了我的代码,请查看附件图片以获取更多信息。请帮忙吗????

Private Sub cmdbutton12_Click()
  Dim iRow As Long
  Dim ws As Worksheet
  Set ws = Worksheets("Fuels_Slips_Issued")
  Dim rngNullString
  'find first empty row in database

   Set rngNullString = Intersect(ws.Columns("A"), ws.Columns("A")).Find("")
     If rngNullString.Row < ws.Cells(ws.Rows.Count, "A").End(xlUp).Row Then
        Set rngNullString = Intersect(ws.Columns("A"), ws.Columns("A")).SpecialCells(xlCellTypeBlanks)
    End If
   iRow = rngNullString.Row

  ' iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
  ' SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
  'check for Name number
   If Trim(Me.tb1.Value) = "" Then
   Me.tb1.SetFocus
   MsgBox "Please complete the FORM"
   Exit Sub
 End If

 'copy the data to the database
  ws.Cells(iRow, 1).Value = Me.tb1.Value
  ws.Cells(iRow, 3).Value = Me.cmb_issued.Value


  MsgBox "Successfully! Data added", vbOKOnly + vbInformation, "Data Added"

  'clear the data
   Me.tb1.Value = ""
   Me.cmb_issued = ""
   Me.tb1.SetFocus

   Worksheets("Fuels_Slips_Issued").Activate
 End Sub

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

您需要重复将数据复制到表中的过程,并将其包含在要更改的行中。修改代码如下:

'copy the data to the database
Dim c As Long 'starts as zero

Do
    ws.Cells(iRow + c, 1).Value = Me.tb1.Value
    ws.Cells(iRow + c, 3).Value = Me.cmb_issued.Value
    c = c + 1

Loop Until c = Me.TxtBoxNumber

您可能还需要确保用户在命令单击时输入一个整数。希望能奏效。