VBA代码学习-插入列,并且输入data

Sub FindAndInsert()
    Dim searchRange As Range
    Dim foundCell As Range
    Dim colNum As Integer
    Dim insertCol As Integer
    Dim lastRow As Long
    Dim insertData As Variant
    
    '设置查找范围为第一行
    Set searchRange = Range("1:1")
    
    '查找33
    Set foundCell = searchRange.Find(What:=33, LookIn:=xlValues, LookAt:=xlWhole)
    If Not foundCell Is Nothing Then
        colNum = foundCell.Column
        insertCol = colNum '添加一列在找到的列数前面
        Columns(insertCol).Insert Shift:=xlToRight '插入列
        lastRow = Cells(Rows.Count, insertCol - 1).End(xlUp).Row '获取新列最后一行
        insertData = Array("InsertData1", "InsertData2", "InsertData3", "InsertData4", "InsertData5", "InsertData6", "InsertData7", "InsertData8", "InsertData9", "InsertData10", "InsertData11", "InsertData12", "InsertData13") '在新列中插入不同的数据
        Range(Cells(2, insertCol), Cells(lastRow, insertCol)).Value = Application.Transpose(insertData)
    End If
    
    '查找48
    Set foundCell = searchRange.Find(What:=48, LookIn:=xlValues, LookAt:=xlWhole)
    If Not foundCell Is Nothing Then
        colNum = foundCell.Column
        insertCol = colNum '添加一列在找到的列数前面
        Columns(insertCol).Insert Shift:=xlToRight '插入列
        lastRow = Cells(Rows.Count, insertCol - 1).End(xlUp).Row '获取新列最后一行
        insertData = Array("InsertData14", "InsertData15", "InsertData16", "InsertData17", "InsertData18", "InsertData19", "InsertData20", "InsertData21", "InsertData22", "InsertData23", "InsertData24", "InsertData25", "InsertData26") '在新列中插入不同的数据
        Range(Cells(2, insertCol), Cells(lastRow, insertCol)).Value = Application.Transpose(insertData)
    End If
    
    '查找82
    Set foundCell = searchRange.Find(What:=82, LookIn:=xlValues, LookAt:=xlWhole)
    If Not foundCell Is Nothing Then
        colNum = foundCell.Column
        insertCol = colNum '添加一列在找到的列数前面
        Columns(insertCol).Insert Shift:=xlToRight '插入列
        lastRow = Cells(Rows.Count, insertCol - 1).End(xlUp).Row '获取新列最后一行
        insertData = Array("InsertData27", "InsertData28", "InsertData29", "InsertData30", "InsertData31", "InsertData32", "InsertData33", "InsertData34", "InsertData35", "InsertData36", "InsertData37", "InsertData38", "InsertData39") '在新列中插入不同的数据
        Range(Cells(2, insertCol), Cells(lastRow, insertCol)).Value = Application.Transpose(insertData)
    End If
End Sub

在代码中,首先将要查找的元素定义为33、48和82,然后设置查找范围为第一行。使用Find函数查找每一个元素,并获取它所在的列数。接着,在该列前面插入一列,并在新列中插入13个不同的数据。这里将新列数据设置为不同的数组,可以根据需要进行修改。需要注意的是,在插入数据时,需要使用Application.Transpose函数将一维数组转置成列。最后,循环结束后,新列就会添加到找到的元素所在的列数的前面,而且新列中也会插入13个不同的数据。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喀什的小姑娘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值