Conditional Loop Not Pasting As Desired

ExcelRalph

New Member
Joined
Jan 19, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm attempting to copy fields from two worksheets if a condition is met and paste into defined columns of a third worksheet "Sheet6".

The code correctly identifies the fields, however it appears that the loop resets after the a condition is not met. This seems to result in non consecutive rows on the third worksheet. I don't want any gaps in rows on "Sheet6".
Can you help? Or provide a more efficient code? Thank you!
GRE Excel Help.xlsm
ABCDEFGHIJKLMNOPQRS
14Country CodeMACAccountAccountNumberCPAOriginal CPAPIDProduct ClustersGSFA Customer IDCurrencyProduct clusters to preserve (Rates and all other attributes)Source PIDEffective DateDivisorPayment TermsBilling CycleOMS Offer Required (Y/N)OfferIDSpecial Instructions/Notes
15(Sheet5)(Sheet5)(Sheet5)(Sheet5)117167#N/ANew1Product Clusters ABCDNoneN/A5/1/2021Non-Standard45WeeklyY15230-01Example, No KAP action/push through to OMS.
Sheet2


GRE Excel Help.xlsm
ABCDEFGHIJK
1CountryMACAccount NameAccount NumberCPAPIDProduct ClustersGSFA Customer IDCurrencyIn Scope?Note
2US428Customer DCA2678651995544PUD00ORELProductsABCTXNMC771602212No
3US428Customer ABC756877776117167USD00OSNGTD3rd;TDDom_Territories;TDExport;TDImportUSMC796692175USD
4US428Customer DCA796692175995544USD00OSNGTD3rd;TDDom_Territories;TDExport;TDImportUSMC796692175USD
5US428Customer ABC845830910117167USD00OSNGTD3rd;TDDom_Territories;TDExport;TDImportUSMC854198166USDNo
6US428Customer DCA7654954117167PUD00OSNGProductsABCPRMC854198166USD
7US428Customer ABC8458309117167PUD00OSNGProductsABCPRMC854198166USDNo
8US428Customer ABC8461431117167PUD00OSNGProductsABCPRMC771602212USDNo
9
GRE As Is


GRE Excel Help.xlsm
ABCDEFGHIJKLMNOPQRS
2CountryMACAccount NameAccount NumberCPAOriginal CPAPIDProduct ClustersGSFA Customer IDCurrencyProduct clusters to preserve (Rates and all other attributes)Source PIDEffective DateDivisorPayment TermsBilling CycleOMS Offer Required (Y/N)OfferIDSpecial Instructions/Notes
3US428Customer ABC756877776117167117167New1Product Clusters ABCDUSMC796692175NoneN/A5/1/2021Non-Standard45WeeklyY15230-01Example, No KAP action/push through to OMS.
4US428Customer DCA796692175117167995544New1Product Clusters ABCDUSMC796692175NoneN/A5/1/2021Non-Standard45WeeklyY15230-01Example, No KAP action/push through to OMS.
5
6US428Customer DCA7654954117167117167New1Product Clusters ABCDPRMC854198166NoneN/A5/1/2021Non-Standard45WeeklyY15230-01Example, No KAP action/push through to OMS.
7
8
GRE To Be
 

Attachments

  • CurrentVBA.png
    CurrentVBA.png
    13.1 KB · Views: 4

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
can you post your code with code tags not as picture, I could make a simple chage to get your code to work, but I don't want to have to retype all of it. To do this select your code in excel and copy it then select the vba symbol in edit box on this website and paste your code.
 
Upvote 0
Hi, and thank you for your offer to help. Also just to note my Sheet5 = GRE As Is and Sheet6 = GRE To Be.


VBA Code:
Sub GRE_To_Be_Section1()
    Dim UsdRws As Long, i As Long
    Dim CPA As Long
    
    
    
    
    
        CPA = Worksheets("Sheet2").Range("E15").Value
        UsdRws = Sheet5.Cells(Rows.Count, 1).End(xlUp).Row
        For i = 2 To UsdRws
            If Sheet5.Range("J" & i).Value = "" And Sheet5.Range("E" & i).Value = CPA Then
                Sheet5.Range("A" & i).Resize(, 4).Copy Sheet6.Cells(i, 1)
                Sheet5.Range("E" & i).Resize(, 1).Copy Sheet6.Cells(i, 6)
                Worksheets("Sheet2").Range("E15").Resize(, 1).Copy Sheet6.Cells(i, 5)
                Worksheets("Sheet2").Range("G15").Resize(, 13).Copy Sheet6.Cells(i, 7)
                Sheet5.Range("H" & i).Resize(, 1).Copy Sheet6.Cells(i, 9)
                              
            End If
        Next i
       

End Sub
 
Upvote 0
Thanks for the code, here is the quick modification get rid of the blanks row:
VBA Code:
Sub GRE_To_Be_Section1()
    Dim UsdRws As Long, i As Long
    Dim CPA As Long
    Dim indi As Long
    
    
    
    
        CPA = Worksheets("Sheet2").Range("E15").Value
        UsdRws = Sheet5.Cells(Rows.Count, 1).End(xlUp).Row
        indi = 1
        For i = 2 To UsdRws
            If Sheet5.Range("J" & i).Value = "" And Sheet5.Range("E" & i).Value = CPA Then
                Sheet5.Range("A" & i).Resize(, 4).Copy Sheet6.Cells(indi, 1)
                Sheet5.Range("E" & i).Resize(, 1).Copy Sheet6.Cells(indi, 6)
                Worksheets("Sheet2").Range("E15").Resize(, 1).Copy Sheet6.Cells(indi, 5)
                Worksheets("Sheet2").Range("G15").Resize(, 13).Copy Sheet6.Cells(indi, 7)
                Sheet5.Range("H" & i).Resize(, 1).Copy Sheet6.Cells(indi, 9)
                indi = indi + 1
            End If
        Next i
       

End Sub
You can make the code much much faster if you have a large number of rows to process. This is by loading all of sheet 6 into a variant array, define an output array and then run the loop entirely in memory this will be about 1000 times faster. You can't use the resize method so you would have to create some additional loops. Then finally write the output array out to the worksheet
 
Upvote 0
Solution
Thank you very much!

Since I was using a "Header Row" in row 2 of Sheet6, I modified the indi = 3 and it works perfectly.

Noted on the speed available with the array method and I will explore further. The final Sheet6 at times will have two sections so my focus had been on the top section.

Thank you again!
 
Upvote 0

Forum statistics

Threads
1,207,089
Messages
6,076,517
Members
446,211
Latest member
b306750

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