List folders in directory, with update function - vba

Im trying to get a list of all folders in a directory. and have a button that enables an update, on the list, without re-creating it every time. So only listing new folder that are not already in the excel sheet.
This is the code that I have working. But I would like it to be able to search the sheet if the folder is already there, if it is then skip it, if not the add it. Once the update it completed the filter by name in column C
Sub folder_names_including_subfolder()
Application.ScreenUpdating = False
Dim fldpath
Dim fso As Object, j As Long, folder1 As Object
If ActiveSheet.Name = "test" Then
fldpath = "Z:\\"
ElseIf ActiveSheet.Name = "test1" Then
fldpath = "Y:\\"
End If
Cells(3, 1).Value = fldpath
Cells(4, 1).Value = "Path"
Cells(4, 2).Value = "Dir"
Cells(4, 3).Value = "Name"
Cells(4, 4).Value = "Folder Size"
Cells(4, 5).Value = "Date Created"
Cells(4, 6).Value = "Date Last Modified"
Cells(4, 7).Value = "Codec"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder1 = fso.GetFolder(fldpath)
get_sub_folder folder1
Set fso = Nothing
Range("A3").Font.Size = 9
ActiveWindow.DisplayGridlines = False
Range("A3:G" & Range("A4").End(xlDown).Row).Font.Size = 9
Range("A4:G4").Interior.Color = vbCyan
Application.ScreenUpdating = True
End Sub
Sub get_sub_folder(ByRef prntfld As Object)
Dim SubFolder As Object, subfld As Object, j As Long
For Each SubFolder In prntfld.SubFolders
j = Range("A3").End(xlDown).Row + 1
Cells(j, 1).Value = SubFolder.Path
Cells(j, 2).Value = Left(SubFolder.Path, InStrRev(SubFolder.Path, "\"))
Cells(j, 3).Value = SubFolder.Name
Cells(j, 4).Value = Application.WorksheetFunction.RoundDown((((SubFolder.Size / 1024) / 1024) / 1024), 2) & " " & "GB"
Cells(j, 5).Value = SubFolder.DateCreated
Cells(j, 6).Value = SubFolder.DateLastModified
With Cells(j, 7).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Sheet3!$A$1:$A$5"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Next SubFolder
For Each subfld In prntfld.SubFolders
get_sub_folder subfld
Next subfld
Columns("C:F").AutoFit
Columns("G").ColumnWidth = 10
End Sub

Just test before storing the material:
For Each SubFolder In prntfld.SubFolders
checkit = SubFolder.Name
If Application.WorksheetFunction.CountIf(Range("C:C"), checkit) = 0 Then
j = Range("A3").End(xlDown).Row + 1
Cells(j, 1).Value = SubFolder.Path
Cells(j, 2).Value = Left(SubFolder.Path, InStrRev(SubFolder.Path, "\"))
Cells(j, 3).Value = SubFolder.Name
Cells(j, 4).Value = Application.WorksheetFunction.RoundDown((((SubFolder.Size / 1024) / 1024) / 1024), 2) & " " & "GB"
Cells(j, 5).Value = SubFolder.DateCreated
Cells(j, 6).Value = SubFolder.DateLastModified
With Cells(j, 7).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Sheet3!$A$1:$A$5"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
Next SubFolder

Related

Automatic Changing of values in excel

Data after closig and opening of excelI am having a tricky situation with my excel. I have written a VBA code which collects HEX data from many CSV file and converts it into decimal and stores it into decimal. This works perfectly when tested I had no problem, I had also saved it. But when I reopen the excel half the cells are back to Hex format and only half are in decimal. I don't know why this happens. When doing it for first time it works but on saving closing it and again opening it gives me this problem
Here is the vba code
OriginalData
Sub Sample()
Dim myfiles
Dim i As Integer
Dim J As Long
Dim l As Long
Dim LastRow As Long
myfiles = Application.GetOpenFilename(filefilter:="CSV Files (*.csv), *.csv", MultiSelect:=True)
If Not IsEmpty(myfiles) Then
For i = LBound(myfiles) To UBound(myfiles)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & myfiles(i), Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1, 0))
.Name = "Sample"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 3
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Next i
LR = Range("A" & Rows.Count).End(xlUp).Row
For J = 2 To LR
Cells(J, 4).Value = CLng("&H" & Mid(Cells(J, 4).Value, 4, Len(Cells(J, 4).Value)))
Cells(J, 5).Value = CLng("&H" & Mid(Cells(J, 5).Value, 8, Len(Cells(J, 5).Value)))
Cells(J, 6).Value = CLng("&H" & Mid(Cells(J, 6).Value, 8, Len(Cells(J, 6).Value)))
Cells(J, 7).Value = CLng("&H" & Mid(Cells(J, 7).Value, 8, Len(Cells(J, 7).Value)))
Cells(J, 8).Value = CLng("&H" & Mid(Cells(J, 8).Value, 4, Len(Cells(J, 8).Value)))
Cells(J, 9).Value = CLng("&H" & Mid(Cells(J, 9).Value, 8, Len(Cells(J, 9).Value)))
Next
LastRow = Range("C" & Rows.Count).End(xlUp).Row
For l = 2 To LastRow
'Cells(l, 14).Value = Left(Cells(l, 3).Value, 3)
'Cells(l, 13).Value = Right(Range(l, 3).Value, 4)
'(l, 12).Value = Val(Left(Right(Cells(l, 3).Value, 7), 2))
Cells(l, 10).Value = Left(Cells(l, 3).Value, 3) + Val(Left(Right(Cells(l, 3).Value, 7), 2)) / 60 + Right(Cells(l, 3).Value, 4) / 3600
Next
Else
MsgBox "No File Selected"
End If
End Sub
I parse more than one CSV files at the same time so when on reopening only the parse of the first file remains in decimal format others change to original hex format

Set Focus to formField1

I am in need of a little help. I creater a UserForm and am attempting to get the form (on one worksheet) to write to another worksheet. I feel like I am getting close but I keep getting the following error:
I keep getting the Run-time 2110 Error in excel.
Beyond that I am trying to get the data in my userform to post to a worksheet titled Hourly CI Data.
The following code is from my UserForm object:
Private Sub cmdbtnCancel_Click()
' Clear data fields and reset the form
Me.formField1.Value = ""
Me.formField2.Value = ""
Me.formField3.Value = ""
Me.formField4.Value = ""
Me.formField5.Value = ""
Me.formField6.Value = ""
Me.formField7.Value = ""
Me.formField8.Value = ""
Me.formField9.Value = ""
Me.formField10.Value = ""
Me.formField11.Value = ""
Me.formField1.SetFocus
Unload Me
End Sub
Sub cmdbtnSave_Click()
Dim iRow As Long
Dim ws As Worksheet
Unload Me
Set ws = Worksheets("Hourly Slot CI - Data")
' Find the next empty row
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
' Input the data in the Data Table
ws.Cells(iRow, 1).Value = Me.formField1.Value
ws.Cells(iRow, 2).Value = Me.formField2.Value
ws.Cells(iRow, 3).Value = Me.formField3.Value
ws.Cells(iRow, 4).Value = Me.formField4.Value
ws.Cells(iRow, 5).Value = Me.formField5.Value
ws.Cells(iRow, 6).Value = Me.formField6.Value
ws.Cells(iRow, 7).Value = Me.formField7.Value
ws.Cells(iRow, 8).Value = Me.formField8.Value
ws.Cells(iRow, 9).Value = Me.formField9.Value
ws.Cells(iRow, 10).Value = Me.formField10.Value
ws.Cells(iRow, 11).Value = Me.formField11.Value
ws.Cells(iRow, 12).Value = Me.formField12.Value
ws.Cells(iRow, 13).Value = Me.formField13.Value
ws.Cells(iRow, 14).Value = Me.formField14.Value
ws.Cells(iRow, 15).Value = Me.formField15.Value
ws.Cells(iRow, 16).Value = Me.formField16.Value
ws.Cells(iRow, 17).Value = Me.formField17.Value
ws.Cells(iRow, 18).Value = Me.formField18.Value
ws.Cells(iRow, 19).Value = Me.formField19.Value
ws.Cells(iRow, 20).Value = Me.formField20.Value
ws.Cells(iRow, 21).Value = Me.formField21.Value
ws.Cells(iRow, 22).Value = Me.formField22.Value
ws.Cells(iRow, 23).Value = Me.formField23.Value
ws.Cells(iRow, 24).Value = Me.formField24.Value
ws.Cells(iRow, 25).Value = Me.formField25.Value
ws.Cells(iRow, 26).Value = Me.formField26.Value
ws.Cells(iRow, 27).Value = Me.formField27.Value
ws.Cells(iRow, 28).Value = Me.formField28.Value
ws.Cells(iRow, 29).Value = Me.formField29.Value
ws.Cells(iRow, 30).Value = Me.formField30.Value
ws.Cells(iRow, 31).Value = Me.formField31.Value
ws.Cells(iRow, 1).Activate
' Clear all fields and reset the form
Me.formField1.Value = ""
Me.formField2.Value = ""
Me.formField3.Value = ""
Me.formField4.Value = ""
Me.formField5.Value = ""
Me.formField6.Value = ""
Me.formField7.Value = ""
Me.formField8.Value = ""
Me.formField9.Value = ""
Me.formField10.Value = ""
Me.formField11.Value = ""
Me.formField12.Value = ""
Me.formField13.Value = ""
Me.formField14.Value = ""
Me.formField15.Value = ""
Me.formField16.Value = ""
Me.formField17.Value = ""
Me.formField18.Value = ""
Me.formField19.Value = ""
Me.formField20.Value = ""
Me.formField21.Value = ""
Me.formField22.Value = ""
Me.formField23.Value = ""
Me.formField24.Value = ""
Me.formField25.Value = ""
Me.formField26.Value = ""
Me.formField27.Value = ""
Me.formField28.Value = ""
Me.formField29.Value = ""
Me.formField30.Value = ""
Me.formField31.Value = ""
Me.formField1.SetFocus
End Sub
I am unsure on exactly how to set this focus correctly so any input is helpful. Thank you in advance!
You're unloading the form before you actually try to set the values. You need to unload the form after you're completely done with it at the very end. In your case, on the CmdbtnSave_Click sub, you'd want to put it right before End Sub, and make sure you remove it towards the beginning.
EDIT: Also, if you're unloading the form, you shouldn't need to clear out the different formfields. Unloading will remove it(and the formfield values) from the computer's memory.

VBA Userform Find function display records

I'm in the process of making a userform. I have managed to setup a find function using the code below which then also loops and counts the number of cases in the spreadsheet.
I have also created a function to find the next item which is operated by a separate command button but it does not display the records in the userform so it can be amended.
Does anyone have any ideas on how to fix this?
Private Sub FindNext_Click()
Cells.FindNext(After:=ActiveCell).Activate
End Sub
Private Sub Find_Click()
Worksheets("Master").Activate
Dim strFind As String
Dim FirstAddress As String
Dim rSearch As Range
Set rSearch = Range("a1", Range("e65536").End(xlUp))
Dim f As Integer
strFind = Me.TextBox1.Value
With rSearch
Set c = .Find(strFind, LookIn:=xlValues)
If Not c Is Nothing Then
c.Select
With Me
.TextBox2.Value = c.Offset(0, 1).Value
.TextBox3.Value = c.Offset(0, 2).Value
.TextBox4.Value = c.Offset(0, 3).Value
.TextBox5.Value = c.Offset(0, 4).Value
.TextBox6.Value = c.Offset(0, 5).Value
.TextBox7.Value = c.Offset(0, 6).Value
.TextBox8.Value = c.Offset(0, 7).Value
.TextBox9.Value = c.Offset(0, 8).Value
.TextBox10.Value = c.Offset(0, 9).Value
.TextBox11.Value = c.Offset(0, 10).Value
.TextBox12.Value = c.Offset(0, 11).Value
.TextBox13.Value = c.Offset(0, 12).Value
.TextBox14.Value = c.Offset(0, 13).Value
.TextBox20.Value = c.Offset(0, 14).Value
.TextBox21.Value = c.Offset(0, 15).Value
.TextBox15.Value = c.Offset(0, 16).Value
.TextBox22.Value = c.Offset(0, 17).Value
.TextBox16.Value = c.Offset(0, 18).Value
.TextBox18.Value = c.Offset(0, 19).Value
.TextBox19.Value = c.Offset(0, 20).Value
.update.Enabled = True
.Add.Enabled = False
f = 0
End With
FirstAddress = c.Address
Do
f = f + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
If f > 1 Then
Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
Case vbCancel
End Select
Me.Height = frmMax
End If
Else: MsgBox strFind & " not listed"
End If
End With
End Sub
Thanks
You need to encapsulate the update code into its own method (sub) then you can call it for both Find and Find Next. Like:
Private Sub FindNext_Click()
Dim nextCell As Range
Set nextCell = Cells.FindNext(After:=ActiveCell)
'FindNext loops round to the initial cell if it finds no other so we test for it
If Not nextCell.Address(external:=true) = ActiveCell.Address(external:=true) Then
updateFields anchorCell:=nextCell
End If
End Sub
Private Sub Find_Click()
Worksheets("Master").Activate
Dim strFind As String
Dim FirstAddress As String
Dim rSearch As Range
Set rSearch = Range("a1", Range("e65536").End(xlUp))
Dim f As Integer
strFind = Me.TextBox1.Value
With rSearch
Set c = .Find(strFind, LookIn:=xlValues)
If Not c Is Nothing Then
updateFields anchorCell:=c
FirstAddress = c.Address
Do
f = f + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
If f > 1 Then
Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
Case vbCancel
End Select
Me.Height = frmMax
End If
Else: MsgBox strFind & " not listed"
End If
End With
End Sub
Private Sub updateFields(anchorCell As Range)
anchorCell.Select
With Me
.TextBox2.Value = anchorCell.Offset(0, 1).Value
.TextBox3.Value = anchorCell.Offset(0, 2).Value
.TextBox4.Value = anchorCell.Offset(0, 3).Value
.TextBox5.Value = anchorCell.Offset(0, 4).Value
.TextBox6.Value = anchorCell.Offset(0, 5).Value
.TextBox7.Value = anchorCell.Offset(0, 6).Value
.TextBox8.Value = anchorCell.Offset(0, 7).Value
.TextBox9.Value = anchorCell.Offset(0, 8).Value
.TextBox10.Value = anchorCell.Offset(0, 9).Value
.TextBox11.Value = anchorCell.Offset(0, 10).Value
.TextBox12.Value = anchorCell.Offset(0, 11).Value
.TextBox13.Value = anchorCell.Offset(0, 12).Value
.TextBox14.Value = anchorCell.Offset(0, 13).Value
.TextBox20.Value = anchorCell.Offset(0, 14).Value
.TextBox21.Value = anchorCell.Offset(0, 15).Value
.TextBox15.Value = anchorCell.Offset(0, 16).Value
.TextBox22.Value = anchorCell.Offset(0, 17).Value
.TextBox16.Value = anchorCell.Offset(0, 18).Value
.TextBox18.Value = anchorCell.Offset(0, 19).Value
.TextBox19.Value = anchorCell.Offset(0, 20).Value
.Update.Enabled = True
.Add.Enabled = False
End With
End Sub

Listbox - Run-time 380 error invalid property value

Afternoon
I'm a mere novice of an amateur in the world of VB.
I'm currently creating a userform in Excel and to search for records I decided to use a listbox option to allow a user to scroll through the search results.
However, I've encountered a run-time 380 error invalid property value due to the listbox exceeding ten entries.
I have managed to find a solution using rowsource command but I can't find how to use it in my code. Any advice is welcome and if anyone can think of a better way I would be grateful.
`enter code here
Dim MyData As Range
Dim c As Range
Dim rFound As Range
Dim r As Long
Dim rng As Range
Const frmMax As Long = 640
Const frmHt As Long = 210
Const frmWidth As Long = 280
Dim sFileName As String
Dim oCtrl As MSForms.Control
Private Sub Add_Click()
Set c = Range("a65536").End(xlUp).Offset(1, 0)
Application.ScreenUpdating = False
With Me
c.Value = .TextBox1.Value
c.Offset(0, 1).Value = .TextBox2.Value
c.Offset(0, 2).Value = .TextBox3.Value
c.Offset(0, 3).Value = .TextBox4.Value
c.Offset(0, 4).Value = .TextBox5.Value
c.Offset(0, 5).Value = .TextBox6.Value
c.Offset(0, 6).Value = .TextBox7.Value
c.Offset(0, 7).Value = .TextBox8.Value
c.Offset(0, 8).Value = .TextBox9.Value
c.Offset(0, 9).Value = .TextBox10.Value
c.Offset(0, 10).Value = .TextBox11.Value
ClearControls
End With
Application.ScreenUpdating = True
End Sub
Private Sub Find_Click()
Worksheets("Master").Activate
Dim strFind As String
Dim FirstAddress As String
Dim rSearch As Range
Set rSearch = Range("a1", Range("e65536").End(xlUp))
Dim f As Integer
strFind = Me.TextBox1.Value
With rSearch
Set c = .Find(strFind, LookIn:=xlValues)
If Not c Is Nothing Then
c.Select
With Me
.TextBox2.Value = c.Offset(0, 1).Value
.TextBox3.Value = c.Offset(0, 2).Value
.TextBox4.Value = c.Offset(0, 3).Value
.TextBox5.Value = c.Offset(0, 4).Value
.TextBox6.Value = c.Offset(0, 5).Value
.TextBox7.Value = c.Offset(0, 6).Value
.TextBox8.Value = c.Offset(0, 7).Value
.TextBox9.Value = c.Offset(0, 8).Value
.TextBox10.Value = c.Offset(0, 9).Value
.update.Enabled = True
.Add.Enabled = False
f = 0
End With
FirstAddress = c.Address
Do
f = f + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
If f > 1 Then
Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
FindAll
Case vbCancel
End Select
Me.Height = frmMax
End If
Else: MsgBox strFind & " not listed"
End If
End With
If Sheet2.AutoFilterMode Then Sheet2.Range("A8").AutoFilter
End Sub
Private Sub TextBox11_Change()
End Sub
Private Sub update_Click()
Application.ScreenUpdating = False
If rng Is Nothing Then GoTo skip
For Each c In rng
If r = 0 Then c.Select
r = r - 1
Next c
skip:
Set c = ActiveCell
c.Value = Me.TextBox1.Value
c.Offset(0, 1).Value = Me.TextBox2.Value
c.Offset(0, 2).Value = Me.TextBox3.Value
c.Offset(0, 3).Value = Me.TextBox4.Value
c.Offset(0, 4).Value = Me.TextBox5.Value
c.Offset(0, 5).Value = Me.TextBox6.Value
c.Offset(0, 6).Value = Me.TextBox7.Value
c.Offset(0, 7).Value = Me.TextBox8.Value
c.Offset(0, 8).Value = Me.TextBox9.Value
c.Offset(0, 9).Value = Me.TextBox10.Value
c.Offset(0, 10).Value = Me.TextBox11.Value
With Me
.update.Enabled = False
.Add.Enabled = True
ClearControls
End With
If Sheet1.AutoFilterMode Then Sheet1.Range("A8").AutoFilter
Application.ScreenUpdating = True
On Error GoTo 0
End Sub
Sub FindAll()
Worksheets("Master").Activate
Dim strFind As String
Dim rFilter As Range
Set rFilter = Sheet2.Range("a1", Range("Z65536").End(xlUp))
Set rng = Sheet2.Range("a1", Range("a65536").End(xlUp))
strFind = Me.TextBox1.Value
With Sheet2
If Not .AutoFilterMode Then .Range("A2").AutoFilter
rFilter.AutoFilter Field:=1, Criteria1:=strFind
Set rng = rng.Cells.SpecialCells(xlCellTypeVisible)
Me.ListBox1.Clear
For Each c In rng
With Me.ListBox1
.AddItem c.Value
.List(.ListCount - 1, 1) = c.Offset(0, 1).Value
.List(.ListCount - 1, 2) = c.Offset(0, 2).Value
.List(.ListCount - 1, 3) = c.Offset(0, 3).Value
.List(.ListCount - 1, 4) = c.Offset(0, 4).Value
.List(.ListCount - 1, 5) = c.Offset(0, 5).Value
.List(.ListCount - 1, 6) = c.Offset(0, 6).Value
.List(.ListCount - 1, 7) = c.Offset(0, 7).Value
.List(.ListCount - 1, 8) = c.Offset(0, 8).Value
.List(.ListCount - 1, 9) = c.Offset(0, 9).Value
.List(.ListCount - 1, 10) = c.Offset(0, 10).Value
End With
Next c
End With
End Sub
Private Sub ListBox1_Click()
If Me.ListBox1.ListIndex = -1 Then 'not selected
MsgBox " No selection made"
ElseIf Me.ListBox1.ListIndex >= 1 Then 'User has selected
r = Me.ListBox1.ListIndex
With Me
.TextBox1.Value = ListBox1.List(r, 0)
.TextBox2.Value = ListBox1.List(r, 1)
.TextBox3.Value = ListBox1.List(r, 2)
.TextBox4.Value = ListBox1.List(r, 3)
.TextBox5.Value = ListBox1.List(r, 4)
.TextBox6.Value = ListBox1.List(r, 5)
.TextBox7.Value = ListBox1.List(r, 6)
.TextBox8.Value = ListBox1.List(r, 7)
.TextBox9.Value = ListBox1.List(r, 8)
.TextBox10.Value = ListBox1.List(r, 9)
.update.Enabled = True 'allow amendment or
.Add.Enabled = False 'don't want duplicate
End With
End If
End Sub
Sub ClearControls()
With Me
For Each oCtrl In .Controls
Select Case TypeName(oCtrl)
Case "TextBox": oCtrl.Value = Empty
Case "OptionButton": oCtrl.Value = False
End Select
Next oCtrl
End With
End Sub
Private Sub UserForm_Click()
End Sub
You might take a look at the ListView Control (Right-click on the toolbox and search for additional controls, look for Microsoft ListView Control, version 6.0).
Not being the most modern and polished, it may still be very fitting for your immediate needs.
Some sample might look like this:
You build the columns by adding the ColumnHeaders first. Then you add ListItems (=first column) which also each allocates a set óf SubItems (=2nd to last column, index from 1).
Dim l As ListItem
With Me.ListView1
.FullRowSelect = True
.LabelEdit = lvwManual
.View = lvwReport
For i = 1 To 11
.ColumnHeaders.Add , , CStr(i)
Next
.HideColumnHeaders = False
Set l = .ListItems.Add(, , c.Text)
For i = 1 To 10
l.SubItems(i) = c.Offset(0, i).Text
Next
End With

Runtime error when trying to format cells in a range

I am having a lot of trouble with my vba code. I have created a sub that will execute when the file is opened. However, I keep getting a runtime error that says I have an application defined or object-defined problem. This code works when the "With Cells(13, dateFinder.Column)" block is not accompanied by the "With Cells(17, dateFinder.Column)" block below it.
Here is the code:
Sub equityRaiseOpen()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Worksheets("PRESENTATION")
Set ws2 = Worksheets(7)
ws2.Activate
Dim dateRange As Range
ws2.Unprotect
Dim dte As Date
'this block finds current quarter ending date
If Application.WorksheetFunction.RoundUp(Month(Now()) / 3, 0) = 1 Then
dte = DateValue("3/31/" & Year(Now()))
ElseIf Application.WorksheetFunction.RoundUp(Month(Now()) / 3, 0) = 2 Then
dte = DateValue("6/30/" & Year(Now()))
ElseIf Application.WorksheetFunction.RoundUp(Month(Now()) / 3, 0) = 3 Then
dte = DateValue("9/30/" & Year(Now()))
ElseIf Application.WorksheetFunction.RoundUp(Month(Now()) / 3, 0) = 4 Then
dte = DateValue("12/31/" & Year(Now()))
End If
Set dateRange = Range("FFO___AFFO_SUMMARY")
Dim iterator As Range
Dim colNum As Integer
'this block finds the column number for the current quarter ending date
For Each iterator In dateRange
If iterator = dte Then
colNum = iterator.Column
End If
Next
ws2.Range("B1") = dte
Dim dateFinder As Range
Set dateFinder = ws2.Range("B1")
Dim i As Integer
'Call putDates
i = 9
'ws2.Unprotect
Dim j As Integer
Set dateFinder = ws2.Range("B1")
For j = 1 To i
Cells(12, dateFinder.Column) = 0.4
'create data validiation for debt source
Debug.Print Cells(13, dateFinder.Column).Address
With Cells(13, dateFinder.Column)
.Interior.Color = RGB(255, 255, 255)
.Locked = False
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
With .Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=DebtList"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
.Value = "Revolver"
End With
With Cells(17, dateFinder.Column)
.Interior.Color = RGB(255, 255, 255)
.Locked = False
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
With .Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="ATM,Common, Preferred"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
.Value = "ATM"
End With
Set dateFinder = dateFinder.Offset(0, 2)
Next j
'ws2.Protect
End Sub