简体   繁体   English

Excel VBA:工作表将单元格值更改为其他工作表

[英]Excel VBA: Worksheet Change cell value to different sheet

I've been working on this for some time now and have hit a real stumbling block. 我已经为此工作了一段时间,遇到了一个真正的绊脚石。

I have a set of values that are available via a validated dropdown menu in Sheet 3, Column D. Once selected this value currently displays in a different sheet (Sheet 7) using excel function ='Sheet 3'!D4 and so on, and I have some code that reads this and performs an IF statement to produce a value in another cell. 我有一组值,这些值可通过工作表3的D列中的经过验证的下拉菜单获得。一旦选择此值,当前将使用excel函数='工作表3'!D4等显示在其他工作表(工作表7)中,等等。我有一些读取此代码并执行IF语句以在另一个单元格中产生值的代码。

My problem is the code is dependant on reading the value and not the formula. 我的问题是代码取决于读取值而不是公式。

I currently have a worksheet change command for a separate function I want to run, is there a way for this to run a second function and call any changes from sheet 3 column D into sheet 8 column D and then run my other change function? 我目前有一个要运行的单独函数的工作表更改命令,有没有办法让它运行第二个函数,并从工作表3列D到工作表8列D调用任何更改,然后运行我的其他更改函数?

Sheet 7 Code: 表格7的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range

If Intersect(Target, Range("D2:D102")) Is Nothing Then Exit Sub
Application.EnableEvents = False
On Error GoTo Finalize

For Each c In Target.Cells
Select Case c.Column
Case 4
Call Print_Quality(c)
End Select
Next c

Finalize:
Application.EnableEvents = True
End Sub

Sheet 7 Module: 工作表7模块:

Sub Print_Quality(c As Range)
Dim PrintQuality As String
Dim PrintSpeed As String

PrintQuality = c.Value

If PrintQuality = "A Quality 1" Then PrintSpeed = "100"

c.Offset(0, 5).Value = PrintSpeed

End Sub

I've been trying this route but to no avail: 我一直在尝试这条路线,但无济于事:

Worksheet 3 code: 工作表3的代码:

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("D4:D104")) Is Nothing Then Exit Sub
Application.EnableEvents = False
On Error GoTo Finalize

UpdateVal

Finalize:
Application.EnableEvent = True

End Sub

Module: 模块:

Sub UpdateVal()
Worksheets("Sheet 7").Range("D2").Value = Worksheets("Sheet 3").Range("D4").Value
End Sub

Many thanks 非常感谢

Sods law, I've managed to fix this an hour after my desperation post. 索德斯法律,我在绝望后的一个小时内设法解决了这个问题。

I completed another worksheet change from the sheet it was calling from (Sheet 3) 我完成了另一个工作表的更改(从工作表3调用)

    Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range

Set KeyCells = Range("D4:D104")

If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then

Call UpdateVal

End If

End Sub

then added this function into the module 然后将此功能添加到模块中

Sub UpdateVal()
Sheet8.Cells(2, 4).Value = Sheet3.Cells(4, 4)
End Sub

this now references the value of the dropdowns in sheet 8 and allows other functionality to continue using the cell value 现在,它引用了工作表8中下拉菜单的值,并允许其他功能继续使用单元格值

Have you tried stepping through your code to see where it is having an issue? 您是否尝试单步执行代码以查看问题所在? It not, I would suggest putting a break at the beginning of each module, and then use F8 to step through. 并非如此,我建议在每个模块的开头放置一个休息时间,然后使用F8逐步执行。 This will confirm it is running as it should. 这将确认它正在正常运行。

You should also be fully-qualifying your references to worksheets. 您还应该完全限定对工作表的引用。 While presumably the sheet references should carry through given that they are in worksheet modules, there is the chance of failure. 尽管假定工作表引用位于工作表模块中,但应该可以进行工作表引用,但存在失败的机会。 You can simply assign a variable to hold the worksheet like so: 您可以简单地分配一个变量来保存工作表,如下所示:

Dim wb as Workbook
Dim ws as Worksheet

Set wb = ThisWorkbook
Set ws = wb.Sheets("YourSheetName")

Additionally, your worksheet 3 code: EnableEvent = True 此外,您的工作表3代码:EnableEvent = True

Should be: EnableEvents = True 应该是:EnableEvents = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Excel 中的不同工作表获取单元格值以在 VBA 中使用它? - Get a cell value from a different worksheet in Excel to use it in VBA? Excel VBA如何根据不同工作表中单元格的值删除行 - Excel VBA How to delete rows based on the value of a cell in a different worksheet VBA excel更改具有不同值的单元格 - VBA excel change cell with different value Excel宏根据第一张工作表中的特定单元格值在其他工作表中添加单元格值 - Excel macro add a cell value in a different worksheet based on a specific cell value in the first sheet Excel VBA:将表中的单元格值复制到有条件的其他工作表中的表 - Excel VBA: Copy cell value in a table to a table on a different sheet with condition Excel VBA 根据整个工作表中的多项选择更改单元格值 - Excel VBA to change a cell value depending on multiple selection throughout the sheet 如何使用另一个工作表中单元格内的单元格地址更改具有 Excel vba 的单元格的值? - How to change the value of a cell with Excel vba using a cell address that's inside a cell in another worksheet? 如果单元格值包含在另一个工作表中,则为Excel vba - Excel vba if cell value is included in another worksheet vba excel在日期单元格值上隐藏工作表 - vba excel hide worksheet on date cell value 根据 Excel VBA 中的单元格值选择工作表 - Selecting a worksheet based on a cell value in Excel VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM