UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> I hope people work on Sunday...Excel For...Next    
 
   
ian_john_walker
post Mar 2 2008, 04:29 AM
Post #1

UtterAccess Addict
Posts: 270
From: Southampton, UK



Morning Chaps,

A tricky one for me, but probably not you guys!

I have a worksheet which contains rows of data, which are transferred onto a form and printed. The trick is there could be anything from 1 to 50 rows and any record with a Y in the end column needs to be copied and pasted, then printed and repeat...

I am trying to use For...Next without much success, can anyone help?

My code I have so far is:

For counter = 1 To 10 'changed to 10 from 50 to reduce time!

If Range("I12") = "Y" Then
Range("L12:O12").Select
Selection.Copy
Sheets("STF (linked)").Select
Range("R5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Sheets("STF linked master").Select

End If

Next counter

This sort of works but copies the same row repeatedly, I don't know how to move it onto the next row? Can anyone help?

Ian
Go to the top of the page
 
+
argeedblu
post Mar 2 2008, 04:59 AM
Post #2

UA Forum + Wiki Administrator
Posts: 11,954
From: Sudbury, Ontario, Canada



Ian,

I don't regularly work with Excel VBA but I think this solution should work for you. The problem is that you are refering to a specific cell and range in the second and third statements. (I12, L12:O12). You need to change those references on each iteration of the loop.

The first cell you want to check appears to be 11 greater than the index counter. So you need to calculate the Range by adding 11 to the counter. Try this:

If Range("I" & counter + 11) = "Y" then
Range ("L" & counter +11 & ":"O" & counter + 11).Select

Obviously, you would have to do a similar calculation to specify the destination range, unless you want to always want to paste to exactly the same destination.

Range("R" & counter + 4).Select

Glenn
Go to the top of the page
 
+
ian_john_walker
post Mar 2 2008, 05:19 AM
Post #3

UtterAccess Addict
Posts: 270
From: Southampton, UK



Morning Glenn,

This is brilliant, thank you! I had an idea this is where I was going wrong but just couldn't sort it!

Thanks for your help!

Ian
Go to the top of the page
 
+
argeedblu
post Mar 2 2008, 05:21 AM
Post #4

UA Forum + Wiki Administrator
Posts: 11,954
From: Sudbury, Ontario, Canada



You are very welcome, Ian.

Glenn
Go to the top of the page
 
+
norie
post Mar 2 2008, 09:05 AM
Post #5

UtterAccess VIP
Posts: 4,296



Try this.
CODE
For counter = 1 To 10 'changed to 10 from 50 to reduce time!

     Set rng =Sheets("STF linked master").Range("I" & counter)
     If rng.Value = "Y" Then
          rng.Resize(,4).Copy
          With Sheets("STF (linked)")
                .Range("R5").PasteSpecial Paste:=xlPasteValues
                .PrintOut Copies:=1, Collate:=True
          End With
    End If

Next counter
Go to the top of the page
 
+
argeedblu
post Mar 3 2008, 05:15 AM
Post #6

UA Forum + Wiki Administrator
Posts: 11,954
From: Sudbury, Ontario, Canada



Hmmm.

I think your code would need to be more like:
CODE
Dim counter As Integer
Dim rng As Range
    For counter = 1 To 10    'changed to 10 from 50 to reduce time!
        Set rng = Sheets("STF linked master").Range("I" & counter + 11)
        If rng.Value = "Y" Then
            'rng.Resize(, 4).Copy
            Set rng = Sheets("STF linked master").Range("L" & counter + 11 & ":" & "O" & counter + 11)
            rng.Copy
            With Sheets("STF (linked)")
                .Range("R5").PasteSpecial Paste:=xlPasteValues
                '.PrintOut Copies:=1, Collate:=True
            End With
        End If
    Next counter
End Sub


to meet Ian's original specification, Norie.

Glenn
Go to the top of the page
 
+
norie
post Mar 3 2008, 09:38 AM
Post #7

UtterAccess VIP
Posts: 4,296



Glenn

Oops!

Missed the +11 but I still think the resize is a little less cumbersome than concatenating to create the range to copy.
Go to the top of the page
 
+
ian_john_walker
post Mar 3 2008, 09:44 AM
Post #8

UtterAccess Addict
Posts: 270
From: Southampton, UK



Thanks both, the original suggestion was much easier and even I can almost understand it!!!

Regards

Ian
Go to the top of the page
 
+
KingMartin
post Mar 3 2008, 10:02 AM
Post #9

Retired Moderator
Posts: 10,959
From: Prague,CZ / Kiev,UA



Hi guys,

I know the issue has been solved, but we don't need to copy-paste, do we? (IMG:http://www.utteraccess.com/forum/style_emoticons/default/sad.gif)

CODE
Const HOW_MANY_ROWS As Long = 10
Dim cl As Range
For Each cl In Sheets("STF linked master").Range("I12").Resize(HOW_MANY_ROWS)
    If cl.Value = "Y" Then
        Sheets("STF (linked)").Range("R5").Resize(4).Value = _
                Application.Transpose(cl.Offset(, 3).Resize(, 4))
            '.PrintOut Copies:=1, Collate:=True
    End If
Next
Go to the top of the page
 
+
norie
post Mar 3 2008, 11:11 AM
Post #10

UtterAccess VIP
Posts: 4,296



Martin

Why not? :(

I like copying and pasting, it's fun. (IMG:http://www.utteraccess.com/forum/style_emoticons/default/wink.gif)
Go to the top of the page
 
+
argeedblu
post Mar 3 2008, 11:25 AM
Post #11

UA Forum + Wiki Administrator
Posts: 11,954
From: Sudbury, Ontario, Canada



Resizing didn't include the cells the op requires (columns L - O). Resizing would include I - L, wouldn't it?

Glenn
Go to the top of the page
 
+
norie
post Mar 3 2008, 11:29 AM
Post #12

UtterAccess VIP
Posts: 4,296



Glenn

That's right, especially without the +11 which I missed.(IMG:http://www.utteraccess.com/forum/style_emoticons/default/dazed.gif)

Oops, and another thing I missed - a little bit of Offset wouldn't do any harm.

That's what you get for writing air code and not testing it.



Edited by: norie on Mon Mar 3 11:31:05 EST 2008.
Go to the top of the page
 
+
KingMartin
post Mar 3 2008, 12:40 PM
Post #13

Retired Moderator
Posts: 10,959
From: Prague,CZ / Kiev,UA



Hi Glenn,

sorry for bringing even more confusion into this thread, but your question is directed at me? (IMG:http://www.utteraccess.com/forum/style_emoticons/default/sad.gif)

This:

cl.Offset(, 3).Resize(, 4))

will return the range the OP wants, I think... (IMG:http://www.utteraccess.com/forum/style_emoticons/default/dazed.gif) (IMG:http://www.utteraccess.com/forum/style_emoticons/default/sad.gif)
Go to the top of the page
 
+
argeedblu
post Mar 3 2008, 01:19 PM
Post #14

UA Forum + Wiki Administrator
Posts: 11,954
From: Sudbury, Ontario, Canada



No, Martin,

My reply was to Norie.

Your code appears to better Excel VBA than mine.

Glenn
Go to the top of the page
 
+
ian_john_walker
post Mar 3 2008, 01:32 PM
Post #15

UtterAccess Addict
Posts: 270
From: Southampton, UK



Now I'm really confused!! I think you've solved a problem which didn't exist!!

I'll leave you three to fight out for the resolution in just one line of code!!

Regards

Ian
Go to the top of the page
 
+
KingMartin
post Mar 3 2008, 01:34 PM
Post #16

Retired Moderator
Posts: 10,959
From: Prague,CZ / Kiev,UA



QUOTE
Your code appears to better Excel VBA than mine.

Well... (IMG:http://www.utteraccess.com/forum/style_emoticons/default/blush.gif)

I just don't like to leave sheet in copy mode and have data on clipboard...

Range2.Value = Range1.Value

is quite fast and elegant way of how to copy values (IMG:http://www.utteraccess.com/forum/style_emoticons/default/wink.gif)

Martin
Go to the top of the page
 
+
KingMartin
post Mar 3 2008, 01:37 PM
Post #17

Retired Moderator
Posts: 10,959
From: Prague,CZ / Kiev,UA



Hello Ian,

my first contribution to this thread was introduced by "I know the issue has been solved".

What we are discussing is rather "best practice". If the first code supplied by Glenn works for you, rejoice (IMG:http://www.utteraccess.com/forum/style_emoticons/default/wink.gif)

However, you may be interested in alternative coding, as norie and me posted... that's all... (IMG:http://www.utteraccess.com/forum/style_emoticons/default/sad.gif)

Martin
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 23rd May 2013 - 06:14 AM