I am having issues setting conditional formatting via VBA. I am setting it this was since when the code deletes my rows the formatting is deleted as well. The problem I have is, it does not properly set the formatting. It should be setting at follows but it mixing the two together in a sense.
Border:- If A5 is not blank it should add cell borders through T to the bottom of the entire workbook.
Gray Fill:- If A5 is not blank it should fill column Q & R to the bottom of the entire workbook.
It is difficult to provide a sample workbook, but if required I will do my best to get a mock up.
My current code is as follows:
Option Explicit
Public Sub LoadEffort()
'//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\\'
'|| Open all Excel files in a specific folder and combines data into one sheet ||'
'|| John_w (10/8/2010 ) (2007-2010 compatible) ||'
'|| http://www.mrexcel.com/forum/excel-questions/500421-macro-merge-multiple-files-into-one-worksheet.html ||'
'\\||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||//'
Dim path As String
Dim shtDest As Worksheet
Dim Wkb As Workbook
Dim CopyRng As Range, Dest As Range
Dim RowofCopySheet As Integer
Dim selectedFiles As Variant, filename As Variant
Dim i As Long
Dim LR As Long
Dim rngFilter As Range
Dim r As Long, f As Long
Dim GetDesktop As String
Application.ScreenUpdating = False
Application.Calculation = xlManual
'// Deletes the Effort Dump sheet data to prepare for new report being imported.\\'
Sheets("Effort Dump").Activate
Set rngFilter = ActiveSheet.AutoFilter.Range
r = rngFilter.Rows.Count
f = rngFilter.SpecialCells(xlCellTypeVisible).Count
If r > f Then
ActiveWorkbook.Worksheets("Effort Dump").AutoFilter.Sort.SortFields.Clear
ActiveSheet.ShowAllData
End If
i = Range("A" & Rows.Count).End(xlUp).Row + 3
If i = 4 Then
Range("A5").Select
Else
Rows("5:5").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete
With Sheet11
With Sheet11.Range("A:T")
.FormatConditions.Delete
End With
With .Range("$A$5:$T$1048576")
.Activate
.FormatConditions.Add xlExpression, Formula1:="=NOT(ISBLANK($A5))"
.FormatConditions(1).Borders.LineStyle = xlContinuous
.FormatConditions(1).Borders.Weight = xlThin
End With
With .Range("$Q$5:$R$1048576")
.Activate
.FormatConditions.Add xlExpression, Formula1:="=NOT(ISBLANK($A5))"
.FormatConditions(1).Interior.ColorIndex = 48
End With
End With
Range("A5").Select
End If
Set shtDest = ActiveWorkbook.Sheets("Effort Dump")
'// Importing of the new files.\\'
RowofCopySheet = 2 ' Row to start on in the sheets you are copying from
GetDesktop = CreateObject("WScript.Shell").SpecialFolders("Desktop") & _
Application.PathSeparator
path = GetDesktop
selectedFiles = SelectFiles(path)
If IsArray(selectedFiles) Then
Application.EnableEvents = False
'// Open and merge each selected file
For Each filename In selectedFiles
If filename <> ActiveWorkbook.FullName Then
Set Wkb = Workbooks.Open(filename)
With Wkb.Sheets(1)
Set CopyRng = .Range(.Cells(RowofCopySheet, 1), _
.Cells(Cells(Rows.Count, 1).End(xlUp).Row, .Cells(1, Columns.Count).End(xlToLeft).Column))
End With
Set Dest = shtDest.Range("A" & shtDest.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1)
CopyRng.Copy
Dest.PasteSpecial xlPasteValuesAndNumberFormats
Application.CutCopyMode = False 'Clear Clipboard
Wkb.Close False
End If
Next
Application.EnableEvents = True
Range("A1").Select
MsgBox "Processing Complete."
Else
MsgBox "No files were selected"
End If
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub
Private Function SelectFiles(startFolderPath As String) As Variant
Dim Filter As String
Dim FilterIndex As Integer
'File filters
Filter = "Excel workbooks (*.xls), *.xls"
FilterIndex = 1
'Set start drive and path
ChDrive (startFolderPath)
ChDir (startFolderPath)
With Application
'Get array of selected file(s)
SelectFiles = .GetOpenFilename(Filter, FilterIndex, "Select File(s) to Merge", , MultiSelect:=True)
'Reset start drive and path
ChDrive (.DefaultFilePath)
ChDir (.DefaultFilePath)
End With
End Function
EDIT: I am using Excel 2013.
Bookmarks