Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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/0/mercurial/2.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
Excel 仅将单元格添加到列范围_Excel_Insert_Row_Vba - Fatal编程技术网

Excel 仅将单元格添加到列范围

Excel 仅将单元格添加到列范围,excel,insert,row,vba,Excel,Insert,Row,Vba,我正在尝试编写一个宏(VBA/编写代码的新宏),该宏将向现有列表(Col B)添加一个值。该值由输入框定义,然后按字母顺序添加到列表中。我可以让宏工作,但是我想在一个范围内添加单元格,而不是整行 我的电子表格在A列到K列中有数据。这是我要插入“行”的区域。然而,有一个陷阱。我在列L到AF中有另一组数据,我不想将行添加到其中。这就是我失败的地方,因为我只能插入完整的行 有办法做到这一点吗?我没有遇到过任何方法,因为插入必须基于新值在列表中的位置(按字母顺序)进行,这会阻止我选择要插入的位置。我曾尝

我正在尝试编写一个宏(VBA/编写代码的新宏),该宏将向现有列表(Col B)添加一个值。该值由输入框定义,然后按字母顺序添加到列表中。我可以让宏工作,但是我想在一个范围内添加单元格,而不是整行

我的电子表格在A列到K列中有数据。这是我要插入“行”的区域。然而,有一个陷阱。我在列L到AF中有另一组数据,我不想将行添加到其中。这就是我失败的地方,因为我只能插入完整的行

有办法做到这一点吗?我没有遇到过任何方法,因为插入必须基于新值在列表中的位置(按字母顺序)进行,这会阻止我选择要插入的位置。我曾尝试录制宏以查看代码,但由于选择是由值定义的,因此无法操作该输入

这是我到目前为止的代码…可能有点笨重,因为我还在学习

Sub Add_Project()
Dim NewProject As String, iRow As Long, ProjRng As Range, RowRng As Range
'The ProjRng MUST represent the project column!
    Set ProjRng = Range("B:B")
'Defines the range of columns to add a row to
    Set RowRng = Range("B:K")
'Create message box for user input on project name
    NewProject = InputBox("Enter Project Name")
    If NewProject = "" Then Exit Sub
'Determines if the New Project name already exists
    iRow = Application.WorksheetFunction.Match(NewProject, ProjRng)
    If Cells(ProjRng.row + iRow - 1, ProjRng.Column) = NewProject Then
        MsgBox ("Project already exists")
Exit Sub
    End If
'Inserts a new row with containing the new Project name
    With Cells(ProjRng.row + iRow, ProjRng.Column)
        .EntireRow.Insert
        .Offset(-1, 0).Value = NewProject
    End With
Exit Sub
End Sub
我意识到宏正在执行我指示它执行的操作。我想在添加“EntireRow”的部分中添加一些只会增加A:K列范围的内容。对于我可以从哪里开始的任何建议或指针,我将不胜感激。
谢谢

tigeravatar-非常感谢您的帮助!!代码工作得很好。我很感激您能够简化代码以删除许多不必要的行。在我继续适应VBA的世界时,我将详细研究这一点,以从您的建议中学习。一个澄清的问题。使用If/CountIf函数与使用Match函数相比,有哪些优点/缺点?我喜欢你的方法,很想知道这是一个偏好还是仅仅是一个好的编码。谢谢!我更喜欢Countif,因为它比Match更不容易出错,所以我认为它是测试是否存在某些东西的好方法。当您需要查找某物的位置时,“匹配”非常有用。精确匹配很容易出错,所以我尽量避免。像这样,你只需要知道它在列表中的位置就可以了。再次感谢你的帮助!!
Sub Add_Project()

    Dim strNewProject As String
    Dim iRow As Long

    strNewProject = InputBox("Enter Project Name")
    If Len(strNewProject) = 0 Then Exit Sub 'Pressed cancel

    If WorksheetFunction.CountIf(Columns("B"), strNewProject) > 0 Then
        MsgBox "Project already exists"
        Exit Sub
    End If

    iRow = WorksheetFunction.Match(strNewProject, Columns("B")) + 1
    Intersect(Range("A:K"), Rows(iRow)).Insert xlShiftDown
    Cells(iRow, "B").Value = strNewProject

End Sub