范围搜索并创建具有特定字符串名称

时间:2017-05-16 17:22:01

标签: excel vba excel-vba

我想从userform搜索存储在单元格中的输入,如果值可用,那么sub将退出,否则代码应该创建一个带有字符串名称的工作表。完整的代码看起来像这样:

Public CE_LASTROW_PNO_CHK, i As Integer
Public PNO As String
Sub CMD_CEENTRYOK_Click()
Dim emptyRow As Long

'---------------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------------

'Make CE_MASTER_DATA active
    CE_MASTER_DATA.Activate
'---------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------
'Determine emptyRow
    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'---------------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------------
'Transfer information
Cells(emptyRow, 1).Value = "P-" & CE_P_MID.Value & "-" & CE_P_LAST.Value
Cells(emptyRow, 2).Value = UNIT.Value
Cells(emptyRow, 3).Value = CE_PHASE.Value
Cells(emptyRow, 4).Value = CE_DISCRIPTION.Value
Cells(emptyRow, 5).Value = CE_ENTRY_RCVD_DD.Value
Cells(emptyRow, 6).Value = CE_ENTRY_RCVD_MM.Value
Cells(emptyRow, 7).Value = CE_ENTRY_RCVD_YYYY.Value
Call CE_EXISTING_PNO_CHK
End Sub
'check for the existing P-No
Sub CE_EXISTING_PNO_CHK()

CE_LASTROW_PNO_CHK = WorksheetFunction.CountA(Range("A:A")) - 1
CE_LASTROW_PNO_CHK = CE_LASTROW_PNO_CHK + 1

    For i = 1 To CE_LASTROW_PNO_CHK
     If Sheets("CE_MASTER_DATA").Cells(1, i) = "P-" & CE_P_MID.Value & "-" & 
CE_P_LAST.Value Then
        Rows(i).Select
        Selection.Delete Shift:=xlUp
        MsgBox (" Project already exists")
     Exit Sub
    End If
Next i
Call CreateSheet
ActiveWorkbook.Save
Unload Me
End Sub

 Sub CreateSheet()
For Each Worksheet In ThisWorkbook.Worksheets
    If Worksheet.Name = PNO Then
    Exit Sub ' if found - exit sub
    End If
Next Worksheet
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = PNO
End Sub

用户表单看起来像这样enter image description here问题是以太计数器工作不正常或我无法正确调试它。此外,检查现有工作表是错误的。

任何帮助将不胜感激

0 个答案:

没有答案