Question about look up

sey

New Member
Joined
Apr 28, 2011
Messages
35
Hi

I have a question for looping.
This is all my code, and ("eersterij") means firstrow. In first row he find all dimensions. u can see on the first image, firstrow is selected, second row is the row under first row and finally there is a third row under second row. (But there can be more rows)

Here you see my code:

In the images u see:

First he is looking for how many rows there are ( now there are 3 rows).
Then he paste the dimensions from the first row in a template (you can see in the second picture).
Then he paste the template in a worksheet.
Then he cleans the dimensions in the template and he copy this template 2 times more.
You can see in the last picture : 3 rows with dimensions, so 3 pictures of a template.

But what i want is this:
First he copy the dimensions from the first row in the template and paste the template in that other worksheet. ( then he cleans the dimenions in the original template)
THEN he copy the dimensions of the SECOND row and paste them in the original template and paste the template under the second one in a new worksheet.( then he cleans the dimenions in the original template)
THEN the third one.
(IF there are more rows then ofcourse for fourth, fifth... row too.)

The code :
For Each acell In Range("eersterij")
If acell.Value > 0 Then
acell.copy
y = acell.Value / x

I need to write something other in place of ("eersterij") , so he goes to the second row and third row too, without writing this code 3 times.


Code:
Sub copypastelookupalin1()
Dim acell As Range
Dim teller As Integer
Dim y As Double
Dim bcell As Range
Dim counter As Integer

 counter = 0
 teller = 0
' kijken hoeveel keer een template moet gemaakt worden
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell = ActiveCell.Offset(1, 1)

    For Each bcell In Range("Bereik")
        If IsEmpty(bcell) Then
            'bcell = blank
        Else: 'bcell = 1
            counter = counter + 1
            
' maak een cel om te starten
Sheets("kwnie").Activate
Range("D20").Name = "afmt100"
' bereken het aantal cellen die nodig zijn
Z = Range("totalelengte2") / 100
'bereken de schaal
a = Z / 83
' de afstand die 1 cel zal bevatten
Sheets("SETUP").Activate
Range("totalelengte2").Select
x = Range("totalelengte2").Value / Z

For Each acell In Range("eersterij")
If acell.Value > 0 Then
acell.copy
y = acell.Value / x

Sheets("kwnie").Activate
Range("afmt100").Activate
'ActiveCell.Value = "|"
ActiveCell.Offset(0, Round(teller + y / 2)).Select
teller = teller + y
ActiveSheet.PasteSpecial
End If
Next acell
' afbeelding aanpassen aan de schaal
    
  ActiveSheet.Shapes("afbeeldingg").Select
  ActiveSheet.Shapes("afbeeldingg").Delete
  Sheets("stuktekening gordingenang").Select
  ActiveSheet.Shapes("object 1").copy
  Sheets("kwnie").Select
  Range("A24").Select
  ActiveSheet.Paste
  Selection.Name = "afbeeldingg"
  ActiveSheet.Shapes("afbeeldingg").Select
  Application.CutCopyMode = False
  Selection.ShapeRange.LockAspectRatio = msoTrue
  Selection.ShapeRange.ScaleHeight a, msoFalse, msoScaleFromTopLeft
  Selection.ShapeRange.ScaleWidth a, msoFalse, msoScaleFromTopLeft
    
' cellen opmaken voor waarden mooi in te plaatsen
Range("voorbeeld").Select
Selection.copy
Range("invulplaatsen").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
' templates kopieren en onder elkaar polaatsen
            Sheets("kwnie").Activate
            Range("Print_Area").CopyPicture
            Sheets("Stuktekeningtemplate").Activate
            Range("startcel").Select
           
            ActiveCell.Offset(((counter - 1) * 50) + 1, 0).Select
            ActiveSheet.PasteSpecial
                
' cellen legen voor volgende copy
Sheets("kwnie").Activate
Range("invulplaatsen").Select
Selection.ClearContents
     
        End If
    Next bcell
  
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell.FormulaR1C1 = counter
    
End Sub



2q3yalx.jpg
[/IMG]
k2jnm.jpg
[/IMG]hi,
27yq3k1.jpg
[/IMG]
 
The theory behind it is to replace
Code:
For Each acell In Range("eersterij")

We know from the defined name that this will run through S21, T21, U21 ....

Cells is an alternative to Range, but is defined in a different way.. Cells(row number, column number)

S21 would be defined as Cells(21, 19) as it is Row 21, and column S is the 19th column on the sheet (A = 1, B = 2, etc).

Code:
For abc = 19 to 28

means abc = column S, T, U ... to AB

Code:
For xzy = 21 To counter + 20

With 3 rows of data, (rows 21,22 and 23), counter = 3, 20+3 = 23

Can you see the idea behind it now?

Thinking about it logically, I'm fairly sure the correct syntax would be

Code:
Dim xyz As Long, abc As Long
 
For xzy = 21 To counter + 20
For abc = 19 to 28
Set aCell = Sheets("SETUP").Cells(xyz, abc)
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hmmm, oke i understand, (only the +20 isn't that clear, but i think i understand)
But to be sure, can you have a look at my new question named "easy lookup question" or something. Because it's almost the same i want but much easier to understand.




The theory behind it is to replace
Code:
For Each acell In Range("eersterij")

We know from the defined name that this will run through S21, T21, U21 ....

Cells is an alternative to Range, but is defined in a different way.. Cells(row number, column number)

S21 would be defined as Cells(21, 19) as it is Row 21, and column S is the 19th column on the sheet (A = 1, B = 2, etc).

Code:
For abc = 19 to 28

means abc = column S, T, U ... to AB

Code:
For xzy = 21 To counter + 20

With 3 rows of data, (rows 21,22 and 23), counter = 3, 20+3 = 23

Can you see the idea behind it now?

Thinking about it logically, I'm fairly sure the correct syntax would be

Code:
Dim xyz As Long, abc As Long
 
For xzy = 21 To counter + 20
For abc = 19 to 28
Set aCell = Sheets("SETUP").Cells(xyz, abc)
 
Upvote 0
Another way to look at +20.

The row to start at is 21, 20 is the row before 21, so it's +number of rows before row 21, or +21-1
 
Upvote 0
Yeah i thought so. if i paste that code into mine code, i have every time problems with the next and end if...
But thanks anyway for all your help!
Another way to look at +20.

The row to start at is 21, 20 is the row before 21, so it's +number of rows before row 21, or +21-1
 
Upvote 0
I haven't done any testing on this, only edited that method into the code.

Code:
Sub allesin1()
Dim acell As Range
Dim teller As Integer
Dim y As Double
Dim bcell As Range
Dim counter As Integer
 counter = 0
 teller = 0
' kijken hoeveel keer een template moet gemaakt worden
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell = ActiveCell.Offset(1, 1)
    For Each bcell In Range("Bereik")
        If Not (IsEmpty(bcell)) Then counter = counter + 1
 
' maak een cel om te starten
Sheets("kwnie").Activate
Range("D20").Name = "afmt100"
For xyz = 21 To counter + 20
' bereken het aantal cellen die nodig zijn
Z = Range("totalelengte1").Offset(xyz, 0) / 100
'bereken de schaal
a = Z / 83
' de afstand die 1 cel zal bevatten
Sheets("SETUP").Activate
Range("totalelengte1").Offset(xyz, 0).Select
x = Range("totalelengte1").Offset(xyz, 0).Value / Z
 
For abc = 19 To 28
Set acell = Sheets("SETUP").Cells(xyz, abc)
If acell.Value > 0 Then
acell.Copy
y = acell.Value / x
Sheets("kwnie").Activate
Range("afmt100").Activate
'ActiveCell.Value = "|"
ActiveCell.Offset(0, Round(teller + y / 2)).Select
teller = teller + y
ActiveSheet.PasteSpecial
End If
Next abc
' afbeelding aanpassen aan de schaal
 
  ActiveSheet.Shapes("afbeeldingg").Select
  ActiveSheet.Shapes("afbeeldingg").Delete
  Sheets("stuktekening gordingenang").Select
  ActiveSheet.Shapes("object 1").Copy
  Sheets("kwnie").Select
  Range("A24").Select
  ActiveSheet.Paste
  Selection.Name = "afbeeldingg"
  ActiveSheet.Shapes("afbeeldingg").Select
  Application.CutCopyMode = False
  Selection.ShapeRange.LockAspectRatio = msoTrue
  Selection.ShapeRange.ScaleHeight a, msoFalse, msoScaleFromTopLeft
  Selection.ShapeRange.ScaleWidth a, msoFalse, msoScaleFromTopLeft
 
' cellen opmaken voor waarden mooi in te plaatsen
Range("voorbeeld").Select
Selection.Copy
Range("invulplaatsen").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
' templates kopieren en onder elkaar polaatsen
            Sheets("kwnie").Activate
            Range("Print_Area").CopyPicture
            Sheets("Stuktekeningtemplate").Activate
            Range("startcel").Select
 
            ActiveCell.Offset(((counter - 1) * 50) + 1, 0).Select
            ActiveSheet.PasteSpecial
 
' cellen legen voor volgende copy
Sheets("kwnie").Activate
Range("invulplaatsen").Select
Selection.ClearContents
 
 
        Next xyz
 
    Next bcell
 
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell.FormulaR1C1 = counter
 
End Sub

edit:- which column is totalelengte2 ? That will need editing into the code.
 
Last edited:
Upvote 0
Thank you very much for the effort.
It's not like i did, but still some errors in it. But maybe yours is almost correct. So i will try a little bit with ur code u send me now.
Thank you!

I haven't done any testing on this, only edited that method into the code.

Code:
Sub allesin1()
Dim acell As Range
Dim teller As Integer
Dim y As Double
Dim bcell As Range
Dim counter As Integer
 counter = 0
 teller = 0
' kijken hoeveel keer een template moet gemaakt worden
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell = ActiveCell.Offset(1, 1)
    For Each bcell In Range("Bereik")
        If Not (IsEmpty(bcell)) Then counter = counter + 1
 
' maak een cel om te starten
Sheets("kwnie").Activate
Range("D20").Name = "afmt100"
For xyz = 21 To counter + 20
' bereken het aantal cellen die nodig zijn
Z = Range("totalelengte1").Offset(xyz, 0) / 100
'bereken de schaal
a = Z / 83
' de afstand die 1 cel zal bevatten
Sheets("SETUP").Activate
Range("totalelengte1").Offset(xyz, 0).Select
x = Range("totalelengte1").Offset(xyz, 0).Value / Z
 
For abc = 19 To 28
Set acell = Sheets("SETUP").Cells(xyz, abc)
If acell.Value > 0 Then
acell.Copy
y = acell.Value / x
Sheets("kwnie").Activate
Range("afmt100").Activate
'ActiveCell.Value = "|"
ActiveCell.Offset(0, Round(teller + y / 2)).Select
teller = teller + y
ActiveSheet.PasteSpecial
End If
Next abc
' afbeelding aanpassen aan de schaal
 
  ActiveSheet.Shapes("afbeeldingg").Select
  ActiveSheet.Shapes("afbeeldingg").Delete
  Sheets("stuktekening gordingenang").Select
  ActiveSheet.Shapes("object 1").Copy
  Sheets("kwnie").Select
  Range("A24").Select
  ActiveSheet.Paste
  Selection.Name = "afbeeldingg"
  ActiveSheet.Shapes("afbeeldingg").Select
  Application.CutCopyMode = False
  Selection.ShapeRange.LockAspectRatio = msoTrue
  Selection.ShapeRange.ScaleHeight a, msoFalse, msoScaleFromTopLeft
  Selection.ShapeRange.ScaleWidth a, msoFalse, msoScaleFromTopLeft
 
' cellen opmaken voor waarden mooi in te plaatsen
Range("voorbeeld").Select
Selection.Copy
Range("invulplaatsen").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
' templates kopieren en onder elkaar polaatsen
            Sheets("kwnie").Activate
            Range("Print_Area").CopyPicture
            Sheets("Stuktekeningtemplate").Activate
            Range("startcel").Select
 
            ActiveCell.Offset(((counter - 1) * 50) + 1, 0).Select
            ActiveSheet.PasteSpecial
 
' cellen legen voor volgende copy
Sheets("kwnie").Activate
Range("invulplaatsen").Select
Selection.ClearContents
 
 
        Next xyz
 
    Next bcell
 
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell.FormulaR1C1 = counter
 
End Sub

edit:- which column is totalelengte2 ? That will need editing into the code.
 
Upvote 0
I've taken a guess on totalelengte1 being column Q, but you will need to edit the column number in for totalelengte2 (see the red text)

Rich (BB code):
Sub allesin1()
Dim acell As Range
Dim teller As Integer
Dim y As Double
Dim bcell As Range
Dim counter As Integer
 counter = 0
 teller = 0
' kijken hoeveel keer een template moet gemaakt worden
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell = ActiveCell.Offset(1, 1)
    For Each bcell In Range("Bereik")
        If Not (IsEmpty(bcell)) Then counter = counter + 1
            
' maak een cel om te starten
Sheets("kwnie").Activate
Range("D20").Name = "afmt100"
For xyz = 21 To counter + 20
' bereken het aantal cellen die nodig zijn
Z = Sheets("SETUP").Cells(xyz, totalelengte2) / 100
'bereken de schaal
a = Z / 83
' de afstand die 1 cel zal bevatten
Sheets("SETUP").Activate
x = Z = Sheets("SETUP").Cells(xyz, 17) / Z
 
For abc = 19 To 28
Set acell = Sheets("SETUP").Cells(xyz, abc)
If acell.Value > 0 Then
acell.Copy
y = acell.Value / x
Sheets("kwnie").Activate
Range("afmt100").Activate
'ActiveCell.Value = "|"
ActiveCell.Offset(0, Round(teller + y / 2)).Select
teller = teller + y
ActiveSheet.PasteSpecial
End If
Next abc
' afbeelding aanpassen aan de schaal
    
  ActiveSheet.Shapes("afbeeldingg").Select
  ActiveSheet.Shapes("afbeeldingg").Delete
  Sheets("stuktekening gordingenang").Select
  ActiveSheet.Shapes("object 1").Copy
  Sheets("kwnie").Select
  Range("A24").Select
  ActiveSheet.Paste
  Selection.Name = "afbeeldingg"
  ActiveSheet.Shapes("afbeeldingg").Select
  Application.CutCopyMode = False
  Selection.ShapeRange.LockAspectRatio = msoTrue
  Selection.ShapeRange.ScaleHeight a, msoFalse, msoScaleFromTopLeft
  Selection.ShapeRange.ScaleWidth a, msoFalse, msoScaleFromTopLeft
    
' cellen opmaken voor waarden mooi in te plaatsen
Range("voorbeeld").Select
Selection.Copy
Range("invulplaatsen").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
' templates kopieren en onder elkaar polaatsen
            Sheets("kwnie").Activate
            Range("Print_Area").CopyPicture
            Sheets("Stuktekeningtemplate").Activate
            Range("startcel").Select
           
            ActiveCell.Offset(((counter - 1) * 50) + 1, 0).Select
            ActiveSheet.PasteSpecial
                
' cellen legen voor volgende copy
Sheets("kwnie").Activate
Range("invulplaatsen").Select
Selection.ClearContents
     
        
        Next xyz
        
    Next bcell
  
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell.FormulaR1C1 = counter
    
End Sub
 
Upvote 0
Like that? and now an error by the red collored text.

Code:
Sub allesin122()
Dim acell As Range
Dim teller As Integer
Dim y As Double
Dim bcell As Range
Dim counter As Integer
 counter = 0
 teller = 0
' kijken hoeveel keer een template moet gemaakt worden
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell = ActiveCell.Offset(1, 1)
    For Each bcell In Range("Bereik")
        If Not (IsEmpty(bcell)) Then counter = counter + 1
            
' maak een cel om te starten
Sheets("kwnie").Activate
Range("D20").Name = "afmt100"
For xyz = 21 To counter + 20
' bereken het aantal cellen die nodig zijn
Z = Sheets("SETUP").Cells(xyz, 17) / 100
'bereken de schaal
a = Z / 83
' de afstand die 1 cel zal bevatten
Sheets("SETUP").Activate
x = Z = Sheets("SETUP").Cells(xyz, 17) / Z
 
For abc = 19 To 28
Set acell = Sheets("SETUP").Cells(xyz, abc)
If acell.Value > 0 Then
acell.copy
[COLOR=red]y = acell.Value / x
[/COLOR]Sheets("kwnie").Activate
Range("afmt100").Activate
'ActiveCell.Value = "|"
ActiveCell.Offset(0, Round(teller + y / 2)).Select
teller = teller + y
ActiveSheet.PasteSpecial
End If
Next abc
' afbeelding aanpassen aan de schaal
    
  ActiveSheet.Shapes("afbeeldingg").Select
  ActiveSheet.Shapes("afbeeldingg").Delete
  Sheets("stuktekening gordingenang").Select
  ActiveSheet.Shapes("object 1").copy
  Sheets("kwnie").Select
  Range("A24").Select
  ActiveSheet.Paste
  Selection.Name = "afbeeldingg"
  ActiveSheet.Shapes("afbeeldingg").Select
  Application.CutCopyMode = False
  Selection.ShapeRange.LockAspectRatio = msoTrue
  Selection.ShapeRange.ScaleHeight a, msoFalse, msoScaleFromTopLeft
  Selection.ShapeRange.ScaleWidth a, msoFalse, msoScaleFromTopLeft
    
' cellen opmaken voor waarden mooi in te plaatsen
Range("voorbeeld").Select
Selection.copy
Range("invulplaatsen").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
' templates kopieren en onder elkaar polaatsen
            Sheets("kwnie").Activate
            Range("Print_Area").CopyPicture
            Sheets("Stuktekeningtemplate").Activate
            Range("startcel").Select
           
            ActiveCell.Offset(((counter - 1) * 50) + 1, 0).Select
            ActiveSheet.PasteSpecial
                
' cellen legen voor volgende copy
Sheets("kwnie").Activate
Range("invulplaatsen").Select
Selection.ClearContents
     
        
        Next xyz
        
    Next bcell
  
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell.FormulaR1C1 = counter
    
End Sub
But maybe it's better I send it to you? if you are still busy with this code.

I've taken a guess on totalelengte1 being column Q, but you will need to edit the column number in for totalelengte2 (see the red text)

Rich (BB code):
Sub allesin1()
Dim acell As Range
Dim teller As Integer
Dim y As Double
Dim bcell As Range
Dim counter As Integer
 counter = 0
 teller = 0
' kijken hoeveel keer een template moet gemaakt worden
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell = ActiveCell.Offset(1, 1)
    For Each bcell In Range("Bereik")
        If Not (IsEmpty(bcell)) Then counter = counter + 1
 
' maak een cel om te starten
Sheets("kwnie").Activate
Range("D20").Name = "afmt100"
For xyz = 21 To counter + 20
' bereken het aantal cellen die nodig zijn
Z = Sheets("SETUP").Cells(xyz, totalelengte2) / 100
'bereken de schaal
a = Z / 83
' de afstand die 1 cel zal bevatten
Sheets("SETUP").Activate
x = Z = Sheets("SETUP").Cells(xyz, 17) / Z
 
For abc = 19 To 28
Set acell = Sheets("SETUP").Cells(xyz, abc)
If acell.Value > 0 Then
acell.Copy
y = acell.Value / x
Sheets("kwnie").Activate
Range("afmt100").Activate
'ActiveCell.Value = "|"
ActiveCell.Offset(0, Round(teller + y / 2)).Select
teller = teller + y
ActiveSheet.PasteSpecial
End If
Next abc
' afbeelding aanpassen aan de schaal
 
  ActiveSheet.Shapes("afbeeldingg").Select
  ActiveSheet.Shapes("afbeeldingg").Delete
  Sheets("stuktekening gordingenang").Select
  ActiveSheet.Shapes("object 1").Copy
  Sheets("kwnie").Select
  Range("A24").Select
  ActiveSheet.Paste
  Selection.Name = "afbeeldingg"
  ActiveSheet.Shapes("afbeeldingg").Select
  Application.CutCopyMode = False
  Selection.ShapeRange.LockAspectRatio = msoTrue
  Selection.ShapeRange.ScaleHeight a, msoFalse, msoScaleFromTopLeft
  Selection.ShapeRange.ScaleWidth a, msoFalse, msoScaleFromTopLeft
 
' cellen opmaken voor waarden mooi in te plaatsen
Range("voorbeeld").Select
Selection.Copy
Range("invulplaatsen").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
' templates kopieren en onder elkaar polaatsen
            Sheets("kwnie").Activate
            Range("Print_Area").CopyPicture
            Sheets("Stuktekeningtemplate").Activate
            Range("startcel").Select
 
            ActiveCell.Offset(((counter - 1) * 50) + 1, 0).Select
            ActiveSheet.PasteSpecial
 
' cellen legen voor volgende copy
Sheets("kwnie").Activate
Range("invulplaatsen").Select
Selection.ClearContents
 
 
        Next xyz
 
    Next bcell
 
    Sheets("SETUP").Activate
    Range("begincellll").Select
    ActiveCell.FormulaR1C1 = counter
 
End Sub
 
Upvote 0
Which is the correct column for totalelengte1 and for totalelengte2?

When the error happens there should be a number on the error message, this will help to find the cause of the error.
 
Upvote 0
Q, so that's correct i think?
And yes : Division by error.

Man you are smart :stickouttounge:


Which is the correct column for totalelengte1 and for totalelengte2?

When the error happens there should be a number on the error message, this will help to find the cause of the error.
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,696
Members
452,938
Latest member
babeneker

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