大家好,我是VBA编程的新手,遇到了一些困难...
我有一个带OptionButtons的UserForm。所以我想要的是,当我单击OptionButton时,代码将在Tabell2的列中搜索,并在找到时插入新的ToLeft列。
我的代码显然是错误的和/或写得不好...
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
答案 0 :(得分:1)
您似乎在不同的工作表之间工作,但这是一个概述。
以下内容假设您同时在Worksheets("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