Visual Basic语言:在excel中插入行、合并特定单元格和添加特定文本

8zzbczxx  于 2023-03-04  发布在  其他
关注(0)|答案(1)|浏览(122)

这是我第一次在这里发帖。
我的目标如下:我想在搜索特定单元格值后插入特定数量的行,并合并这些插入单元格的某些列。我还想向插入单元格中的另一列添加特定值。我知道这听起来很模糊,但在最后我添加了一些图片,希望能让事情更清楚。在您评论我不应该合并单元格之前,这是我老板想要的,所以要撕掉。
系统(输入框)的过程如下所示:
1.您要查找哪个值?
1.选择要查找的范围。
1.您要添加多少行?
1.您希望在行[array#]中使用哪个值?
1.您要将哪些列与插入的行合并?
在Excel中,该过程之前看起来像this,之后看起来像this。如您所见,现在在“Car”之后又插入了3行。列“type”、“Tag number”和“Cost”现在合并,而“tire number”有3个新添加的值,即“1”、“2”和“3”。
我已经写了一些代码,这有助于我在告诉Excel需要查找哪个单元格值后插入动态数量的行。问题是我不明白如何设置单元格合并的范围,以及如何为添加的每一行添加特定单元格中的特定值。基本上,我不明白Excel中的“范围”功能,因为我的大脑有90%是果冻。

Private Sub ToggleButton1_Click()Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next

inputSearch = InputBox("After which value would you like to add a row?")
inputAmountOfRows = InputBox("How many Rows would you like to add?")
inputColumnRangeCopy1 = InputBox("From which column would you like to start copying?")
inputColumnRangeCopy2 = InputBox("From which column would you like to end copying?")

Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Select Range", WorkRng.Address, Type:=8)
Set WorkRng = WorkRng.Columns(1)

xLastRow = WorkRng.Rows.Count
Application.ScreenUpdating = False

For xRowIndex = xLastRow To 1 Step -1
    Set Rng = WorkRng.Range("A" & xRowIndex)
    If Rng.Value = inputSearch Then
        For i = inputAmountOfRows To 1 Step -1
        Rng.Offset(1, 0).EntireRow.Insert Shift:=xlDown
            With Range(????)
                .Merge
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlTop
            End With
        Next i
    End If
Next xRowIndex

Application.ScreenUpdating = True

End Sub
gudnpqoy

gudnpqoy1#

如果我没理解错的话...

Sub test()
Dim inputSearch As String: Dim inputAmountOfRows As Integer
Dim c As Range: Dim fa As String: Dim addr As String
Application.ScreenUpdating = False

inputSearch = InputBox("Which value would you like to add a row?")
inputAmountOfRows = InputBox("How many rows would you need for " & inputSearch & "?")

Set c = ActiveSheet.Columns(2).Find(inputSearch, lookat:=xlWhole)
If c Is Nothing Or c.MergeCells Then Exit Sub
fa = c.Address

    Do
        addr = c.Offset(0, -1).Address
        
        With Range(addr)
            .Resize(1, 4).Copy
            .Resize(inputAmountOfRows - 1, 1).Insert Shift:=xlDown
        End With

        With Range(addr).Offset(0, 3)
            .AutoFill Destination:=.Resize(inputAmountOfRows, 1), Type:=xlFillSeries
        End With

        Application.DisplayAlerts = False
        For i = 0 To 2
        With Range(addr).Offset(0, i).Resize(inputAmountOfRows, 1)
            .Merge
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlTop
        End With
        Next i
        Application.DisplayAlerts = False

        Set c = ActiveSheet.Columns(2).FindNext(c)
    
    Loop Until c.Address = fa

End Sub

inputSearch是用户类型(如汽车、自行车、飞机等)的位置。
inputAmountOfRows是用户键入所需行数的位置。
代码首先检查用户输入的inputSearch是否在第2列中找到。如果没有找到,或者找到了但找到的单元格已经合并,则退出sub。
然后循环以搜索是否存在下一个相同值。
在循环内:
它会复制与inputAmountOfRows一样多的内容。
如果填充系列列3
它合并列A、B和C
老实说,我不明白:
inputColumnRangeCopy1 = InputBox("From which column would you like to start copying?")
inputColumnRangeCopy2 = InputBox("From which column would you like to end copying?")
这就是为什么我没有在上面的代码中使用它。
请注意,代码假定D列(轮胎编号)中的每一行在开始条件下的值为1。

相关问题