I have a project I am working on where my team uses an excel front end to manipulate the data which in turn updates an access database back-end to hold the database. (there are good reasons for this)
The current version works by if a user changes data in a cell and wants to update the database they highlight the cell(s) and hit an update button. (this becomes annoying doing multiple updates). So I started playing with the worksheet_changed function.
In order for the worksheet_changed function to work the user has to move off of the 'updated' cell in order for excel to notice the change and update the code. (In my case hitting enter or down arrow after data entry). I have gotten this to work well using the offset property to look at the row above and enter that line into the database - however - when the spreadsheet is filtered as it always is...if the row above happens to be hidden it will update that row when actually I need the visible cell to update....so I am stuck - below is a small chunk of the code used to update the database.
Private Sub Worksheet_Change(ByVal Target As Range)
Refreshbuttons
Dim KeyCells As Range
Dim aCell As Range
Const TARGET_DB = "MKT DB1.accdb"
Dim VErrors(4) As String
VErrors(0) = "Y"
VErrors(1) = "YES"
VErrors(2) = "1"
VErrors(3) = "TRUE"
Dim NVErrors(5) As String
NVErrors(0) = "N"
NVErrors(1) = "NO"
NVErrors(2) = ""
NVErrors(3) = "0"
NVErrors(4) = "FALSE"
Set srch = Range("A4:Z4").Find("PROJECTID", , xlValues, xlWhole)
PRO = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("PROJECTDES", , xlValues, xlWhole)
PD = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("ECAT", , xlValues, xlWhole)
EC = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("SALEMODEL", , xlValues, xlWhole)
SM = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("MKDBROSOURCE", , xlValues, xlWhole)
MDR = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("SOLREVIEWED", , xlValues, xlWhole)
SRD = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("DBSUPPORTEDDUEDATE", , xlValues, xlWhole)
DSDD = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("CATEGORY", , xlValues, xlWhole)
CT = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("COMPLETE", , xlValues, xlWhole)
CMP = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("STYLECOUNT", , xlValues, xlWhole)
SC = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("ECATREADY", , xlValues, xlWhole)
ECR = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("ESTHRS", , xlValues, xlWhole)
EST = Chr(srch.Column + 64)
Set srch = Range("A4:Z4").Find("ACTUALHRS", , xlValues, xlWhole)
AH = Chr(srch.Column + 64)
Set cnn = New ADODB.Connection
MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn
End With
projectCount = 0
**For Each C In Selection.Offset(-1,0).Rows
tmp = C.Address** // THIS IS WHERE MY ISSUE IS - IT LOOKS TO THE ROW ABOVE AND NOT THE VISIBLE ROW
ChangeFields = ""
ChangeValuesOld = ""
ChangeValuesNew = ""
If Range("A" & C.Row).EntireRow.Hidden = False Then
'create the recordset
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
'On Error GoTo Err1:
strSQL = "SELECT * FROM Projects WHERE Projectid = " & Range(PRO & C.Row).Value & ""
rst.Open Source:=strSQL, _
ActiveConnection:=cnn
If rst.EOF = False Then
'Start = GetTickCount()
If rst("Projectid") <> Range(PRO & C.Row).Value Or (IsNull(rst("Projectid")) And Range(PRO & C.Row).Value <> "") Then
If IsNull(rst("projectid")) Then
ChangeValuesOld = ChangeValuesOld & "NULL "
Else
ChangeValuesOld = ChangeValuesOld & rst("projectid") & " "
End If
If IsEmpty(Range(PRO & C.Row).Value) Then
ChangeValuesNew = ChangeValuesNew & "NULL "
Else
ChangeValuesNew = ChangeValuesNew & Range(PRO & C.Row).Value & " "
End If
ChangeFields = ChangeFields & "PROJECTID "
End If
If rst("ProjectDes") <> Range(PD & C.Row).Value Or (IsNull(rst("ProjectDes")) And Range(PD & C.Row).Value <> "") Then
If IsNull(rst("ProjectDes")) Then
ChangeValuesOld = ChangeValuesOld & "NULL "
Else
ChangeValuesOld = ChangeValuesOld & rst("ProjectDes") & " "
End If
If IsEmpty(Range(PD & C.Row).Value) Then
ChangeValuesNew = ChangeValuesNew & "NULL "
Else
ChangeValuesNew = ChangeValuesNew & Range(PD & C.Row).Value & " "
End If
ChangeFields = ChangeFields & "ProjectDes "
End If
If rst("ECAT") <> Range(EC & C.Row).Value Or (IsNull(rst("ECAT")) And Range(EC & C.Row).Value <> "") Then
If IsNull(rst("ECAT")) Then
ChangeValuesOld = ChangeValuesOld & "NULL "
Else
ChangeValuesOld = ChangeValuesOld & rst("ECAT") & " "
End If
If IsEmpty(Range(EC & C.Row).Value) Then
ChangeValuesNew = ChangeValuesNew & "NULL "
Else
ChangeValuesNew = ChangeValuesNew & Range(EC & C.Row).Value & " "
End If
ChangeFields = ChangeFields & "ECAT "
Any help is GREATLY appreciated - thank you