Hi Dear
i am new to VBA. when i am doing test by pressing F8 my macros are running fine but when i am running my macros worksheet function sumif is not calculating its showing zero
below is my macro
i am new to VBA. when i am doing test by pressing F8 my macros are running fine but when i am running my macros worksheet function sumif is not calculating its showing zero
below is my macro
VBA Code:
Dim sht As Worksheet
Dim shtName As String
Dim i As Long
Dim rng As Range
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim SettlementRange As Range
Dim DoctorshareRange As Range
Set SettlementRange = Range("V:V")
Set DoctorshareRange = Range("Q:Q")
i = Sheets.Count
Dim cell As Range
For i = 1 To i
If Sheets(i).Name = "vida data after macros" Then
Sheets("vida data after macros").Select
Set ws = Worksheets("vida data after macros")
For Each cell In Range("G1:G6893")
If InStr(cell.Value, "DR.ADLA BAKRI A. HASSAN") > 0 Then
cell.Activate
Exit For
End If
Next cell
ActiveCell.Offset(1, -5).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Dr.Adla Bakri").Select
ActiveSheet.Range("A9").Select
ActiveSheet.Paste
With Selection
.HorizontalAlignment = xlLeft
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Font
.Name = "Arial"
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Range("V1").Select
ActiveCell.FormulaR1C1 = "Invoice"
Range("V3").Select
ActiveCell.FormulaR1C1 = "Settlement"
Set rng1 = Range("V:V").Find("Invoice", SearchDirection:=xlPrevious)
rng1.Activate
ActiveCell.Offset(1, 0).Select
Selection.EntireRow.Insert
ActiveCell.Offset(0, -5).Range("A1").Select
ActiveCell = _
WorksheetFunction.SumIf(SettlementRange, "invoice", DoctorshareRange)
Selection.Font.Bold = True
ActiveCell.Select
ActiveCell.Name = "Adlainvoice"
Set rng2 = Range("V:V").Find("Settlement", SearchDirection:=xlPrevious)
rng2.Activate
ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(0, -5).Range("A1").Select
ActiveCell = _
WorksheetFunction.SumIf(SettlementRange, "Settlement", DoctorshareRange)
ActiveCell.Name = "Adlasettlement"
Selection.Font.Bold = True
Range("A4:V8").Select
With Selection.Font
.Name = "Arial"
.Size = 11
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Range("A5").Select
Selection.NumberFormat = "[$-en-US]mmm-yy;@"
Range("A8:V8").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("A8").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Font.Bold = True
ActiveSheet.Cells.EntireColumn.AutoFit
ActiveSheet.Columns("B").Delete
ActiveSheet.Columns("E").Delete
ActiveSheet.Columns("F:H").Delete
ActiveSheet.Columns("I").Delete
ActiveSheet.Columns("L").Delete
Rows("1:3").Select
Selection.Delete Shift:=xlUp
End If
Next i
Sheets("vida data after macros").Select
For Each cell In Range("G1:G6893")
If InStr(cell.Value, "DR.AHMED J. JAMAL") > 0 Then
cell.Activate
Exit For
End If
Next cell
ActiveCell.Offset(1, -5).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Dr.Ahmed Jamal").Select
ActiveSheet.Range("A9").Select
ActiveSheet.Paste
With Selection
.HorizontalAlignment = xlLeft
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Font
.Name = "Arial"
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Range("V1").Select
ActiveCell.FormulaR1C1 = "Invoice"
Range("V3").Select
ActiveCell.FormulaR1C1 = "Settlement"
Set rng1 = Range("V:V").Find("Invoice", SearchDirection:=xlPrevious)
rng1.Activate
ActiveCell.Offset(1, 0).Select
Selection.EntireRow.Insert
ActiveCell.Offset(0, -5).Range("A1").Select
ActiveCell = _
WorksheetFunction.SumIf(SettlementRange, "invoice", DoctorshareRange)
Selection.Font.Bold = True
ActiveCell.Select
ActiveCell.Name = "Ahmedjamalinvoice"
Set rng2 = Range("V:V").Find("Settlement", SearchDirection:=xlPrevious)
rng2.Activate
ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(0, -5).Range("A1").Select
ActiveCell = _
WorksheetFunction.SumIf(SettlementRange, "Settlement", DoctorshareRange)
ActiveCell.Name = "Ahmedjamalsettlement"
Selection.Font.Bold = True
Range("A4:V8").Select
With Selection.Font
.Name = "Arial"
.Size = 11
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Range("A5").Select
Selection.NumberFormat = "[$-en-US]mmm-yy;@"
Range("A8:V8").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("A8").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Font.Bold = True
ActiveSheet.Cells.EntireColumn.AutoFit
ActiveSheet.Columns("B").Delete
ActiveSheet.Columns("E").Delete
ActiveSheet.Columns("F:H").Delete
ActiveSheet.Columns("I").Delete
ActiveSheet.Columns("L").Delete
Rows("1:3").Select
Selection.Delete Shift:=xlUp
Sheets("vida data after macros").Select
For Each cell In Range("G1:G6893")
If InStr(cell.Value, "DR.JAMAL SALEH") > 0 Then
cell.Activate
Exit For
End If
Next cell
ActiveCell.Offset(1, -5).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Dr. Jamal Saleh").Select
ActiveSheet.Range("A9").Select
ActiveSheet.Paste
With Selection
.HorizontalAlignment = xlLeft
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Font
.Name = "Arial"
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Range("V1").Select
ActiveCell.FormulaR1C1 = "Invoice"
Range("V3").Select
ActiveCell.FormulaR1C1 = "Settlement"
Set rng1 = Range("V:V").Find("Invoice", SearchDirection:=xlPrevious)
rng1.Activate
ActiveCell.Offset(1, 0).Select
Selection.EntireRow.Insert
ActiveCell.Offset(0, -5).Range("A1").Select
ActiveCell = _
WorksheetFunction.SumIf(SettlementRange, "invoice", DoctorshareRange)
Selection.Font.Bold = True
ActiveCell.Select
ActiveCell.Name = "Jamalsalahinvoice"
Set rng2 = Range("V:V").Find("Settlement", SearchDirection:=xlPrevious)
rng2.Activate
ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(0, -5).Range("A1").Select
ActiveCell = _
WorksheetFunction.SumIf(SettlementRange, "Settlement", DoctorshareRange)
ActiveCell.Name = "Jamalsalahsettlement"
Selection.Font.Bold = True
Range("A4:V8").Select
With Selection.Font
.Name = "Arial"
.Size = 11
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Range("A5").Select
Selection.NumberFormat = "[$-en-US]mmm-yy;@"
Range("A8:V8").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("A8").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlDot
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Font.Bold = True
ActiveSheet.Cells.EntireColumn.AutoFit
ActiveSheet.Columns("B").Delete
ActiveSheet.Columns("E").Delete
ActiveSheet.Columns("F:H").Delete
ActiveSheet.Columns("I").Delete
ActiveSheet.Columns("L").Delete
Rows("1:3").Select
Selection.Delete Shift:=xlUp
End Sub
Last edited by a moderator: