VB Code for equal assignment

MaheshBabu

New Member
Joined
Nov 5, 2013
Messages
7
Hi Every1,

I have 3 columns in an excel sheet (ID, Signed by & To be checked), and in another sheet, I have names under a column "Checkers".

I need a macro where in if I run it "To be checked" column in sheet 1 should be equally assigned to the "Checkers" in sheet 2 and the rest should be blanks.

Some1 help me with the macro.

Please find the below sample:

Before Macro:

[TABLE="width: 241"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]ID[/TD]
[TD]Signed by[/TD]
[TD]To be Checked[/TD]
[/TR]
[TR]
[TD]121[/TD]
[TD]AAA[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]BBB[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]124[/TD]
[TD]CCC[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]125[/TD]
[TD]AAA[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]126[/TD]
[TD]BBB[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]127[/TD]
[TD]CCC[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]128[/TD]
[TD]DDD[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]129[/TD]
[TD]AAA[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]130[/TD]
[TD]EEE[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]131[/TD]
[TD]DDD[/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]

After Macro :

[TABLE="width: 241"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Loan #[/TD]
[TD]Signed by[/TD]
[TD]To be Checked[/TD]
[/TR]
[TR]
[TD]121[/TD]
[TD]AAA[/TD]
[TD]Don[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]BBB[/TD]
[TD]Don[/TD]
[/TR]
[TR]
[TD]124[/TD]
[TD]CCC[/TD]
[TD]Don[/TD]
[/TR]
[TR]
[TD]125[/TD]
[TD]AAA[/TD]
[TD]Adam[/TD]
[/TR]
[TR]
[TD]126[/TD]
[TD]BBB[/TD]
[TD]Adam[/TD]
[/TR]
[TR]
[TD]127[/TD]
[TD]CCC[/TD]
[TD]Adam[/TD]
[/TR]
[TR]
[TD]128[/TD]
[TD]DDD[/TD]
[TD]Rose[/TD]
[/TR]
[TR]
[TD]129[/TD]
[TD]AAA[/TD]
[TD]Rose[/TD]
[/TR]
[TR]
[TD]130[/TD]
[TD]EEE[/TD]
[TD]Rose[/TD]
[/TR]
[TR]
[TD]131[/TD]
[TD]DDD[/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]
 
I have modified hiker95's code slightly to adjust with change in data. Replace previous code with this one.
Code:
Option Explicit
Sub AssignCheckers()
' hiker95, 11/06/2013
' edited by taurean, 11/07/2013
' http://www.mrexcel.com/forum/excel-questions/737261-vbulletin-code-equal-assignment.html
Dim a As Variant, c As Variant
Dim i As Long, n As Long
With Sheets("Sheet2")
  c = .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row)
End With
With Sheets("Sheet1")
  a = .Range("A2:C" & .Range("A" & Rows.Count).End(xlUp).Row)
  n = Application.Floor(UBound(a, 1) / UBound(c, 1), 1)
  For i = LBound(c, 1) To UBound(c, 1)
    .Cells((i - 1) * n + 2, "C").Resize(n, 1).Value = c(i, 1)
  Next i
End With
End Sub
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
taurean,

Very nicely done - thanks.

I was trying to resolve the present task by using arrays in memory, and, not writing directly to the worksheet.

Thanks again.
 
Upvote 0
I was trying to resolve the present task by using arrays in memory, and, not writing directly to the worksheet.
Something like this maybe...

Code:
Sub DistributeCheckers()
  Dim X As Long, Z As Long, PerChecker As Long, IDs As Variant, Checkers As Variant, OutArr As Variant
  IDs = Sheets("Sheet1").Range("A2:A" & Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row)
  Checkers = Sheets("Sheet2").Range("A2:A" & Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row)
  PerChecker = Int(UBound(IDs) / UBound(Checkers))
  ReDim OutArr(1 To PerChecker * UBound(Checkers), 1 To 1)
  For X = 1 To PerChecker * UBound(Checkers) Step PerChecker
    For Z = 1 To PerChecker
      OutArr(X + Z - 1, 1) = Checkers(1 + ((X - 1) / PerChecker), 1)
    Next
  Next
  Sheets("Sheet1").Range("C2").Resize(UBound(OutArr)) = OutArr
End Sub
 
Upvote 0
Rick Rothstein,

Thanks again.


Rick Rothstein, taurean,

i want a code in such a way that if my ID's and No. of Checkers are not constant(It may increase/decrease) the ID's in sheet 1 should be equally assigned to the checkers in a given range of cells be it 'sheet2' are any where in the excel....and the rest of the rows should be blank....

Per MaheshBabu in reply #7.

If we play with the number of items in both column A's, both macros may fail.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,111
Messages
6,170,160
Members
452,305
Latest member
chenhi131

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