Foundcell that is not empty in the column - excel

I have been trying to only display a combo box list that is not empty but it's not working. Why? Though I add this line "If Not IsEmpty(ActiveCell.Value) Then"
Private Sub SearchButton_Click()
If SearchTeamComboBox.ListIndex < 0 And SearchSelectPPComboBox.ListIndex < 0 Then
MsgBox "Please select Team and the Process/project you want to search ."
SearchTeamComboBox.SetFocus
ElseIf SearchTeamComboBox.ListIndex < 0 Then
MsgBox "Please select Team."
SearchTeamComboBox.SetFocus
ElseIf SearchSelectPPComboBox.ListIndex < 0 Then
MsgBox "Please select the Process/project you want to search ."
SearchSelectPPComboBox.SetFocus
Else
Dim WHAT_TO_FIND As String
Dim ws As Excel.Worksheet
Dim FoundCell As Excel.Range
WHAT_TO_FIND = SearchSelectPPComboBox.Value
Set ws = Sheets(SearchTeamComboBox.Value)
Set FoundCell = ws.Range("F8:F" & ws.Range("F8").SpecialCells(xlCellTypeLastCell).Row).Find(what:=WHAT_TO_FIND, lookat:=xlWhole)
'If Not IsEmpty(ActiveCell.Value) Then
If Not FoundCell Is Nothing Then
MsgBox (WHAT_TO_FIND & " is found ")
Me.ExistingProcessProjectNameTextbox = FoundCell.Offset(0, 0).Value
Me.ExistingTeamComboBox = SearchTeamComboBox.Value
Me.ExistingchecklistComboBox.Value = FoundCell.Offset(0, 1).Value
Me.ExistingORRComboBox.Value = FoundCell.Offset(0, 2).Value
Me.ExistingdateTextBox.Value = FoundCell.Offset(0, 3).Value
End If
End If
End If
End Sub
Next codes! ................................................................................................................................................................................................................................................................................................................................
Private Sub SearchTeamComboBox_Change()
Application.EnableEvents = False
SearchSelectPPComboBox.Clear
Application.EnableEvents = True
Dim PP As Object
Dim rngList As Range
Dim strSelected As String
Dim LastRow As Long
' check that a team has been selected
If SearchTeamComboBox.ListIndex <> -1 Then
strSelected = SearchTeamComboBox.Value
If strSelected = "ACLT" Then
LastRow = Worksheets("ACLT").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("ACLT").Range("E8:E" & LastRow)
ElseIf strSelected = "AIFCIF" Then
LastRow = Worksheets("AIFCIF").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("AIFCIF").Range("E8:E" & LastRow)
ElseIf strSelected = "FDM" Then
LastRow = Worksheets("FDM").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("FDM").Range("E8:E" & LastRow)
ElseIf strSelected = "Imaging" Then
LastRow = Worksheets("Imaging").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("Imaging").Range("E8:E" & LastRow)
ElseIf strSelected = "MRT" Then
LastRow = Worksheets("MRT").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("MRT").Range("E8:E" & LastRow)
ElseIf strSelected = "PAT" Then
LastRow = Worksheets("PAT").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("PAT").Range("E8:E" & LastRow)
ElseIf strSelected = "SSU" Then
LastRow = Worksheets("SSU").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("SSU").Range("E8:E" & LastRow)
ElseIf strSelected = "VEL" Then
LastRow = Worksheets("VEL").Range("E" & Rows.Count).End(xlUp).Row
Set rngList = Worksheets("VEL").Range("E8:E" & LastRow)
End If
For Each PP In rngList
If Len(PP.Value) > 0 Then
SearchSelectPPComboBox.AddItem PP.Offset(, 1)
Next PP
End If
End If
End Sub

I added conditions for each combo box to add items only if not empty, in the Else branch
Private Sub SearchButton_Click()
Dim cmbTeam As ComboBox, cmbPP As ComboBox, lastRow As Long
Dim searchedText As String, ws As Worksheet, found As Range
Set cmbTeam = SearchTeamComboBox
Set cmbPP = SearchSelectPPComboBox
If cmbTeam.ListIndex < 0 And cmbPP.ListIndex < 0 Then
MsgBox "Please select Team and the Process/project to search."
cmbTeam.SetFocus
ElseIf cmbTeam.ListIndex < 0 Then
MsgBox "Please select Team."
cmbTeam.SetFocus
ElseIf cmbPP.ListIndex < 0 Then
MsgBox "Please select the Process/project to search."
cmbPP.SetFocus
Else
searchedText = cmbPP.Value
Set ws = Worksheets(cmbTeam.Value)
lastRow = ws.Range("F8").SpecialCells(xlCellTypeLastCell).Row
Set found = ws.Range("F8:F" & lastRow).Find(what:=searchedText, lookat:=xlWhole)
If Not found Is Nothing Then
With found
MsgBox (searchedText & " is found ")
If Len(.Offset(0, 0).Value) > 0 Then Me.ExistingProcessProjectNameTextbox = .Offset(0, 0).Value
Me.ExistingTeamComboBox = cmbTeam.Value
If Len(.Offset(0, 1).Value) > 0 Then Me.ExistingchecklistComboBox.Value = .Offset(0, 1).Value
If Len(.Offset(0, 2).Value) > 0 Then Me.ExistingORRComboBox.Value = .Offset(0, 2).Value
If Len(.Offset(0, 3).Value) > 0 Then Me.ExistingdateTextBox.Value = .Offset(0, 3).Value
End If
End If
End If
End Sub
I also removed one extra "End If", but I didn't test it (don't have your form)
Edit:
You can fix the For Each part by replacing this:
For Each PP In rngList
If Len(PP.Value) > 0 Then
SearchSelectPPComboBox.AddItem PP.Offset(, 1)
Next PP
with this
For Each PP In rngList
If Len(PP.Offset(, 1)) > 0 Then SearchSelectPPComboBox.AddItem PP.Offset(, 1).Value2
Next
or this
For Each PP In rngList
If Len(PP.Offset(, 1).Value2) > 0 Then
SearchSelectPPComboBox.AddItem PP.Offset(, 1).Value2
End If
Next
It's also safe to change Dim PP As Object to Dim PP As Range

Related

How do I copy values (and not the formatting) within a With..End With block?

I have tried incoperating .PasteSpecial xlPasteValues and .value in to the code. But can't seem to get it right.
Private Sub CommandButton1_Click()
Dim wsSource, wsTarget As Worksheet
Dim i, iLastSource, iRowTarget, count As Long
Dim cell As Range
Set wsSource = Worksheets("Stig Jan")
iLastSource = wsSource.Cells(Rows.count, 1).End(xlUp).Row
Set wsTarget = Worksheets("Laura Jan")
count = 0
With wsSource
iRowTarget = wsTarget.Cells(Rows.count, 1).End(xlUp).Row + 1
For i = 36 To iLastSource
Set cell = .Cells(i, 4)
If cell.Font.Bold = False Then
If cell.Value = "Fælles" Or cell.Value = "Lagt Ud" Then
.Rows(i).Columns("A:H").Copy wsTarget.Range("A" & iRowTarget)
wsTarget.Range("D" & iRowTarget).ClearContents
iRowTarget = iRowTarget + 1
count = count + 1
End If
End If
If cell.Value = "Fælles" Or cell.Value = "Lagt Ud" Then
wsSource.Rows(i).Columns("A:H").Font.Bold = True
End If
Next
End With
If you want to use value transfer, change:
.Rows(i).Columns("A:H").Copy wsTarget.Range("A" & iRowTarget)
to
wsTarget.Range("A" & iRowTarget & ":H" & iRowTarget).Value = .Range("A" & i & ":H" & i).Value

If condition is true filter the value and put "yes" next to it else"no"

Hello i m very new at VBA as i m suffering with an issue here,in filtered cell if the condition is correct then put "yes" else put "NO"
but when i run the code in for LOOP it put the yes data in all even if the condition is not true
VBA
Sub check()
Dim j As Long
Dim dsheet As Worksheet
Dim lastrow As Long
Dim fr As Range
Dim psheet As Worksheet
Dim c As Range
Set dsheet = Worksheets("Workings")
Set psheet = Worksheets("sheet1")
lastrow = dsheet.Cells(Rows.Count, 1).End(xlUp).row
For j = 1 To lastrow
psheet.Range("M2").Value = dsheet.Range("A2" & j)
psheet.Range("N2").Value = dsheet.Range("B2" & j)
psheet.Range("A1").AutoFilter Field:=1, Criteria1:=psheet.Range("M2")
psheet.Range("B1").AutoFilter Field:=2, Criteria1:=psheet.Range("N2")
psheet.Range("A2:I" & psheet.Cells(Rows.Count, 1).End(xlUp).row).SpecialCells (xlCellTypeVisible)
dsheet.Range("M2").Value = dsheet.Range("A" & j)
dsheet.Range("N2").Value = dsheet.Range("B" & j)
dsheet.Range("A1").AutoFilter Field:=1, Criteria1:=dsheet.Range("M2")
dsheet.Range("B1").AutoFilter Field:=2, Criteria1:=dsheet.Range("N2")
Set fr = psheet.Range("C2:C50").Find(what:="12345", MatchCase:=True)
For Each c In dsheet.Range("E2:E2000" & Range("A" & Rows.Count).End(xlUp).row).SpecialCells(xlCellTypeVisible)
If fr Is Nothing Then
dsheet.Range("A2" & Range("A" & Rows.Count).End(xlUp).row).SpecialCells (xlCellTypeVisible)
If c.Offset(, -1).Value = vbNullString Then Exit For
c.Value = "NO"
Else
dsheet.Range("A2" & Range("A" Rows.Count).End(xlUp).row).SpecialCells (xlCellTypeVisible)
If c.Offset(, -1). Value =vbNullString Then Exit For
c.Value = "Yes"
End If
Next c
Next j
dsheet. AutoFilterMode = False
psheet. AutoFilterMode = False
End Sub
so, i want the the code to put the "yes" or "NO" according to the condition,It will be great help if anyone help me in this issue
You need an End If for each If statement. It should be like this: Also, proper indenting helps make your code more readable.
If fr Is Nothing Then
dsheet.Range("A2" & Range("A" & Rows.Count).End(xlUp).row).SpecialCells (xlCellTypeVisible)
If c.Offset(, -1).Value = vbNullString Then Exit For
End If
c.Value = "NO"
Else
dsheet.Range("A2" & Range("A" Rows.Count).End(xlUp).row).SpecialCells
(xlCellTypeVisible)
If c.Offset(, -1). Value =vbNullString Then Exit For
End If
c.Value = "Yes"
End If

SUM Ranges depending on value with VBA

I'm trying to do the following with VBA. Imagine I have some data as it follows:
I would like my final result to be the sum of every data that is between "BEGINDATA" and "ENDDATA". So it would look like this:
My goal is to obtain the green data and write it next to "ENDDATA"
Any idea or suggestion?
Thank you so much!!
Try:
Option Explicit
Sub test()
Dim Lastrow As Long, BeginData As Long, EndData As Long, i As Long
With ThisWorkbook.Worksheets("Sheet1")
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To Lastrow
If .Range("A" & i).Value = "BEGINDATA" Then
BeginData = i
ElseIf .Range("A" & i).Value = "ENDDATA" Then
EndData = i
End If
If EndData > BeginData Then
.Range("B" & i).Value = Application.Sum(.Range("B" & BeginData + 1 & ":B" & EndData - 1))
End If
Next i
End With
End Sub
Another Version:
Option Explicit
Sub test()
Dim Lastrow As Long, BeginData As Long, EndData As Long, i As Long
With ThisWorkbook.Worksheets("Sheet1")
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To Lastrow
If .Range("A" & i).Value = "BEGINDATA" Then
BeginData = i
ElseIf .Range("A" & i).Value = "ENDDATA" Then
EndData = i
End If
If EndData > BeginData Then
With .Range("B" & i)
.Value = Application.Sum(Sheet1.Range("B" & BeginData + 1 & ":B" & EndData - 1))
.Interior.Color = vbGreen
End With
End If
Next i
End With
End Sub
You can also achieve this using Find which will be faster then looping
Option Explicit
Sub Demo()
Dim BeginData As Range, EndData As Range
Dim FirstBeginAddress As String
' Update with your range
With Sheet1.Columns(1)
Set BeginData = .Find(what:="BEGINDATA", after:=.Cells(.Cells.Count), LookIn:=xlValues, lookat:=xlWhole)
If Not BeginData Is Nothing Then
FirstBeginAddress = BeginData.Address
Set EndData = .Find("ENDDATA", after:=BeginData)
Do
Debug.Print "BeginAddress", BeginData.Address
If Not EndData Is Nothing And EndData.Row > BeginData.Row Then
Debug.Print "EndAddress", EndData.Address
'' For Formula
EndData.Offset(0, 1).Formula = "=SUM(" & Range(BeginData.Offset(1, 1), EndData.Offset(-1, 1)).Address & ")"
'' For value
'EndData.Offset(0, 1).Value2 = Application.Sum(Range(BeginData.Offset(1, 1), EndData.Offset(-1, 1)))
Set EndData = .Find("ENDDATA", after:=EndData)
Else
Err.Raise 998, "Demo", "Unable to find Data Footer"
End If
Set BeginData = .Find("BEGINDATA", after:=BeginData)
Loop Until BeginData Is Nothing Or BeginData.Address = FirstBeginAddress
Else
Err.Raise 999, "Demo", "Unable to find Data Header"
End If
End With
End Sub

compare 2 excel sheets for differences

I need to compare 2 excel sheets (Sheet1 (old report) & Sheet2 (new report)) for differences. If there are any additions or removals in Sheet2 compared to Sheet1 I need to print that.
I found this script to find the differences but this is not including the removals in the sheet. Can you help fixing this? Below is sample example on my expectation.
Sheet1:
S.No Name Class
abc1 1st
abc2 1st
abc3 1st
Sheet2:
S.No Name Class
abc1 1st
abc2 2nd
abc4 1st
.
Comparison should tell all these:
"Row(3,3)" is changed from "1st" to "2nd"
New row inserted in "sheet2" "Row4"
"Sheet1" "Row4" is deleted in "Sheet2"
Script currently I have:
Sub Compare2Shts()
For Each cell In Worksheets("CompareSheet#1").UsedRange
If cell.Value <> Worksheets("CompareSheet#2").Range(cell.Address) Then
cell.Interior.ColorIndex = 3
End If
Next
For Each cell In Worksheets("CompareSheet#2").UsedRange
If cell.Value <> Worksheets("CompareSheet#1").Range(cell.Address) Then
cell.Interior.ColorIndex = 3
End If
Next
End Sub
Sub CompareAnother2Shts()
For Each cell In Worksheets("CompareSheet#1").Range("A1:J50000")
If cell.Value <> Worksheets("CompareSheet#2").Range(cell.Address) Then
cell.Interior.ColorIndex = 3
End If
Next
For Each cell In Worksheets("CompareSheet#2").Range("A1:J50000")
If cell.Value <> Worksheets("CompareSheet#1").Range(cell.Address) Then
cell.Interior.ColorIndex = 3
End If
Next
End Sub
Sub FindDupes() 'assuming both sheets are in same book and book is open
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim cell1 As Range
Dim cell2 As Range
Dim str As String
str = InputBox("Type name of first sheet")
Set sht1 = Worksheets(str)
str = InputBox("Type name of second sheet")
Set sht2 = Worksheets(str)
sht1.Range("A65536").End(xlDown).Activate
Selection.End(xlUp).Activate
LastRowSht1 = ActiveCell.Row
sht2.Activate
sht2.Range("A65536").End(xlDown).Activate
Selection.End(xlUp).Activate
LastRowSht2 = ActiveCell.Row
sht1.Activate
For rowSht1 = 1 To LastRowSht1
If sht1.Cells(rowSht1, 1) = "" Then Exit Sub
For rowSht2 = 1 To LastRowSht2
If sht1.Cells(rowSht1, 1).Value = sht2.Cells(rowSht2, 1).Value Then
sht1.Cells(rowSht1, 1).Interior.ColorIndex = 3
sht2.Cells(rowSht2, 1).Interior.ColorIndex = 3
End If
Next
Next
sht1.Cells(1, 1).Select
End Sub
******** ******** ******** ******** ******** ******** ******** ********
Sub checkrev()
With Sheets("Sheet1")
Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh1Range = .Range("A1:A" & Sh1LastRow)
End With
With Sheets("Sheet2")
Sh2LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh2Range = .Range("A1:A" & Sh2LastRow)
End With
'compare sheet 1 with sheet 2
For Each Sh1cell In Sh1Range
Set c = Sh2Range.Find( _
what:=Sh1cell, LookIn:=xlValues)
If c Is Nothing Then
Sh1cell.Interior.ColorIndex = 3
Sh1cell.Offset(0, 1).Interior.ColorIndex = 3
Else
If Sh1cell.Offset(0, 1) <> c.Offset(0, 1) Then
Sh1cell.Interior.ColorIndex = 6
Sh1cell.Offset(0, 1).Interior.ColorIndex = 6
End If
End If
Next Sh1cell
'compare sheet 2 with sheet 1
For Each Sh2cell In Sh2Range
Set c = Sh1Range.Find( _
what:=Sh2cell, LookIn:=xlValues)
If c Is Nothing Then
Sh2cell.Interior.ColorIndex = 3
Sh2cell.Offset(0, 1).Interior.ColorIndex = 3
Else
If Sh2cell.Offset(0, 1) <> c.Offset(0, 1) Then
Sh2cell.Interior.ColorIndex = 6
Sh2cell.Offset(0, 1).Interior.ColorIndex = 6
End If
End If
Next Sh2cell
End Sub
******** ******** ******** ******** ******** ******** ******** ********
Sub TestCompareWorksheets()
' compare two different worksheets in the active workbook
CompareWorksheets Worksheets("Sheet1"), Worksheets("Sheet2")
' compare two different worksheets in two different workbooks
' CompareWorksheets ActiveWorkbook.Worksheets("Sheet1"), _
Workbooks("WorkBookName.xls").Worksheets("Sheet2")
End Sub
Sub CompareWorksheets(ws1 As Worksheet, ws2 As Worksheet)
Dim r As Long, c As Integer
Dim lr1 As Long, lr2 As Long, lc1 As Integer, lc2 As Integer
Dim maxR As Long, maxC As Integer, cf1 As String, cf2 As String
Dim rptWB As Workbook, DiffCount As Long
Application.ScreenUpdating = False
Application.StatusBar = "Creating the report..."
Set rptWB = Workbooks.Add
Application.DisplayAlerts = False
While Worksheets.Count > 1
Worksheets(2).Delete
Wend
Application.DisplayAlerts = True
With ws1.UsedRange
lr1 = .Rows.Count
lc1 = .Columns.Count
End With
With ws2.UsedRange
lr2 = .Rows.Count
lc2 = .Columns.Count
End With
maxR = lr1
maxC = lc1
If maxR < lr2 Then maxR = lr2
If maxC < lc2 Then maxC = lc2
DiffCount = 0
For c = 1 To maxC
Application.StatusBar = "Comparing cells " & Format(c / maxC, "0 %") & "..."
For r = 1 To maxR
cf1 = ""
cf2 = ""
On Error Resume Next
cf1 = ws1.Cells(r, c).FormulaLocal
cf2 = ws2.Cells(r, c).FormulaLocal
On Error GoTo 0
If cf1 <> cf2 Then
DiffCount = DiffCount + 1
Cells(r, c).Formula = "'" & cf1 & " <> " & cf2
End If
Next r
Next c
Application.StatusBar = "Formatting the report..."
With Range(Cells(1, 1), Cells(maxR, maxC))
.Interior.ColorIndex = 19
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error Resume Next
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error GoTo 0
End With
Columns("A:IV").ColumnWidth = 20
rptWB.Saved = True
If DiffCount = 0 Then
rptWB.Close False
End If
Set rptWB = Nothing
Application.StatusBar = False
Application.ScreenUpdating = True
MsgBox DiffCount & " cells contain different formulas!", vbInformation, _
"Compare " & ws1.Name & " with " & ws2.Name
End Sub
******** ******** ******** ******** ******** ******** ******** ********
Sub Match()
r1 = Worksheets("sheet1").Cells(Rows.Count, "A").End(xlUp).Row
r2 = Worksheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Row
Set r3 = Worksheets("sheet1")
Worksheets("sheet2").Range("B2").Select
For a = 2 To r2
For i = 2 To r1
If Cells(a, "A") = r3.Cells(i, "A") Then
temp = r3.Cells(i, "B")
te = te & "," & temp
Else
End If
Next i
Cells(a, "B") = te
te = ""
Next a
End Sub
Sub Match2()
Dim myCon As String
Dim myCell As Range
Dim cell As Range
For Each cell In Sheet2.Range("A2:A10")
myCon = ""
For Each myCell In Sheet1.Range("A1:A15")
If cell = myCell Then
If myCon = "" Then
myCon = myCell.Offset(0, 1)
Else
myCon = myCon & ", " & myCell.Offset(0, 1)
End If
End If
Next myCell
cell.Offset(0, 1) = myCon
Next cell
End Sub
******** ******** ******** ******** ******** ******** ******** ********
Sub Duplicates()
ScreenUpdating = False
'get first empty row of sheet1
'find matching rows in sheet 2
With Sheets("Masterfile")
RowCount = 1
Do While .Range("A" & RowCount) <> ""
ID = Trim(.Range("A" & RowCount))
'compare - look for ID in Sheet 2
With Sheets("List")
Set c = .Columns("A").Find(what:=ID, _
LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
End With
If c Is Nothing Then
.Range("B" & RowCount) = "No"
Else
.Range("B" & RowCount) = "Yes"
End If
RowCount = RowCount + 1
Loop
End With
ScreenUpdating = True
End Sub
The code you have looks overly complex.
For a non-vba solution, see below.
Sheet 1 formula:
=IF(ISERROR(VLOOKUP(A2,Sheet2!$A$2:$B$4,2,0)),"Removed",IF(VLOOKUP(A2,Sheet2!$A$2:$B$4,2,0)=B2,"Same","Changed to: " &VLOOKUP(A2,Sheet2!$A$2:$B$4,2,0)))
Sheet 2 formula:
=IF(ISERROR(VLOOKUP(A2,Sheet1!$A$2:$B$4,2,0)),"Added",IF(VLOOKUP(A2,Sheet1!$A$2:$B$4,2,0)=B2,"Same","Changed"))
I realize I may haved simplified things a bit, but you can adjust wording and whatever is needed. You can also apply conditional formatting as needed.

Get values on a row based on two or more rows in Excel

I have values in DEMAND row and values in the COLLECTION row, now I want BALANCE = DEMAND-COLLECTION, there are two times collection for an entry so according to the occurrence of collection the balance should arise. Can you please let me know the macro code for that.
I have DEMAND values D1:S1 COLLECTION values from D2:S2 and the balance should be there in the next row.
I came to this step after the solution I got from
Insert row base on specific text and its occurrence
I am using the following code
Sub try()
Dim c As Range
Dim lRow As Long
lRow = 1
Dim lRowLast As Long
Dim bFound As Boolean
With ActiveSheet
lRowLast = .Cells(.Rows.Count, 1).End(xlUp).Row
Do
Set c = .Range("A" & lRow)
If c.Value Like "*COLLECTION*" Then
bFound = True
ElseIf bFound Then
bFound = False
If c.Value <> "BALANCE" Then
c.EntireRow.Insert
lRowLast = lRowLast + 1
c.Offset(-1, 0).Value = "BALANCE"
c.Offset(-1, 0).Font.Color = RGB(0, 0, 0)
End If
End If
lRow = lRow + 1
Loop While lRow <= lRowLast + 1
End With
End Sub
before macro check IMAGE
After Macro I want this check image
So I would use SUMIF applied with FormulaR1C1 for that. The advantage is that we can set the formula in one step for the whole row.
Sub try()
Dim c As Range
Dim lRow As Long
lRow = 1
Dim lRowLast As Long
Dim lRowDiff As Long
Dim lRowPortion As Long
lRowPortion = 1
Dim bFoundCollection As Boolean
With ActiveSheet
lRowLast = .Cells(.Rows.Count, 1).End(xlUp).Row
Do
Set c = .Range("A" & lRow)
If c.Value Like "*COLLECTION*" Then
bFoundCollection = True
ElseIf bFoundCollection Then
bFoundCollection = False
If c.Value <> "BALANCE" Then
c.EntireRow.Insert
lRowLast = lRowLast + 1
Set c = c.Offset(-1, 0)
c.Value = "BALANCE"
End If
If c.Value = "BALANCE" Then
.Range(c, c.Offset(0, 18)).Font.Color = RGB(0, 0, 0)
.Range(c, c.Offset(0, 18)).Interior.Color = RGB(200, 200, 200)
lRowDiff = c.Row - lRowPortion
.Range(c.Offset(0, 3), c.Offset(0, 18)).FormulaR1C1 = _
"=SUMIF(R[-" & lRowDiff & "]C1:RC1, ""*DEMAND*"", R[-" & lRowDiff & "]C:RC)" & _
"-SUMIF(R[-" & lRowDiff & "]C1:RC1, ""*COLLECTION*"", R[-" & lRowDiff & "]C:RC)"
lRowPortion = c.Row + 1
End If
End If
lRow = lRow + 1
Loop While lRow <= lRowLast + 1
End With
End Sub

Resources