我正在尝试使用多个表单选项按钮,如下面的代码所示。目标是从一列复制一系列数据并将其粘贴到另一列中。没有什么花哨。所以,我有多个选项按钮和一个命令按钮,这是Button38。当我运行代码时,我没有收到错误消息,但它无法正常工作。任何帮助将非常感谢,我也是VBA的新手。
Sub Button38_Click()
Application.ScreenUpdating = False
Sheets("Sheet2").Visible = True
Sheets("Sheet2").Select
If OptionButton22 = True Then
Range("AI2:AI182").Copy
Range("AK2:AK182").PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
ElseIf OptionButton23 = True Then
Range("AD2:AD182").Copy
Range("AK2:AK182").PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
ElseIf OptionButton24 = True Then
Range("AE2:AE182").Copy
Range("AK2:AK182").PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
End If
Sheets("Sheet1").Select
Sheets("Sheet2").Visible = False
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:1)
我将所有内容重新设置为Sheet1和sheet2。 Sheet1是具有Form选项按钮的那个,sheet2具有列。我想让sheet2一直隐藏起来。
您正在使用表单控件(选项按钮)。此外,您无需取消隐藏/隐藏工作表。让它保持隐藏。此代码将起作用,即使隐藏string rawInput;
vector<vector<int>> temp;
while(getline(cin, rawInput) ){
istringstream bufferInput(rawInput);
temp.push_back(vector<int>{std::istream_iterator<int>{bufferInput}, std::istream_iterator<int>{}});
}
,范围也会被复制粘贴。
这是你在尝试什么?
Sheet2
答案 1 :(得分:0)
如果您将所有对象限定为其父对象,则代码应该有效。见下文。 (我还重新编写了一些代码,使其更清晰,更易于维护/读取,并且更短)。
Sub Button38_Click()
Dim ws1 as Worksheet
Set ws1 = Worksheets("Sheet1")
Application.ScreenUpdating = False
With Sheet2 'using the VBA sheet object name (change if needed)
.Visible = xlSheetVisible
Dim sCol As String
If .OptionButton22 = True Then
sCol = "AI"
ElseIf .OptionButton23 = True Then
sCol = "AD"
ElseIf .OptionButton24 = True Then
sCol = "AE"
End If
.Visible = xlSheetHidden
End With
ws1.Range("AK2:AK182").Value = ws1.Range(sCol & "2:" & sCol & "182").Value
'if you need the range theme copied as well use the code below
'With ws1
'.Range(sCol & "2:" & sCol & "182").Copy
'.Range("AK2:AK182").PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
'End With
End Sub