I am using the below code to cleanup a bunch of workbooks. I came across a workbook that does not have any data inputted below row 1. Row 1 contains the headers. The macro keeps throwing the generic error "Run-time '1004': Application-defined or object-defined error". Clicking the debug issue reveals that the issue is related to the following line
I think this must be relating to the sheet not having any data below row 1. Any ideas what I can do to remedy this error? I am clearly a novice when it comes to VBA!
Code:
NxtRw = .Range("E1").End(xlDown).Offset(1).Row
I think this must be relating to the sheet not having any data below row 1. Any ideas what I can do to remedy this error? I am clearly a novice when it comes to VBA!
Code:
Sub UpdateHeadersAspirationalInstitutional(Ws As Worksheet)
Dim Hdrs() As Variant
Dim NxtRw As Long
Dim Cnt As Long
Dim LastRow As Long
LastRow = Range("C" & Rows.Count).End(xlUp).Row
Hdrs = Array("ANULZ_RVNU_AMT", "CALC_BPS_AMT", "EST_MNDT_USD_AMT", "BTQ_NM" _
, "MJR_INV_VHCL_NM", "EST_FDNG_QTR_NM", "SLS_PSN_AVG_BPS_AMT", "STAT_UPDT_TXT", "SRCE_NM", "TM_MBR_NM") 'Sets column names for Aspirational tab. Need to adjust this if a new column is added or removed.
With Ws
.Application.ScreenUpdating = False
.Unprotect 'Unprotects the workbook
.Range("A1").Resize(5, CInt(UBound(Hdrs)) + 1).Delete Shift:=xlUp
For Cnt = 0 To UBound(Hdrs)
.Cells(1, Cnt + 1) = Hdrs(Cnt)
Next Cnt
.Columns(UBound(Hdrs) + 2).Resize(, 16383 - UBound(Hdrs)).Clear
NxtRw = .Range("E1").End(xlDown).Offset(1).Row
.Range("A" & NxtRw, "A" & Rows.Count).EntireRow.Clear
.Range("I2:I" & LastRow).Formula = _
"=MID(CELL(""filename"",A1),FIND(""]"",CELL(""filename"",A1))+1,256)" 'Adds SRCE_NM column based on the workbook name since this is not included as a column name in this file.
.Range("J2:J" & LastRow).Formula = "=CRM!$S$5" 'Adds TM_MBR_NM column based on the TM_MBR_NM from the CRM tab
End With
End Sub