loop problem

KatyJohn

New Member
Joined
Jan 25, 2009
Messages
41
Hey Folks

I'm having problems getting the loop below to work. My objective is to go through a series of sheets and once a cell is identified as "Company" another loop fills in information from another page.

Private Sub CommandButtonModHead_Click()

Dim NewOpenFile As String
Dim FromRng, ToRng
Dim i As Integer
Dim x As Integer
NewOpenFile = Range("BG21")

Workbooks(NewOpenFile).Activate
For i = 1 To Workbooks(NewOpenFile).Sheets.Count

If Sheets(i).Range("C3") = "Company" Then



FromRng = Array("D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10")
ToRng = Array("C25", "C26", "C27", "C28", "C29", "C30", "C31", "C32")
For x = LBound(FromRng) To UBound(FromRng)

Workbooks(NewOpenFile).Sheets("Module Number Entry").Range(FromRng(x)).Copy
Workbooks("2009 Quote Form.xlsm").Sheets(i).Range(ToRng(x)).PasteSpecial Paste:=xlPasteValues

Next x

End If

Sheets(i).Range("C3") = " "

Next


End Sub


katyjohn
 

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.
you have found the sheet named company, but are collecting cell info from the active sheet, you need to specify which sheet your info comes from sheets(i)
an alternative is to use for each method, you can then work with the object variable

Code:
for each sht in Workbooks(NewOpenFile).Sheets
      if sht.name = "company" then
         with sht
               ' copy cell info within with block

          end with
          exit for ' if finished, saves looping more sheets
      end if
next
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

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