Hi, Really struggling with why I am getting the error 1004 "Table features aren't available because the sheet is protected." The code unprotects the sheet. I also manually unprotected both the Itemized Expenses and Raw Expense Data sheets and still get the error. What is so frustrating is I use the same code (editing names as needed) for transferring budget data and it works great.
Greatly appreciate any help with this.
Karen
Sub transferAccessExpenseData()
Dim dataRaw As Worksheet 'This is the Raw Expense Data sheet
Dim dataExpense As Worksheet 'This is the Itemized Expenses sheet
Dim raw_tbl As ListObject 'This is tblExpenseRaw on the Raw Expense Data sheet
Dim expense_tbl As ListObject 'This is tbl_ItemizedExpense on the Itemized Expenses sheet
Dim dataRange As Range 'This is the range of rows that are visible after filtering tblExpenseRaw
Dim getProjectCode As String 'Holds the project code for filtering tblExpenseRaw
Dim vCount As Long 'This variable holds the count of the number of visible rows in tblExpenseRaw
Dim RC As Long 'Variable for counting row numbers to format for tbl_ItemizedExpense
Dim LastRow As Long 'Variable to get last row for tbl_ItemizedExpense
Set dataRaw = Sheets("Raw Expense Data")
Set dataExpense = Sheets("Itemized Expenses")
Set raw_tbl = dataRaw.ListObjects("tblExpenseRaw")
Set expense_tbl = dataExpense.ListObjects("tbl_ItemizedExpense")
Set dataRange = Sheets("Raw Expense Data").Range("tblExpenseRaw")
Application.ScreenUpdating = False
Worksheets("Raw Expense Data").Activate
ActiveSheet.Unprotect ("gme")
'Initializing the project code variable.
getProjectCode = Sheets("Raw Expense Data").Range("C2").Value
'Filter the imported Access expense raw data by project code which is located in column 10
With ActiveSheet.ListObjects("tblExpenseRaw").Range
.AutoFilter
.AutoFilter Field:=10, Criteria1:=getProjectCode
End With
'Need to count the number of visible rows in tblExpenseRaw and store in vCount variable.
With dataRange
For Each rngArea In .SpecialCells(xlCellTypeVisible).Areas
vCount = vCount + rngArea.Rows.count
Next
End With
MsgBox "vCount (visible rows) is " & vCount
Worksheets("Itemized Expenses").Activate
ActiveSheet.Unprotect ("gme")
'Clear out any data in tbl_ItemizedExpense and then resize the table using vCount
With expense_tbl
.AutoFilter.ShowAllData
.DataBodyRange.Clear
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'ERROR BELOW
.Resize .Range.Resize(vCount + 1, .ListColumns.count)
End With
Worksheets("Raw Expense Data").Activate
'Need to copy the visible rows in tblExpenseRaw and paste in the tbl_ItemizedExpense.
'Since the range is already selected, just need to copy.
dataRange.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Worksheets("Itemized Expenses").Activate
Sheets("Itemized Expenses").Range("B6").Select
Selection.PasteSpecial xlPasteValues
Application.CutCopyMode = False
'-----------------------------------------
'------------------------------------------------
'Format rows in tbl_ItemizedExpense
'Data starts on row 6 of the Itemized Expenses sheet.
RC = 6
'Using column 2 because every record must have an Expense ID
LastRow = Cells(Rows.count, 2).End(xlUp).row
'Using the For Next to go through each row of tbl_ItemizedExpense.
'Need the -1 so the total row to excluded
For RC = 6 To LastRow - 1
'Expense ID, Expense Type, GL Code, Funding Source, Comments fields format
Range("B" & RC & ":F" & RC).Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'Unlock Expense Type, GL Code, Funding Source, Comment
Range("C" & RC & ":F" & RC).Select
With Selection
.Locked = False
End With
'Expense Amount field format
Range("G" & RC).Select
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.Locked = False
End With
Selection.NumberFormat = "#,##0.00"
'Transaction Date format
Range("H" & RC).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.Locked = False
End With
Selection.NumberFormat = "m/d/yyyy"
'Notes field format
Range("I" & RC).Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.Locked = False
End With
'Fiscal Year, Project Code, and Transaction Month
Range("J" & RC & ":L" & RC).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.Locked = True
End With
Next RC
Sheets("Itemized Expenses").Range("C6").Select
ActiveSheet.Protect ("gme")
If ActiveSheet.Protection.AllowFiltering = False Then
ActiveSheet.Protect AllowFiltering:=True
End If
Application.ScreenUpdating = True
End Sub
Bookmarks