简体   繁体   中英

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.

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?

Sheet 7 Code:

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:

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:

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)

    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

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. 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

Should be: EnableEvents = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM