Find function add in the macro file

sudarshank2

New Member
Joined
Nov 1, 2015
Messages
12
Hi Guys,
I have created the macro, however if it won't find the data it should show the Total as zero in Column D and E. I'm able to add one find function (Which is Column D). however, the file also need to search one more function and result should show in Column E.

In this macro if data not found then it will delete the entire row, However I need it as zero.

Macro will pick each file in a folder and search for "ABCD" and do r right and pick the lastRow. Here I need help to Search "WXYZ" too. - If any search not found then it should be zero.

Code is below:
Option Explicit


Sub Extract_Excel()

Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long, LastRow As Long
Dim x As String, y As String, z As Integer




'Fill in the path\folder where the files are
MyPath = ActiveWorkbook.Sheets("Consolidation").Range("E10").Value


'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If


'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If


'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop


'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlAutomatic
.ScreenUpdating = False
.EnableEvents = False
End With


'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1

'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
On Error GoTo 0


If Not mybook Is Nothing Then

On Error Resume Next
With mybook.Worksheets(1)
Cells.Find(What:="ABCD").Select
Selection.Offset(0, 2).Select
ActiveCell.Select
LastRow = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
'Set sourceRange = .Range(ActiveCell.Address & ":" & Cells(LastRow - 1, ActiveCell.Column).Address)
Set sourceRange = Cells(LastRow, ActiveCell.Column)
End With

If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0


If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count


If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else

'Copy the file name in column A
With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(Fnum)
End With

x = mybook.Worksheets(1).Range("B1").Value
y = mybook.Worksheets(1).Range("B2").Value


With sourceRange
BaseWks.Cells(rnum, "B"). _
Resize(.Rows.Count).Value = x


BaseWks.Cells(rnum, "C"). _
Resize(.Rows.Count).Value = y
End With

'Set the destrange
Set destrange = BaseWks.Cells(rnum, 4)


'we copy the values from the sourceRange to the destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With

destrange.Value = sourceRange.Value
'sourceRange.Copy Destination:=destrange
rnum = rnum + SourceRcount
End If
End If

mybook.Close savechanges:=False
End If


Next Fnum

'Adding Grand Total
LastRow = BaseWks.Cells(BaseWks.Rows.Count, "D").End(xlUp).Row
Range("D" & LastRow + 1 & ":E" & LastRow + 1).Select
Selection.FormulaR1C1 = "=SUM(R[-" & LastRow & "]C:R[-1]C)"

Selection.Font.Bold = True
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With

With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With

With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
.PatternTintAndShade = 0
End With


Rows("1:1").Select
Selection.Insert Shift:=xlDown
Range("A1").Select
ActiveCell.FormulaR1C1 = "File_Name"
Range("B1").Select
ActiveCell.FormulaR1C1 = "SP"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Date"
Range("D1").Select
ActiveCell.FormulaR1C1 = "ABCD Total"
Range("E1").Select
ActiveCell.FormulaR1C1 = "WXYZ Total"

Range("A1:E1").Select
Selection.HorizontalAlignment = xlCenter
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Selection.Font.Bold = True

'Format the numbers
LastRow = BaseWks.Cells(BaseWks.Rows.Count, "D").End(xlUp).Row
Range("D2:E" & LastRow).NumberFormat = "#,##0.00_);[Red](#,##0.00)"
Range("D2:E" & LastRow).HorizontalAlignment = xlRight

BaseWks.Columns.AutoFit
Range("A1").Select
LastRow = BaseWks.Cells(BaseWks.Rows.Count, "A").End(xlUp).Row
'MsgBox ("No.of Files in a Directory = " & (Fnum - 1) & vbNewLine & "No.of Files with Totals = " & (LastRow - 1) & vbNewLine & "No.of Files without Totals = " & (Fnum - 1) - (LastRow - 1)), vbInformation, "Report Summary"

End If

ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With


End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I got the answer... Thanks!!!!!
Hi Guys,
I have created the macro, however if it won't find the data it should show the Total as zero in Column D and E. I'm able to add one find function (Which is Column D). however, the file also need to search one more function and result should show in Column E.

In this macro if data not found then it will delete the entire row, However I need it as zero.

Macro will pick each file in a folder and search for "ABCD" and do r right and pick the lastRow. Here I need help to Search "WXYZ" too. - If any search not found then it should be zero.

Code is below:
Option Explicit


Sub Extract_Excel()

Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long, LastRow As Long
Dim x As String, y As String, z As Integer




'Fill in the path\folder where the files are
MyPath = ActiveWorkbook.Sheets("Consolidation").Range("E10").Value


'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If


'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If


'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop


'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlAutomatic
.ScreenUpdating = False
.EnableEvents = False
End With


'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1

'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
On Error GoTo 0


If Not mybook Is Nothing Then

On Error Resume Next
With mybook.Worksheets(1)
Cells.Find(What:="ABCD").Select
Selection.Offset(0, 2).Select
ActiveCell.Select
LastRow = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
'Set sourceRange = .Range(ActiveCell.Address & ":" & Cells(LastRow - 1, ActiveCell.Column).Address)
Set sourceRange = Cells(LastRow, ActiveCell.Column)
End With

If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0


If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count


If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else

'Copy the file name in column A
With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(Fnum)
End With

x = mybook.Worksheets(1).Range("B1").Value
y = mybook.Worksheets(1).Range("B2").Value


With sourceRange
BaseWks.Cells(rnum, "B"). _
Resize(.Rows.Count).Value = x


BaseWks.Cells(rnum, "C"). _
Resize(.Rows.Count).Value = y
End With

'Set the destrange
Set destrange = BaseWks.Cells(rnum, 4)


'we copy the values from the sourceRange to the destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With

destrange.Value = sourceRange.Value
'sourceRange.Copy Destination:=destrange
rnum = rnum + SourceRcount
End If
End If

mybook.Close savechanges:=False
End If


Next Fnum

'Adding Grand Total
LastRow = BaseWks.Cells(BaseWks.Rows.Count, "D").End(xlUp).Row
Range("D" & LastRow + 1 & ":E" & LastRow + 1).Select
Selection.FormulaR1C1 = "=SUM(R[-" & LastRow & "]C:R[-1]C)"

Selection.Font.Bold = True
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With

With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With

With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
.PatternTintAndShade = 0
End With


Rows("1:1").Select
Selection.Insert Shift:=xlDown
Range("A1").Select
ActiveCell.FormulaR1C1 = "File_Name"
Range("B1").Select
ActiveCell.FormulaR1C1 = "SP"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Date"
Range("D1").Select
ActiveCell.FormulaR1C1 = "ABCD Total"
Range("E1").Select
ActiveCell.FormulaR1C1 = "WXYZ Total"

Range("A1:E1").Select
Selection.HorizontalAlignment = xlCenter
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Selection.Font.Bold = True

'Format the numbers
LastRow = BaseWks.Cells(BaseWks.Rows.Count, "D").End(xlUp).Row
Range("D2:E" & LastRow).NumberFormat = "#,##0.00_);[Red](#,##0.00)"
Range("D2:E" & LastRow).HorizontalAlignment = xlRight

BaseWks.Columns.AutoFit
Range("A1").Select
LastRow = BaseWks.Cells(BaseWks.Rows.Count, "A").End(xlUp).Row
'MsgBox ("No.of Files in a Directory = " & (Fnum - 1) & vbNewLine & "No.of Files with Totals = " & (LastRow - 1) & vbNewLine & "No.of Files without Totals = " & (Fnum - 1) - (LastRow - 1)), vbInformation, "Report Summary"

End If

ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,680
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top