Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 从特定单元格添加excel用户表单数据_Vba_Excel - Fatal编程技术网

Vba 从特定单元格添加excel用户表单数据

Vba 从特定单元格添加excel用户表单数据,vba,excel,Vba,Excel,我是VBA新手,正在excel 2010 VBA中创建客户数据库。我在excel表格中输入了一些从A1到C50的值,但是从D4到G50是空的。当我尝试使用UserForm输入数据时,它会将数据添加到D51、E51、F51和G51,但我希望数据从D4、E4、F4和G4输入,然后再依此类推到下一行。请帮忙 代码如下: Private Sub Cmdcreate_Click() Dim iRow As Long Dim ws As Worksheet Set ws = Worksheets("M

我是VBA新手,正在excel 2010 VBA中创建客户数据库。我在excel表格中输入了一些从A1到C50的值,但是从D4到G50是空的。当我尝试使用UserForm输入数据时,它会将数据添加到D51、E51、F51和G51,但我希望数据从D4、E4、F4和G4输入,然后再依此类推到下一行。请帮忙

代码如下:

Private Sub Cmdcreate_Click()

Dim iRow As Long

Dim ws As Worksheet

Set ws = Worksheets("My.Sheet")

'''find  first empty row in database

''iRow = ws.Cells(Rows.Count, 1)

''  .End(xlUp).Offset(1, 0).Row

'revised code to avoid problems with Excel tables in newer versions

iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _

    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1


'check for a Name

If Trim(Me.TextBox1.Value) = "" Then

  Exit Sub

End If

'copy the data to the database

ws.Cells(iRow, 5).Value = ComboBox1.Value

ws.Cells(iRow, 6).Value = TextBox1.Value

ws.Cells(iRow, 7).Value = TextBox2.Value

'clear the data

TextBox1.Value = ""

TextBox2.Value = ""

ComboBox1.Value = ""

If ComboBox1.Value = True Then Cells(iRow, 5).Value = ComboBox1

End Sub

我认为,使用下面的代码,您可以在整个工作表中搜索输入到单元格中的任何值,而find函数会在A50单元格中查找该值,并将该行存储为50。这就是为什么你的其他代码从51开始

iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _

SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
如果您想继续使用find函数,您可以将“cells”更改为只包含D列的范围。否则,您可以使用下面的代码来标识最后一行

iRow = ws.range("D1").End(xlDown).Row

谢谢你的建议。然而,我试图使用你提供的代码,但似乎不工作,你能帮我发送整个代码。请在研究了许多论坛之后,我发现了代码,但是代码有一些问题,因为它没有从顶部的列和行添加数据,数据是从第4行添加的。这是excel工作表的链接。[URL=“Please help.hi,您能将范围(“D1”)更改为范围(“D:D”)并重试吗?我现在没有excel,因此无法尝试。