Objective is to copy one workbook to a new workbook, then format the new workbook to "blue medium 2" format.
I get
wrong number of arguments or invalid property assignment
on
ActiveSheet.Cells(1, i).Style = "Medium 2"
Sub CopySheetAndEliminateColumns()
Dim wsCopy As Worksheet
Dim wsNew As Worksheet
Dim ListObj As ListObject
'Set the worksheet to be copied
Set wsCopy = ThisWorkbook.Sheets("AnalysisRound1")
'Create a new workbook and copy the worksheet
Set wsNew = Workbooks.Add(xlWBATWorksheet).Sheets(1)
wsCopy.Cells.Copy
Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Delete Columns with not needed data
Range("F:I,K:L,N:N,P:P").Delete
'Autofit the columns in the new worksheet
wsNew.Cells.EntireColumn.AutoFit
'Borders around active worksheet
Application.ScreenUpdating = False
Dim lngLstCol As Long, lngLstRow As Long
lngLstRow = ActiveSheet.UsedRange.Rows.Count
lngLstCol = ActiveSheet.UsedRange.Columns.Count
For Each rngCell In Range("A2:A" & lngLstRow)
If rngCell.value > "" Then
r = rngCell.Row
c = rngCell.Column
Range(Cells(r, c), Cells(r, lngLstCol)).Select
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
Next
' Code to format new workbook sheet to blue, Medium 2
Dim LastCol As Long
LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
' Set the cell style to Medium 2 for all data in columns, including the headers
Dim i As Long
For i = 1 To LastCol
ActiveSheet.Columns(i).Style = "Medium 2"
'Code bombs on the line below - wrong number of arguments or invalid property assignment -
ActiveSheet.Cells(1, i).Style = "Medium 2"
Next i
Application.ScreenUpdating = True
End Sub
- Changed the variable
Dim LastCol As Longto use the code aboveDim lngLstCol = Activesheet - Moved the code to below
Application.ScreenUpdating = True - The following script turned my original worksheet into Medium 2
Dim w As Worksheet
Dim r As Range
For Each w In Worksheets
If w.ListObjects.Count < 1 Then
Set r = w.Range("A1").CurrentRegion
r.Interior.ColorIndex = xlColorIndexNone
w.ListObjects.Add(Source:=r).Name = w.Name
End If
Next w
Assumption:
Step:
Codes: