使用OptionButton搜索列标题,并使用Excel VBA插入新列

使用OptionButton搜索列标题,并使用Excel VBA插入新列,excel,vba,search,insert,radio-button,Excel,Vba,Search,Insert,Radio Button,大家好,我是VBA编程新手,有一些困难 我有一个带有选项按钮的用户表单。所以我想要的是,当我点击一个OptionButton时,代码将在Tabell2的列中搜索,当找到时,插入一个新列到左侧 我的代码显然是错误的和/或写得不好 Private Sub OptionButton1_Click() Dim cl As Range If OptionButton1.Value = True Then Search "10700" For Each cl In Worksheets("Tab

大家好,我是VBA编程新手,有一些困难

我有一个带有选项按钮的用户表单。所以我想要的是,当我点击一个OptionButton时,代码将在Tabell2的列中搜索,当找到时,插入一个新列到左侧

我的代码显然是错误的和/或写得不好

Private Sub OptionButton1_Click()
  Dim cl As Range
  If OptionButton1.Value = True Then Search "10700"
  For Each cl In Worksheets("Tabelle2").Range("1:1")
  If cl = "10700" Then cl.EntireColumn.Activate
  End If
  End Sub

Private Sub AddColumn()
  Dim cl As Range

  For Each cl In Worksheets("Dokumentenübersicht").Range("1:1")
    If cl = Active Then
       cl.EntireColumn.Insert Shift:=xlToLeft
    End If

    cl.Offset(0, 1) = "role"
Next cl
End Sub

您似乎在不同的工作表之间工作,但这里有一个大致轮廓

以下假设您正在搜索和插入
工作表(“Tabelle2”)

它使用
Range.Find
方法来定位感兴趣的字符串。根据您的代码,要搜索的范围当前设置为第1行

Option Explicit
Private Sub OptionButton1_Click()
    Dim cl As Range
    If  OptionButton1 Then
        Set cl = Worksheets("Tabelle2").Range("1:1").Find("10700")
        If Not cl Is Nothing Then cl.EntireColumn.Insert Shift:=xlToLeft
    End If
End Sub

如果做了一些研究,发现了一个非常相似的问题:但与组合的选择框,它是不同的东西。所以我试着激活列和活动列旁边的插入。它工作了!非常感谢你!!!我现在向前迈进了一步,可以继续与它合作!