This is amazing! I didn't realize that there was an index/match function within VBA as well. However, I realized that you had to reformat the input sheets from two sets of columns to one column for the VBA to work. I am told I need to have the cells on the input worksheets in a particular format (for printability and readability). I've been trying to use VBA to rearrange cells before your VBA starts into a single column and rearrange the cells back after your VBA finishes, but it doesn't seem to work. When editing cells in the second pair of columns, it doesn't update the database. Do you have any advice my particular problem? Maybe I just fundamentally don't understand something about the VBA.
So when someone inputs something into cell "B6" in one worksheet the change will reflect into cell "C1" in
'another worksheet and vice versa
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim wsCount As Integer
Dim i As Integer
Dim wksData As Worksheet
Dim iRow As Long, iCol As Long
Dim sRow As String, sCol As String
Dim r As Range
'exit is more than one cell is changed
If Target.Count > 1 Then Exit Sub
Set wksData = Worksheets("Database")
'This is my attempt at trying to move cells over into one column
If Not Intersect(Target, Range("G6:H13")) Is Nothing Then
For i = 1 To 8
Application.EnableEvents = False
Cells((15 + i), 1).Value = Cells((5 + i), 7).Value
Cells((15 + i), 2).Value = Cells((5 + i), 8).Value
Application.EnableEvents = True
Next i
End If
Set r = wksData.Range("B1").CurrentRegion
If Sh Is wksData Then
'if not in block of headers and row name then get out
If Intersect(r, Target) Is Nothing Then Exit Sub
sRow = Intersect(Target.EntireRow, r.Columns(1)).Value
sCol = Intersect(Target.EntireColumn, r.Rows(1)).Value
iRow = 0
On Error Resume Next
iRow = Application.WorksheetFunction.Match(sRow, Worksheets(sCol).Columns(1), 0)
On Error GoTo 0
On Error GoTo NiceExit
If iRow = 0 Then
MsgBox sRow & " not found on sheet " & sCol
Err.Raise 10000, sRow & " not found on sheet " & sCol
Else
Application.EnableEvents = False
Worksheets(sCol).Cells(iRow, 2).Value = Target.Value
Application.EnableEvents = True
End If
On Error GoTo 0
Else
If Intersect(Sh.Columns(2), Target) Is Nothing Then Exit Sub
sRow = Target.Offset(0, -1).Value
iRow = 0
iCol = 0
On Error Resume Next
iRow = Application.WorksheetFunction.Match(sRow, r.Columns(1), 0)
iCol = Application.WorksheetFunction.Match(Sh.Name, r.Rows(1), 0)
On Error GoTo 0
On Error GoTo NiceExit
If iRow = 0 Then
MsgBox sRow & " not found on sheet " & wksData.Name
Err.Raise 10000, sRow & " not found on sheet " & wksData.Name
ElseIf iCol = 0 Then
MsgBox sCol & " not found on sheet " & wksData.Name
Err.Raise 10000, sCol & " not found on sheet " & wksData.Name
Else
Application.EnableEvents = False
r.Cells(iRow, iCol).Value = Target.Value
Application.EnableEvents = True
End If
On Error GoTo 0
End If
NiceExit:
Err.Clear
Application.EnableEvents = True
End Sub