2014-03-19 19 views
0

我有这个代码,但我想直接在excel中的“名称”下添加名称等,但是至今它只添加它在第1行。有人可以帮我吗?如何在Excel VBA中的某个字段下直接添加值?

例如,当我输入名称框时,无论“我的名字”在我的Excal表单中的哪个位置,我都希望该值直接在Excel中的“Name”下。

我是新来的,这是我的第一个问题:)

'find first empty row in database 
iRow = ws.Cells.Find(What:="Name", SearchOrder:=xlRows, _ 
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1 
'check for a part number 
If Trim(Me.TxtName.Value) = "" Then 
    Me.TxtName.SetFocus 
    MsgBox "Please enter a part number" 
    Exit Sub 
End If 

'copy the data to the database 
'use protect and unprotect lines, 
'  with your password 
'  if worksheet is protected 
With ws 
' .Unprotect Password:="password" 
    .Cells(iRow, 1).Value = Me.TxtName.Value 
    .Cells(iRow, 2).Value = Me.TxtLocation.Value 
    .Cells(iRow, 3).Value = Me.TxtDate.Value 
    .Cells(iRow, 4).Value = Me.TxtQuantity.Value 
' .Protect Password:="password" 
End With 
  • 感谢。
+1

是否使用'上的错误恢复Next'?如果iRow = 1,那么你发现的单元必须是0行,这显然不是这种情况,所以也许它没有找到“名称”。 –

+0

@TimWilliams看着他的代码,irow故意不会是1,所以这不是问题(值总是会在单元格中带有“Name”的单元格,因此将数据放入的最小行是行2)。 –

+0

@cor如果他正在使用'On Error Resume Next',那么'.find'的结果如果从未找到“name”,则结果为0,因此iRow将为1.从msdn:“如果没有,则此方法返回Nothing匹配被发现。“所以使用错误恢复接下来将irow留在1.我会说他必须在那里'错误',否则irow永远不能是1,因为没有'错误'的'.find'会抛出一个运行时错误if没有找到名称 – user1759942

回答

0

请尝试使用下面的代码来代替您发布的代码部分。

Dim anchorCell As Range 

'find first empty row in database 
If ws.Cells(1,1).Value = "Name" Then 
    Set anchorCell = ws.Cells(1,1) 
Else 
    Set anchorCell = ws.Cells.Find(What:="Name", SearchOrder:=xlRows, _ 
     SearchDirection:=xlNext, LookIn:=xlValues) 
End If 

If Not anchorCell Is Nothing Then 
    'check for a part number 
    If Trim(Me.TxtName.Value) = "" Then 
     Me.TxtName.SetFocus 
     MsgBox "Please enter a part number" 
     Exit Sub 
    End If 
    'copy the data to the database 
    'use protect and unprotect lines, 
    '  with your password 
    '  if worksheet is protected 
    With anchorCell 
    ' .Unprotect Password:="password" 
     .Offset(1, 0).Value = Me.TxtName.Value 
     .Offset(1, 1).Value = Me.TxtLocation.Value 
     .Offset(1, 2).Value = Me.TxtDate.Value 
     .Offset(1, 3).Value = Me.TxtQuantity.Value 
    ' .Protect Password:="password" 
    End With 
End If 
+0

谢谢,这就是我想要的:) – user3438601

+0

@ user3438601不用客气,但看看Tim William的答案,以防您正在寻找的是将新数据添加到名称单元格后的数据列表末尾。另外请注意,我无意中使用了.Offset(1,1)等而不是.Offset(1,0)等。现在已更正。 –

1

假设OP真的要填充第一个空行:

Dim pNum, rngName As Range 

pNum = Trim(Me.TxtName.Value) 'check for a part number 

If Len(pNum) = 0 Then 
    Me.TxtName.SetFocus 
    MsgBox "Please enter a part number" 
    Exit Sub 
End If 

'find first empty row in database 
Set rngName = ws.Cells.Find(What:="Name", SearchOrder:=xlRows, _ 
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole) 

If Not rngName is Nothing then 
    With ws.cells(rows.count, rngName.Column).End(xlUp).offset(1,0).entirerow 
     .Cells(1).Value = pNum 
     .Cells(2).Value = Me.TxtLocation.Value 
     .Cells(3).Value = Me.TxtDate.Value 
     .Cells(4).Value = Me.TxtQuantity.Value 
    End With 
Else 
    msgbox "'Name' header not found!" 
End if 
+0

好点。读取代码注释时,听起来好像可能有很多行,并且代码应该将新信息添加到第一个空行,尽管这个问题中的叙述可能会被读取。 –

相关问题