Macro to assign cases based on numbers mentioned in cell

Gaurangg

Board Regular
Joined
Aug 6, 2015
Messages
134
Hi,

I need some help in assigning data to the employees from two different worksheets. I have a excel file with 4 worksheets.

1st Sheet = "Process1", 2nd Sheet = "Process2", 3rd Sheet = EMPList and 4th = Assigned_Tasks

I have different data in process1 and Process2. Where I have Employee data with the numbers of cases to be assigned as per column F and Column G. i.e. EmpID 13647 should be assgined 3 cases from Process1 and 1 case from Process2 worksheets. And these 4 cases should also be updated in Assigned task sheet and emailed to him. This macro should work until last employee.

Also Employee and cases should be filtered as Pending from the status field.

Kindly help with the code.
 

Attachments

  • Assigned_Task.JPG
    Assigned_Task.JPG
    45.9 KB · Views: 4
  • EmpList.JPG
    EmpList.JPG
    99.4 KB · Views: 4
  • Process1.JPG
    Process1.JPG
    95.9 KB · Views: 4
  • Process2.JPG
    Process2.JPG
    91.6 KB · Views: 4

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I have worked on below code for selecting Pending case data from Process1 sheet, But could not select the number of rows based on cell value of F2 in EmpList sheet.


Sub CopyRows()

Dim rng As Range
Dim r As Range
Dim n1, n2 As Integer
Dim n As Integer
Dim i As Long

new1 = Sheets("EmpList").Range("F2").Value
new2 = Sheets("EmpList").Range("G2").Value

With Sheets("Process1")
'Dim MRng As Range
On Error Resume Next
Sheets(1).ShowAllData
On Error GoTo 0

If new1 > 0 Then
Sheets("Process1").Activate
Range("A1:D1").AutoFilter
MRng = Sheet1.Range("A1048576").End(xlUp).Row

Range("C1").Select
Range("$A$1:$D" & MRng).AutoFilter Field:=4, Criteria1:="Pending"
Range("$A$1:$D" & MRng).Select

ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.EntireRow.Hidden = False
ActiveCell.Offset(1, 0).Select
Loop

Range("A2:D", new1).Select ' Here I am getting error. Want to select the range from active cell till the number of rows mentioned in the cell value of F2 in EmpList sheet.

End If
End With

End Sub
 
Upvote 0
I googled and found below code to copy number of rows based on cell value. I haven't tried, however in this code, the cell value in the same worksheet whereas my cell value in different worksheet. Can anyone please help to modify the code for me?

Sub CopyData()
Dim rngSinglecell As Range
Dim rngQuantityCells As Range
Dim intCount As Integer
Dim ws1 As Worksheet
Dim name_ws As String
Dim lastRow As Long, lastRow2 As Long

name_ws = "Sheet1" '<--- put name of your main worksheet

Set ws1 = ThisWorkbook.Sheets(name_ws)
With ws1

lastRow = .Cells(Rows.Count, 3).End(xlUp).Row
Set rngQuantityCells = .Range("C2:C" & lastRow)

For Each rngSinglecell In rngQuantityCells
If IsNumeric(rngSinglecell.Value) Then

If rngSinglecell.Value > 0 Then
For intCount = 1 To rngSinglecell.Value
lastRow2 = ThisWorkbook.Sheets("Feuil2").Cells(Rows.Count, 3).End(xlUp).Row + 1
.Rows(rngSinglecell.Row).EntireRow.Copy ThisWorkbook.Sheets("Feuil2").Rows(lastRow2)
Next
End If

End If
Next

End With

End Sub
-----------------------Or ---------------------------------------------

Sub CopyRowsFromColumnN()

Dim rng As Range
Dim r As Range
Dim numberOfCopies As Integer
Dim n As Integer

'## Define a range to represent ALL the data
Set rng = Range("A1", Range("N1").End(xlDown))

'## Iterate each row in that data range
For Each r In rng.Rows
'## Get the number of copies specified in column 14 ("N")
numberOfCopies = r.Cells(1, 14).Value

'## If that number > 1 then make copies on a new sheet
If numberOfCopies > 1 Then
'## Add a new sheet
With Sheets.Add
'## copy the row and paste repeatedly in this loop
For n = 1 To numberOfCopies
r.Copy .Range("A" & n)
Next
End With
End If
Next

End Sub
 
Upvote 0
Hello
1) try to wrap vba code in side the vba tag like this
VBA Code:
This is VBA code
2) to get fast answers, try to attch a mini sheet to help helpers prevent manual input data to test.
 
Upvote 0
Dear Sir,

Apologies for the late reply. Please find below codes wrapped as vba tag

below code which I have prepared
VBA Code:
Sub CopyRows()

Dim rng As Range
Dim r As Range
Dim n1, n2 As Integer
Dim n As Integer
Dim i As Long

new1 = Sheets("EmpList").Range("F2").Value
new2 = Sheets("EmpList").Range("G2").Value

With Sheets("Process1")
'Dim MRng As Range
On Error Resume Next
Sheets(1).ShowAllData
On Error GoTo 0

If new1 > 0 Then
Sheets("Process1").Activate
Range("A1:D1").AutoFilter
MRng = Sheet1.Range("A1048576").End(xlUp).Row

Range("C1").Select
Range("$A$1:$D" & MRng).AutoFilter Field:=4, Criteria1:="Pending"
Range("$A$1:$D" & MRng).Select

ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.EntireRow.Hidden = False
ActiveCell.Offset(1, 0).Select
Loop

Range("A2:D", new1).Select ' Here I am getting error. Want to select the range from active cell till the number of rows mentioned in the cell value of F2 in EmpList sheet.

End If
End With

End Sub[/SIZE]

What exactly I require is...

1. Filter the case data from Process1 & Process2 sheets with criteria "Pending" from column E "Case Status"
2. Check the number from i2 column in EmpList sheet and select the number of rows from Process1 sheet. Copy those rows data and paste them into Database sheet.
3. check the numner from j2 column in EmpList sheet and select the number of rows from Process2 sheet. Copy those rows data and paste them into database sheet.
4. for an example - i2 = 1 hence select one row from Process1 sheet and copy paste it into Database sheet. then j2 = 4 hence select 4 rows from Process2 sheet and copy paste them into database sheet.
5. update the Employee data from A2:H2 in all these 5 rows data in Database sheet. It seems that these cases have been assigned to this employee.
6. After this, macro should move to next employee in EmpList and copy raws from process1 & process2 sheet as per the numbers updated in i and j column.
7 Do this activity until last employee in the sheet.

Below are Excel-Mini sheet uploaded for all these 4 worksheets.

Test Allocation.xlsx
ABCDE
1Process NameCase IDCase DateDescriptionCase Status
2Process12270115-09-2022XYZAssigned
3Process13560315-09-2022XYZAssigned
4Process14217915-09-2022XYZPending
5Process13474115-09-2022XYZPending
6Process13775215-09-2022XYZPending
7Process11286015-09-2022XYZPending
8Process13057415-09-2022XYZPending
9Process12581415-09-2022XYZPending
10Process14228315-09-2022XYZPending
11Process11586115-09-2022XYZPending
12Process13024915-09-2022XYZPending
13Process11006715-09-2022XYZPending
14Process13371215-09-2022XYZPending
15Process11336215-09-2022XYZPending
Process1


Test Allocation.xlsx
ABCDE
1Process NameCase IDCase DateDescriptionCase Status
2Process22758015-09-2022XYZAssigned
3Process22649415-09-2022XYZAssigned
4Process21612115-09-2022XYZAssigned
5Process21858415-09-2022XYZAssigned
6Process22779315-09-2022XYZPending
7Process24951415-09-2022XYZPending
8Process21895615-09-2022XYZPending
9Process23637615-09-2022XYZPending
10Process23470415-09-2022XYZPending
11Process22247715-09-2022XYZPending
12Process23203715-09-2022XYZPending
13Process22133115-09-2022XYZPending
14Process24490615-09-2022XYZPending
15Process23643215-09-2022XYZPending
Process2


Test Allocation.xlsx
ABCDEFGHIJK
1Emp_IDEmp_NameProcess_LeadProcess_ManagerSkillSiteLocationEmp_EmailProcess1Process2Case_Allocation_Status
228193ADFKADFKXYZABCFTCZHomeadfkadfk@xyz.com14Pending
313650DFAFDEXYZABCFTCZHomedfafde@xyz.com22Pending
496966ETSDAGAXYZABCFTCZHomeetsdaga@xyz.com31Pending
529320CASDERASXYZABCFTCZHomecasderas@xyz.com60Pending
695333CADEDREXYZABCFTCZHomecadedre@xyz.com03Pending
EmpList


Test Allocation.xlsx
ABCDEFGHIJKL
1Process NameCase IDCase DateDescriptionEmp_IDEmp_NameProcess_LeadProcess_ManagerSkillSiteLocationEmp_Email
2
3
4
5
Database
 
Upvote 0
I could end up my code as below, but stuck at one line as getting error of
Run-time error: '1004':
Method 'Range' of object '_Global' failed

VBA Code:
Sub CopyRows()

Dim rng As Range

Dim r As Range
Dim n1, n2 As Integer
Dim n As Integer
Dim i As Long
Dim XCell As String
Dim new1 As Integer

 new1 = Sheets("EmpList").Range("F2").Value
 new2 = Sheets("EmpList").Range("G2").Value

With Sheets("Process1")
'Dim MRng As Range
    On Error Resume Next
    Sheets("Process1").ShowAllData
    On Error GoTo 0
   
If new1 > 0 Then
Sheets("Process1").Activate
Range("A1:D1").AutoFilter
MRng = Sheet1.Range("A1048576").End(xlUp).Row

    Range("C1").Select
    Range("$A$1:$D" & MRng).AutoFilter Field:=4, Criteria1:="Pending"
    Range("$A$1:$D" & MRng).Select
   
    ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.EntireRow.Hidden = False
        ActiveCell.Offset(1, 0).Select
Loop
    XCell = ActiveCell
   
    Range(XCell & ":D", new1).Select
       
End If
End With

End Sub
 
Upvote 0
I found below code that works as per my need, however it selects the continuous rows whereas I want to select the dynamic range from filtered rows. Please if anyone can help would be much appreciated.

VBA Code:
Sub DynamicRangeBasedOnCellValue()
Dim DValue As Variant
Dim DRange As Range
DValue = ActiveSheet.Range("F2").Value ' F2 cell value is 3
Set DRange = ActiveSheet.Range("A1:B" & DValue)
DRange.Select
End Sub
 
Upvote 0
Your code does not help us much to understand what you want to have in output sheet.
Cells refetence in code does not match with cells reference in sample worksheet
For instant,
VBA Code:
Dim new1 As Integer
 new1 = Sheets("EmpList").Range("F2").Value
but cell F2 contains textstring "CZ" (maybe you means range I2?)
Also, sheet Process 1, "pending" is in column E, but your code try to filter column D:
VBA Code:
    Range("$A$1:$D" & MRng).AutoFilter Field:=4, Criteria1:="Pending"

It may help a lot if you could "try to manual input the expected output results (in sheet Database?)"
 
Upvote 0
It took a lot of search and R&D and finally I could solve my issue to select the rows based on the numbers mentioned in the range.
My apologies to giving incorrect reference to get row count i.e. column "F". Ideally it should refer the column "I"
Below is my updated code with resolution: :)

VBA Code:
Sub CopyRows()

Dim rng As Range

Dim r As Range
Dim n1, n2 As Integer
Dim n As Integer
Dim i As Long
Dim XCell As String
Dim new1 As Integer

 new1 = Sheets("EmpList").Range("I2").Value     ' Apologies to give incorrect reference in earlier code
 
With Sheets("Process1")
'Dim MRng As Range
    On Error Resume Next
    Sheets("Process1").ShowAllData
    On Error GoTo 0
  
If new1 = 0 Then
Exit Sub
Else
Sheets("Process1").Activate
Range("A1:E1").AutoFilter
MRng = Sheet1.Range("A1048576").End(xlUp).Row

    Range("C1").Select
    Range("$A$1:$E" & MRng).AutoFilter Field:=5, Criteria1:="Pending"
    Range("$A$1:$E" & MRng).Select
  
    ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.EntireRow.Hidden = False
        ActiveCell.Offset(1, 0).Select
Loop
    ActiveCell.Resize(new1, 6).Select  'here i could select the number of rows mentioned in the "I" column
    Selection.Copy
    Sheets("Database").Select
    Range("A1048576").End(xlUp).Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
      
End If
End With

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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