Can i use loop? or how can i fulfill this requirement? Pls help!

htoh2

New Member
Joined
Oct 20, 2010
Messages
25
Greetings to all experts,

I am a novice in ms excel macro and currently i've met some difficulties in coding to meet my requirement. So, i would like to seek advices from the experts here.

In my SourceFile.xls, i have 2 columns with values as belows:

UserID CompanyCode
Helen ABC
Peter ABC
Mary CDE
Allan EFG

And in my DestinationFile.xls, i have 2 columns with values as belows:

New User ID CompanyCode



And previously, thanks to help from an expert, i'm able to fulfill the requirement :

When you filter in SourceFile.xls e.g. Company Code ="ABC" , the values(Helen,Peter) who are from Company Code = "ABC" will be stored in the DestinationFile.xls as below:


New User ID CompanyCode
Helen ABC
Peter ABC


:eek: Hence, now i have a requirement such that:

After copying the values for Company Code = "ABC" from SourceFile.xls to DestinationFile.xls, it will check the next cell in SourceFile.xlsfor a different company code for e.g Company Code = "CDE" so that it will copy the value into the DestinationFile.xls (as below),and so on.

New User ID CompanyCode
Mary CDE


Your help will be greatly appreciated!
Feel free to clear your doubts if there is any!
Thanks!



Best Regards,
htoh2:)
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi, thank you for your response. :)

The following is my current codes:


Sub Test()
Dim lr As Long
Dim dr As String
lr = Workbooks("SourceFile.xls").Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row
'
Workbooks("SourceFile.xls").Activate
With Sheets("Sheet1").Columns("A:B")

.AutoFilter
.AutoFilter Field:=2, Criteria1:="ABC"
Range("A2:B" & lr).Copy
End With
Workbooks("DestinationFile.xls").Activate
Sheets("Sheet1").Range("A2").Select
ActiveSheet.Paste
dr = Join(Array(Range("A2") & ", " & Range("A3") & ", " & Range("A4") & ", " & Range("A5")))
Range("A1").Value = dr

Rows("1:1").Insert Shift:=xlDown
Range("A1").Formula = "New UserID"
Range("B1").Formula = "Company"
Range("B2").Value = Range("B3").Value

Do While Range("A3").Value <> ""
Range("A3").EntireRow.Delete Shift:=xlUp
Loop
Windows("SourceFile.xls").Activate
Cells.AutoFilter
End Sub


Best regards,
htoh2
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,935
Members
449,195
Latest member
Stevenciu

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