表格数据到特定的单元格

在Excel工作表2我有列A和D的名称,B&E的开始date和列C&F是结束date和窗体与combobox(加载名称)和两个文本框。

当我点击提交button时,它会search与ComboBox值匹配的名称的列,然后将两个文本框的值写入右边相邻的两个EMPTY单元

在这里输入图像说明

Private Sub CommandButton4_Click() Dim irow As Long Dim ws As Worksheet Set ws = Worksheets("Sheet2") With ws .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Value = Me.Combo.Value .Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0).Value = Me.sttdate.value .Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = Me.enddate.Value End With With Me .Combo.Value = "" .startdate.Value = "" .enddate.Value = "" End With End Sub 

此代码将所有表单的值添加到AB&C列中

这应该工作得很好:

 Private Sub CommandButton4_Click() Dim irow As Long, _ wS As Worksheet, _ NextRow As Long, _ cF As Range Set wS = Worksheets("Sheet2") With wS With .Range("A:A") 'First, define properly the Find method Set cF = .Find(What:=Me.Combo.Value, _ After:=.Cells(1, 1), _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False, _ SearchFormat:=False) End With 'If there is a result, keep looking with FindNext method If Not cF Is Nothing Then If cF.Offset(0, 1) <> vbNullString Then Set cF = cF.End(xlToRight).Offset(0, 1) cF.Value = Me.sttdate.Value cF.Offset(0, 1).Value = Me.EndDate.Value Else .Cells(cF.Row, "B").Value = Me.sttdate.Value .Cells(cF.Row, "C").Value = Me.EndDate.Value End If Else NextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row .Cells(NextRow, "A").Value = Me.Combo.Value .Cells(NextRow, "B").Value = Me.sttdate.Value .Cells(NextRow, "C").Value = Me.EndDate.Value End If End With With Me .Combo.Value = "" .StartDate.Value = "" .EndDate.Value = "" End With End Sub 

这应该做的伎俩。 我根据你在解释中写的内容添加了一些检查,以防它有帮助。

 Private Sub CommandButton4_Click() Dim irow As Long Dim ws As Worksheet Set ws = Worksheets("Sheet2") With ws irow = .Range("A" & .Rows.Count).End(xlup).Row Dim rFound as Range Set rFound = .Range("A1:A" & iRow).Find(Me.Combo.Value, lookat:=xlWhole) If not rFound is Nothing Then If IsEmpty(rFound.Offset(,1)) and IsEmtpy(rFound.Offset(,2)) Then rFound.Offset(,1) = Me.sttdate.value rFound.Offset(,2) = Me.enddate.value With Me .Combo.Value = "" .startdate.Value = "" .enddate.Value = "" End With Else Msgbox "Name already has values" End If Else Msgbox "Name not Found" End If End Sub