问题描述
我很乐意在VBA中进行编程。我在互联网上阅读了一些东西,但是我找不到我需要的东西,或者无法找到它。我的问题:单元格B6中的工作表工作表1中的给出了一个项目将被利用多少年的值。
$工作表2和表3中的b $ b
我制作了一个电子表格50年(1年到50年,第7行到第56行)。
在sheet 1中的单元格b6中,我想输入1到50之间的值。当值为49时,我想隐藏sheet2和sheet 3中的第56行。当值为48时,我想在sheet2和sheet 3中隐藏行55:56,依此类推。
这是我到目前为止,但是我不能让它自动工作,当我更改单元格B6中的值:
Sub test1()
如果Range(sheet1!B6)= 50然后
Rows(52:55)。EntireRow.Hidden = False
Else
如果范围(sheet1!B6)= 49然后
行(55)。EntireRow.Hidden = True
Else
如果范围(sheet1!B6)= 48然后
Rows(54:55)。EntireRow.Hidden = True
结束If:End If:End If:
End Sub
我希望有人可以帮助我解决问题。
谢谢
嗯,你在正确的道路上,Benno!
有一些关于VBA编程的提示,可能会帮助你。
1-使用你要交互的工作表的总是明确的引用。否则,Excel可能会假设你的代码适用于活动工作表,最终你会看到它螺丝你的电子表格。
2-如Lionz所提到的,联系与Excel提供的本机方法。你可以在大部分的技巧中使用它们。
3-明确地声明你的变量...他们将显示每个对象在VBA中提供的方法的列表。它可能会节省您在互联网上挖掘的时间。
现在,我们来一个代码草案...
记住这个代码必须在Excel Sheet对象内,如狮子座所解释的。它仅适用于Sheet 2,由您自己的方式适用于Sheet 2和Sheet 3。
希望它有帮助!
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oSheet As Excel.Worksheet
'如果改变的单元格是B6,我们只想做某事,对吗?
如果Target.Address =$ B $ 6然后
'检查它是否是一个数字...
如果IsNumeric(Target.Value)然后
'让我们避免你的债券中的价值,正确吗?
如果Target.Value> 0和Target.Value< 51然后
'让我们分配工作表,我们将显示/隐藏行到一个变量,然后
'仅使用对变量本身的引用而不是表单名称。
'它更安全
'您可以替换sheet 22(不含引号),这将代表
'工作簿中的工作表索引
设置oSheet = ActiveWorkbook.Sheets(Sheet 2)
'我们将在隐藏之前取消隐藏,以确保我们隐藏正确的
oSheet.Range(A7:A56)。EntireRow.Hidden = False
oSheet.Range(A& Target.Value + 7&:A56)。EntireRow.Hidden = True
如果
结束如果
End If
End Sub
I'm kinda new to programming in VBA. I read some stuff on the internet but i couldnt find what i need or couldnt get it working. my problem:
in worksheet 'sheet 1' in cell B6 a value is given for how many years a project will be exploited.
in worksheets 'sheet 2' and 'sheet 3' i made a spreadsheet for 50 years ( year 1 to year 50; row 7 to row 56).
in cell b6 in 'sheet 1' i want to enter a value between 1 and 50. when the value is 49 i want to hide row 56 in 'sheet2' and 'sheet 3'. when the value is 48 i want to hide rows 55:56 in 'sheet2' and 'sheet 3', and so on.this is what i got so far but i cant get it to work automaticly when i change the value in cell B6:
Sub test1()
If Range("sheet1!B6") = 50 Then
Rows("52:55").EntireRow.Hidden = False
Else
If Range("sheet1!B6") = 49 Then
Rows("55").EntireRow.Hidden = True
Else
If Range("sheet1!B6") = 48 Then
Rows("54:55").EntireRow.Hidden = True
End If: End If: End If:
End Sub
i hope someone can help me with my problem.
Thank you
Well, you're on the right path, Benno!
There are some tips regarding VBA programming that might help you out.
1- Use always explicit references to the sheet you want to interact with. Otherwise, Excel may 'assume' your code applies to the active sheet and eventually you'll see it screws your spreadsheet up.
2- As lionz mentioned, get in touch with the native methods Excel offers. You might use them on most of your tricks.
3- Explicitly declare your variables... they'll show the list of methods each object offers in VBA. It might save your time digging on the internet.
Now, let's have a draft code...
Remember this code must be within the Excel Sheet object, as explained by lionz. It only applies to Sheet 2, is up to you to adapt it to both Sheet 2 and Sheet 3 in the way you prefer.
Hope it helps!
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oSheet As Excel.Worksheet
'We only want to do something if the changed cell is B6, right?
If Target.Address = "$B$6" Then
'Checks if it's a number...
If IsNumeric(Target.Value) Then
'Let's avoid values out of your bonds, correct?
If Target.Value > 0 And Target.Value < 51 Then
'Let's assign the worksheet we'll show / hide rows to one variable and then
' use only the reference to the variable itself instead of the sheet name.
' It's safer.
'You can alternatively replace 'sheet 2' by 2 (without quotes) which will represent
' the sheet index within the workbook
Set oSheet = ActiveWorkbook.Sheets("Sheet 2")
'We'll unhide before hide, to ensure we hide the correct ones
oSheet.Range("A7:A56").EntireRow.Hidden = False
oSheet.Range("A" & Target.Value + 7 & ":A56").EntireRow.Hidden = True
End If
End If
End If
End Sub
这篇关于用于在excel 2010中隐藏行的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!