Cycling through a For Loop and copying cell to another sheet then running a subroutine

orangebloss

New Member
Joined
Jun 5, 2013
Messages
43
Hi

I have a project list that I need to cycle through and copy a project name into another sheet, run a subroutine, then return to the original list on the next row down and continue.

1.Go to first cell of project list
2.If the value in the first cell of the row is Yes then paste the value into the Rough Cut worksheet in cell V4
3.Run the UpdateSandbox macro which copies data from the Rough Cut spreadsheet to the Sandbox Spreadsheet
4.Return to the Project List, move one cell down and repeat
5.If the value of the first cell of the row in No then go to the next row and start from 2 again

The subroutine works, but I can't get it to go to the next line to check it - any thoughts or ideas?

VBA Code:
 Sub Cyclethrough()
      Dim x As Integer
       Dim RoughCut As Worksheet
       Dim ProjectList As Worksheet
       Set RoughCut = Worksheets("Rough Cut")
       Set ProjectList = Worksheets("Project Data (G)")
       
             
      ' Set numrows = number of rows of data.
      NumRows = ProjectList.Range("A4", Range("A4").End(xlDown)).Rows.Count
      ' Select cell a1.
      Range("A4").Select
      ' Establish "For" loop to loop "numrows" number of times.
      For x = 1 To NumRows
         If ActiveCell.Value = "Yes" Then
        'If the value in the first cell of the row is Yes then paste the value into the Rough Cut worksheet in cell V4             
         
          
         Application.ScreenUpdating = False
          RoughCut.Range("V4").Value = ActiveCell.Offset(0, 1).Value
  Call UpdateSandbox
  
 ActiveCell.Offset(1, 0).Select

 
  End If
  
    Next
     
   End Sub
 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,490
Office Version
  1. 2019
Platform
  1. Windows
Hi,
its very rare that you need to use Select in code
See if this very quick update to your code does what you want

VBA Code:
Sub Cyclethrough()
       Dim rngData As Range, cell As Range
       Dim RoughCut As Worksheet, ProjectList As Worksheet
      
       Set RoughCut = Worksheets("Rough Cut")
       Set ProjectList = Worksheets("Project Data (G)")
      
       'size range data in Project list
       With ProjectList
        Set rngData = .Range(.Range("A4"), .Range("A" & .Rows.Count).End(xlUp))
       End With
    
      ' Establish "For" loop.
      For Each cell In rngData.Cells
         If cell.Value = "Yes" Then
            'If the value in the first cell of the row is Yes then
            'paste the value into the Rough Cut worksheet in cell V4
            RoughCut.Range("V4").Value = cell.Offset(0, 1).Value
           
            Call UpdateSandbox
         End If
      Next cell

   End Sub

but there may be a much cleaner solution that could be provided by someone here if you want to share all your codes & what you want to do.

Hope helpful

Dave
 
Solution

orangebloss

New Member
Joined
Jun 5, 2013
Messages
43
Sub Cyclethrough() Dim rngData As Range, cell As Range Dim RoughCut As Worksheet, ProjectList As Worksheet Set RoughCut = Worksheets("Rough Cut") Set ProjectList = Worksheets("Project Data (G)") 'size range data in Project list With ProjectList Set rngData = .Range(.Range("A4"), .Range("A" & .Rows.Count).End(xlUp)) End With ' Establish "For" loop. For Each cell In rngData.Cells If cell.Value = "Yes" Then 'If the value in the first cell of the row is Yes then 'paste the value into the Rough Cut worksheet in cell V4 RoughCut.Range("V4").Value = cell.Offset(0, 1).Value Call UpdateSandbox End If Next cell End Sub
Thanks

It's still going through the same and is only copying the first entry multiple times - it never drops to the row below, it's almost as if it resets the Activecell to the first cell? (A4)
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,490
Office Version
  1. 2019
Platform
  1. Windows
Based on what you posted, Updated Code loops through each cell in range on worksheet project list & if value = "Yes" it will copy the offset value (Column B) to worksheet Rough Cut Range("V4") where your unpublished code UpdateSandbox does something?

If this is not what you want then it would be helpful if you publish all your codes & copy of spreadsheet using MrExcel Addin XL2BB - Excel Range to BBCode

Dave
 

orangebloss

New Member
Joined
Jun 5, 2013
Messages
43

ADVERTISEMENT

Hi,
its very rare that you need to use Select in code
See if this very quick update to your code does what you want

VBA Code:
Sub Cyclethrough()
       Dim rngData As Range, cell As Range
       Dim RoughCut As Worksheet, ProjectList As Worksheet
     
       Set RoughCut = Worksheets("Rough Cut")
       Set ProjectList = Worksheets("Project Data (G)")
     
       'size range data in Project list
       With ProjectList
        Set rngData = .Range(.Range("A4"), .Range("A" & .Rows.Count).End(xlUp))
       End With
   
      ' Establish "For" loop.
      For Each cell In rngData.Cells
         If cell.Value = "Yes" Then
            'If the value in the first cell of the row is Yes then
            'paste the value into the Rough Cut worksheet in cell V4
            RoughCut.Range("V4").Value = cell.Offset(0, 1).Value
          
            Call UpdateSandbox
         End If
      Next cell

   End Sub

but there may be a much cleaner solution that could be provided by someone here if you want to share all your codes & what you want to do.

Hope helpful

Dave
I've been an idiot - it's working now I've realised the value has to go in V2 not 4 lol but it's entering the first lot of data twice - so I get 2x ABC entries and one GHI (If column A is No it should just go to the next one not repeat the first?)
Include (Cell A3)Project (Cell B3)
YesABC
NoDEF
YesGHI
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,490
Office Version
  1. 2019
Platform
  1. Windows
You need to publish the UpdateSandbox code & copy of the worksheet would be helpful

Dave
 

orangebloss

New Member
Joined
Jun 5, 2013
Messages
43
Based on what you posted, Updated Code loops through each cell in range on worksheet project list & if value = "Yes" it will copy the offset value (Column B) to worksheet Rough Cut Range("V4") where your unpublished code UpdateSandbox does something?

If this is not what you want then it would be helpful if you publish all your codes & copy of spreadsheet using MrExcel Addin XL2BB - Excel Range to BBCode

Dave
Ah Solved! I moved the Call to the other side of the if! Thank you!!
 

Watch MrExcel Video

Forum statistics

Threads
1,128,206
Messages
5,629,290
Members
416,384
Latest member
frsamiee

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
Top