Excel 2010 中隐藏行的宏

发布于 2024-11-18 08:15:07 字数 723 浏览 2 评论 0原文

我对 VBA 编程有点陌生。我在互联网上阅读了一些内容,但找不到我需要的内容或无法使其正常工作。我的问题:

在单元格 B6 的工作表“表 1”中,给出了项目将被利用的年数。

在工作表“sheet 2”和“sheet 3”中,我制作了 50 年的电子表格(第 1 年到第 50 年;第 7 行到第 56 行)。

在“sheet 1”的单元格 b6 中,我想输入一个 1 到 50 之间的值。当值为 49 时,我想隐藏“sheet2”和“sheet 3”中的第 56 行。当值为 48 时,我想隐藏“sheet2”和“sheet 3”中的第 55:56 行,依此类推。 这是我到目前为止所得到的,但当我更改单元格 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'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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦明 2024-11-25 08:15:07

你几乎明白了。
您正在隐藏活动工作表中的行。这没关系。但更好的方法是在它所在的位置添加。

Rows("52:55").EntireRow.Hidden = False

activesheet.Rows("52:55").EntireRow.Hidden = False

没有它我就会发生奇怪的事情 至于让它自动化。您需要在 VBA 编辑器(不是模块,双击编辑器最左侧的sheet1)中的工作表宏中使用 worksheet_change 事件。)在该工作表中,使用编辑器本身上方的下拉菜单(应该有2 个列表框)。左侧的列表框将包含您要查找的事件。之后只需放入宏即可。它应该类似于下面的代码,

Private Sub Worksheet_Change(ByVal Target As Range)
test1
end Sub

就是这样。每当您更改某些内容时,它都会运行宏 test1。

You almost got it.
You are hiding the rows within the active sheet. which is okay. But a better way would be add where it is.

Rows("52:55").EntireRow.Hidden = False

becomes

activesheet.Rows("52:55").EntireRow.Hidden = False

i've had weird things happen without it. As for making it automatic. You need to use the worksheet_change event within the sheet's macro in the VBA editor (not modules, double click the sheet1 to the far left of the editor.) Within that sheet, use the drop down menu just above the editor itself (there should be 2 listboxes). The listbox to the left will have the events you are looking for. After that just throw in the macro. It should look like the below code,

Private Sub Worksheet_Change(ByVal Target As Range)
test1
end Sub

That's it. Anytime you change something, it will run the macro test1.

唯憾梦倾城 2024-11-25 08:15:07

好吧,你走在正确的道路上,本诺!

有一些有关 VBA 编程的提示可能会对您有所帮助。

  1. 始终使用对要与之交互的工作表的显式引用。否则,Excel 可能会“假设”您的代码适用于活动工作表,最终您会发现它搞砸了您的电子表格。

  2. 正如 lionz 提到的,接触 Excel 提供的本机方法。您可能会在大多数技巧中使用它们。

  3. 显式声明您的变量...它们将显示每个对象在 VBA 中提供的方法列表。它可能会节省您在互联网上挖掘的时间。

现在,让我们有一个草稿代码...

请记住,此代码必须位于 Excel Sheet 对象内,如 lionz 所解释的。它仅适用于 Sheet 2,您可以按照自己喜欢的方式将其适应 Sheet 2 和 Sheet 3。

希望有帮助!

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文