I am trying to execute quite a simple command in VBA (assigning a value to a range) and am getting an error "application defined or web-defined error". After some troubleshooting I think it has to do with the definition of my worksheet. Here is my code:
Sub CommandButton1_Click()
Dim arr() As String
Dim path As String
Dim filename As String
Dim w1 As Workbook
Dim w2 As Workbook
Set w1 = ThisWorkbook
Dim rng As Range
'w1.Worksheets(1).Range(Cells(1, 13), Cells(1, 22)) = 1 **#works perfectly, populates the given cells with 1's**
path = "C:\Users\Nenko\Desktop\"
filename = "D1.csv"
Workbooks.Open filename:=path & filename
ActiveSheet.Columns("A:B").EntireColumn.Delete
ActiveSheet.Columns("F:AQ").EntireColumn.Delete
ActiveSheet.Columns("G:G").EntireColumn.Delete
ActiveSheet.Columns("H:H").EntireColumn.Delete
ActiveSheet.Columns("I:J").EntireColumn.Delete
ActiveSheet.Columns("J:J").EntireColumn.Delete
ActiveSheet.Columns("K:P").EntireColumn.Delete
'Set w2 = Workbooks(path & filename) #subscript out of range
'Set w2 = ThisWorkbook **# application defined or web-defined error in Sheets.Add.Name=arr(a)**
Set w2 = ActiveWorkbook
'Set Range = w2.Worksheets(1).Range(Cells(1, 13), Cells(1, 22)) **# argument not optional**
'Range = 1
'w2.Worksheets(1).Range(Cells(1, 13), Cells(1, 22)) = 1 **#application defined or web-defined error**
EndRow = Sheets(1).Cells(Sheets(1).Rows.Count, "C").End(xlUp).Row
'w2.Worksheets(1).Cells(1, 11) = w2.Worksheets(1).Cells(1, 1).Value **#does nothing**
ActiveSheet.Range("A2:A65536").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ActiveSheet.Range("K:K"), Unique:=True
EndRow2 = Sheets(1).Cells(Sheets(1).Rows.Count, "K").End(xlUp).Row - 1
ReDim arr(EndRow2)
For a = 2 To EndRow2 + 1
arr(a - 1) = w2.Worksheets(1).Cells(a, 11).Value
Next
ActiveSheet.Columns("K:K").EntireColumn.Delete
For a = 1 To EndRow2
Sheets.Add.Name = arr(a)
c = 1
d = 1
For b = 1 To EndRow
If w2.Worksheets(a + 1).Cells(b, 1) = arr(a) Then
w2.Worksheets(1).Rows(c).EntireRow.Value = w2.Worksheets(a + 1).Rows(b).EntireRow.Value
c = c + 1
End If
'If w2.Worksheets(a + 1).Cells(b, 2) = arr(a) Then
'w2.Worksheets(1).Range(Cells(d, 13), Cells(d, 22)) = w2.Worksheets(a + 1).Range(Cells(b, 1), Cells(b, 10))
'd = d + 1
'End If **#this is what I tried to do initially and started getting the error**
Next
Next
End Sub
End Sub
After the # sign I have put the errors I am getting when adding the corresponding line to the code.
What the code is doing is opening a second workbook, taking the unique values from the first column of its only sheet and creating one tab for every unique value, containing the rows corresponding to this value. It works quite fine until I try to use a Range of any form in the second sheet w2, which always returns some kind of error.
During troubleshooting I found out that the command
w1.Worksheets(1).Range(Cells(1, 13), Cells(1, 22)) = 1
populates the corresponding cells in workbook w1 with 1's. On the otherhand the command
w2.Worksheets(1).Range(Cells(1, 13), Cells(1, 22)) = 1
returns "application defined or web-defined error"
I don't understand why it works for the one workbook but not for the other. I have tried defining the second workbook in every way I know in VBA, but in each case I get some kind of error.
Does anyone have an idea what's wrong with the second workbook?