Run Macro When Formula Result Changes

redbaron06

New Member
Joined
Aug 6, 2010
Messages
44
Hi everyone, I would really appreciate some VBA help.

I would like the macro below to run automatically if the cell "BO1" Changes from its previous value. Cell "BO1" is determinded by drop down menus on another worksheet. Any help would be greatly appreciated.

-Terry

___________________________

Sub DoGetData()
Dim strFile As String
Dim strRange As String
strFile = Range("BO1").Value
GetDataFromClosedWorkbook strFile, "A8:N200", ActiveCell, False
End Sub
'GetDataFromClosedWorkbook "C:\FolderName\WorkbookName.xls", _
' "A1:B21", Active - Cell, False
' GetDataFromClosedWorkbook "U:\MPLS\KAZ\306_KAZAKHSTAN__PSC_20100602.xls", "A1:J200", "A10", False
'

Sub GetDataFromClosedWorkbook(SourceFile As String, SourceRange As String, _
TargetRange As Range, IncludeFieldNames As Boolean)
Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset
Dim dbConnectionString As String
Dim TargetCell As Range, i As Integer
dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" & _
"ReadOnly=1;DBQ=" & SourceFile
Set dbConnection = New ADODB.Connection
On Error GoTo InvalidInput
dbConnection.Open dbConnectionString ' open the database connection
Set rs = dbConnection.Execute("[" & SourceRange & "]")
Set TargetCell = TargetRange.Cells(1, 1)
If IncludeFieldNames Then
For i = 0 To rs.Fields.Count - 1
TargetCell.Offset(0, i).Formula = rs.Fields(i).Name
Next i
Set TargetCell = TargetCell.Offset(1, 0)
End If
TargetCell.CopyFromRecordset rs
rs.Close
dbConnection.Close ' close the database connection
Set TargetCell = Nothing
Set rs = Nothing
Set dbConnection = Nothing
On Error GoTo 0
Exit Sub
InvalidInput:
MsgBox "The source file or source range is invalid!", _
vbExclamation, "Get data from closed workbook"
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_Calculate()
Static OldVal As Variant
If Range("BO1").Value <> OldVal Then
    OldVal = Range("BO1").Value
    Call DoGetData
End If
End Sub


How can be this done for range of cells? I mean instead of BO1 if it is range BO1:BO150?
Actually I want macro which can speak cell value so I used application.speak.speech which works ok but real problem is whenever value changes of one cell it speaks all the range values BO1:BO150 all over again. this is the problem I'm facing when I'm applying above mentioned logic to range of cells? I will be really thankful if you can provide any fix for this.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top