Shums Faruk
asked on
Insert Delete Rows as per count.
Dear Experts,
I want to count string of cell value of active sheet in another sheet and count non-blank rows of active sheet, find the difference between both these count and insert/delete rows.
I need your help in correcting below code.
I want to count string of cell value of active sheet in another sheet and count non-blank rows of active sheet, find the difference between both these count and insert/delete rows.
I need your help in correcting below code.
Sub Insert_Delete_Rows()
Dim Ws1 As Worksheet, Ws2 As Worksheet
Dim LR1 As Long, LR2 As Long
Dim MyCount1 As Integer, MyCount2 As Integer, RowDiff As Integer
Dim MyRange As Range
Application.ScreenUpdating = False
Set Ws1 = Worksheets("Aging_Report")
Set Ws2 = Worksheets("Statement_Template")
LR1 = Ws1.Range("C" & Rows.Count).End(xlUp).row
LR2 = Ws2.Range("C" & Rows.Count).End(xlUp).row
Set MyRange = Ws2.Range("C24:C" & LR2)
MyCount1 = Application.WorksheetFunction.CountIfs(Ws1.Range("D3:D" & LR1), Ws2.Range("A12").value, Ws1.Range("B3:B" & LR1), Ws2.Range("J1").value)
Ws2.Range("N1").value = MyCount1
MyCount2 = Application.WorksheetFunction.CountA(MyRange)
Ws2.Range("O1").value = MyCount2
If MyCount1 = MyCount2 Then
Exit Sub
ElseIf MyCount1 > MyCount2 Then
RowDiff = MyCount1 - MyCount2
Ws2.Range("A25:H25").EntireRow.Copy
Ws2.Range("A30:A" & RowDiff + 2).EntireRow.Insert
Application.CutCopyMode = False
ElseIf MyCount1 < MyCount2 Then
RowDiff = MyCount2 - MyCount1
Ws2.Range("A30" & RowDiff + 2).Delete shift:=xlUp
End If
Application.ScreenUpdating = True
End Sub
Upload a small sample workbook with the code implemented and let us know what the code is not doing which it is supposed to do.
Hi,
on line 29 maybe better
on line 29 maybe better
Ws2.Range("A30:A" & RowDiff + 2).Delete shift:=xlUp
than Ws2.Range("A30" & RowDiff + 2).Delete shift:=xlUp
Regards
ASKER
Thanks Subodh & Rgonzo,
Rgonzo, I did changed but still no effect.
Subodh,
I have lots of sheets and many modules to run the file, but I am attaching only the sheets which needs correction.
Once user clicks print statement, UserForm Pops up, user will select for which customer he/she needs to print statement, then he/she needs to select the invoice format 811KY or 822AO or 511KY (I have to put this option because I am failing to extend the rows more that 60 rows), If I get rows insertion/deletion, then I may be able to remove this option.
Hope this info may help.
Thanks again for your help.
Aging-Analysis-Test.xlsm
Rgonzo, I did changed but still no effect.
Subodh,
I have lots of sheets and many modules to run the file, but I am attaching only the sheets which needs correction.
Once user clicks print statement, UserForm Pops up, user will select for which customer he/she needs to print statement, then he/she needs to select the invoice format 811KY or 822AO or 511KY (I have to put this option because I am failing to extend the rows more that 60 rows), If I get rows insertion/deletion, then I may be able to remove this option.
Hope this info may help.
Thanks again for your help.
Aging-Analysis-Test.xlsm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you have an issue with downloading and opening the file due to a temporary bug in the forum, first download and save it on your system and then open it.
ASKER
Thanks a lot Subodh,
I will try with all the required changes I would like to make and revert to you,
For now initial requirement of insert/delete rows are perfectly working fine.
I will try with all the required changes I would like to make and revert to you,
For now initial requirement of insert/delete rows are perfectly working fine.
ASKER
Subodh,
I am little bit confused, before I mess your code, I want to make sure, how I am make change to my below code which was in other module: Update_StatementTemplate() :
This code changes the number format as per cell value, if I am gonna incorporate your code below range will fluctuate.
Same for below, I am copying the range from start row of "A" column to "H" till the last row:
Please help.
Regards,
Shums
I am little bit confused, before I mess your code, I want to make sure, how I am make change to my below code which was in other module: Update_StatementTemplate()
This code changes the number format as per cell value, if I am gonna incorporate your code below range will fluctuate.
If Ws2.Range("I2") = "$" Then
Ws2.Range("H87:H90").NumberFormat = "[$$ ]#,##0.00_ ;-[$$ ]#,##0.00 "
ElseIf Ws2.Range("I2") = "AOA" Then
Ws2.Range("H87:H90").NumberFormat = "[$AOA ]#,##0.00_ ;-[$AOA ]#,##0.00 "
End If
Same for below, I am copying the range from start row of "A" column to "H" till the last row:
Ws2.Range("A25:H25").Copy
Ws2.Range("A25:H82").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Please help.
Regards,
Shums
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Subodh,
That really makes sense. I did accordingly.
Now lastly, I also have below code in UserForm, which sends the details to cells like currency
That really makes sense. I did accordingly.
Now lastly, I also have below code in UserForm, which sends the details to cells like currency
If CurrencyComboBox.value = "$" Then
Ws.Range("I2").value = "$"
[b]Ws.Range("H76").value = ""[/b]
ElseIf CurrencyComboBox.value = "AOA" Then
Ws.Range("I2").value = "AOA"
[b]Ws.Range("H76").value = ExchangeRate.value[/b]
End If
How I am gonna change there?
Not sure which cell is being referred by H76 in your original workbook.
But you can try to find that cell dynamically in the code.
Let's assume you are referring to the cell H85 as per your original sample workbook. And you will find a header in F85 as "Total Devido/Total Due". So you can try the code something like below to find the string "Total Devido/Total Due" in column F dynamically so that you would know the location of H85 and replace Ws.Range("H76").value with HCell
But you can try to find that cell dynamically in the code.
Let's assume you are referring to the cell H85 as per your original sample workbook. And you will find a header in F85 as "Total Devido/Total Due". So you can try the code something like below to find the string "Total Devido/Total Due" in column F dynamically so that you would know the location of H85 and replace Ws.Range("H76").value with HCell
Dim FCell As Range, HCell As Range
Set FCell = Ws.Range("F:G").Find(what:="Total Devido/Total Due", LookIn:=xlValues, lookat:=xlWhole)
If Not FCell Is Nothing Then
Set HCell = FCell.Offset(0, 1)
If CurrencyComboBox.value = "$" Then
Ws.Range("I2").value = "$"
HCell.value = ""
ElseIf CurrencyComboBox.value = "AOA" Then
Ws.Range("I2").value = "AOA"
HCell.value = ExchangeRate.value
End If
End If
ASKER
I already messed it up.
I will come back to you soon.
Thanks a lot for your help.
Kind Regards,
I will come back to you soon.
Thanks a lot for your help.
Kind Regards,
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good Day Neeraj,
I dont want to spoil your Sunday. But you are very kind to help me with my project.
I changed the minrow to 60 to fit one page.
Everything working fine, except one thing if previous count selection was 60 and new count is also less than 60, then it deletes the last two rows which disturbs the border of my template. I think the problem is within this code:
I think eRow -1 deletes the last two rows.
I dont want to spoil your Sunday. But you are very kind to help me with my project.
I changed the minrow to 60 to fit one page.
Everything working fine, except one thing if previous count selection was 60 and new count is also less than 60, then it deletes the last two rows which disturbs the border of my template. I think the problem is within this code:
Ws2.Range("A" & StartRow + minRow - 1 & ":A" & eRow - 1).EntireRow.Delete
I think eRow -1 deletes the last two rows.
ASKER
Good Day Neeraj,
I am attaching revised workbook. Please check where what I am doing wrong.
At the end of the process I also have below code to auto fit the column, but seems its not reading.
I am attaching revised workbook. Please check where what I am doing wrong.
At the end of the process I also have below code to auto fit the column, but seems its not reading.
Ws2.Columns("H:H").AutoFit
Aging-Analysis-Test.xlsm
ASKER
Hi Neeraj,
I have made some correction for my above queries.
1. Row delete remains unchanged if its less than 60 rows (minRows)
2. For Column Autofit, I have assigned Dim LastCell As Long and changed the code to
Now I have two other problems:
1. I don't know where to place clear previous contents code:
Please see revised attached sheet.
Aging-Analysis-Test.xlsm
I have made some correction for my above queries.
1. Row delete remains unchanged if its less than 60 rows (minRows)
2. For Column Autofit, I have assigned Dim LastCell As Long and changed the code to
LastCell = Ws2.Range("H" & Rows.Count).End(xlUp).row
Ws2.Range("H" & LastCell).Columns.AutoFit
Now I have two other problems:
1. I don't know where to place clear previous contents code:
Ws2.Range("A" & StartRow + minRow - 1 & ":A" & eRow - 1).ClearContents
2. If previous count was greater than minRows count (60rows), it doesn't delete correctly for new selection.Please see revised attached sheet.
Aging-Analysis-Test.xlsm
ASKER
Thanks Neeraj for your assistance in closing this project.
See you soon with other project.
See you soon with other project.