Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Please Help me Review Code - LOOP doesn't stop

Hello- Could you please help me review what I am writing wrong in the code
below? The problem I want to solve is that after the data is pasted, the
program keeps on looping and it ends up deleting the correct information that
it had initially pasted. I would like the program to stop after it pastes the
information once. What do I need to change in the code to accomplish that?
Thank you.

Filo
------------------------------------------
Sub InsertDTBinMFR()


Dim DeptName As String, wb As String
Dim z As Long, x As Long

wb = Workbook.Name

Workbooks(wb).Activate
Sheets(1).Select

Worksheets.Add Count:=1, After:=Sheets(Sheets.Count)
ActiveSheet.Name = "MTD-DTB"
DeptName = Left(Sheets(1).Range("F1").Text, 5)


Workbooks("drill testing.xls").Activate
Sheets("DTB").Select

For z = 1 To 20000

If Cells(z, "a").Value = DeptName Then

Firstrow = Range("A" & z).Address

For x = 20000 To 1 Step -1

If Range("A" & x).Value = DeptName Then
lastrow = Range("I" & x).Address

Range(Firstrow, lastrow).Select
Selection.Copy

Workbooks(wb).Activate
Sheets(2).Select
Range("A1").Select

Selection.PasteSpecial Paste:=xlValues
End If

Next x

End If

Next z

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default Please Help me Review Code - LOOP doesn't stop

Filo,

Exit out of the For loop on the line after the paste operation. i.e.

Selection.PasteSpecial Paste:=xlValues
Exit For '<---insert this line to exit out of the current for loop


--
Hope that helps.

Vergel Adriano


"Filo" wrote:

Hello- Could you please help me review what I am writing wrong in the code
below? The problem I want to solve is that after the data is pasted, the
program keeps on looping and it ends up deleting the correct information that
it had initially pasted. I would like the program to stop after it pastes the
information once. What do I need to change in the code to accomplish that?
Thank you.

Filo
------------------------------------------
Sub InsertDTBinMFR()


Dim DeptName As String, wb As String
Dim z As Long, x As Long

wb = Workbook.Name

Workbooks(wb).Activate
Sheets(1).Select

Worksheets.Add Count:=1, After:=Sheets(Sheets.Count)
ActiveSheet.Name = "MTD-DTB"
DeptName = Left(Sheets(1).Range("F1").Text, 5)


Workbooks("drill testing.xls").Activate
Sheets("DTB").Select

For z = 1 To 20000

If Cells(z, "a").Value = DeptName Then

Firstrow = Range("A" & z).Address

For x = 20000 To 1 Step -1

If Range("A" & x).Value = DeptName Then
lastrow = Range("I" & x).Address

Range(Firstrow, lastrow).Select
Selection.Copy

Workbooks(wb).Activate
Sheets(2).Select
Range("A1").Select

Selection.PasteSpecial Paste:=xlValues
End If

Next x

End If

Next z

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default Please Help me Review Code - LOOP doesn't stop

Tell us, from the beginning, what you are trying to do.
How is z determined? Once both wbs are active you do NOT have to select to
copy from one to the other.

--
Don Guillett
SalesAid Software

"Filo" wrote in message
...
Hello- Could you please help me review what I am writing wrong in the code
below? The problem I want to solve is that after the data is pasted, the
program keeps on looping and it ends up deleting the correct information
that
it had initially pasted. I would like the program to stop after it pastes
the
information once. What do I need to change in the code to accomplish that?
Thank you.

Filo
------------------------------------------
Sub InsertDTBinMFR()


Dim DeptName As String, wb As String
Dim z As Long, x As Long

wb = Workbook.Name

Workbooks(wb).Activate
Sheets(1).Select

Worksheets.Add Count:=1, After:=Sheets(Sheets.Count)
ActiveSheet.Name = "MTD-DTB"
DeptName = Left(Sheets(1).Range("F1").Text, 5)


Workbooks("drill testing.xls").Activate
Sheets("DTB").Select

For z = 1 To 20000

If Cells(z, "a").Value = DeptName Then

Firstrow = Range("A" & z).Address

For x = 20000 To 1 Step -1

If Range("A" & x).Value = DeptName Then
lastrow = Range("I" & x).Address

Range(Firstrow, lastrow).Select
Selection.Copy

Workbooks(wb).Activate
Sheets(2).Select
Range("A1").Select

Selection.PasteSpecial Paste:=xlValues
End If

Next x

End If

Next z

End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Please Help me Review Code - LOOP doesn't stop

Thank you!

"Vergel Adriano" wrote:

Filo,

Exit out of the For loop on the line after the paste operation. i.e.

Selection.PasteSpecial Paste:=xlValues
Exit For '<---insert this line to exit out of the current for loop


--
Hope that helps.

Vergel Adriano


"Filo" wrote:

Hello- Could you please help me review what I am writing wrong in the code
below? The problem I want to solve is that after the data is pasted, the
program keeps on looping and it ends up deleting the correct information that
it had initially pasted. I would like the program to stop after it pastes the
information once. What do I need to change in the code to accomplish that?
Thank you.

Filo
------------------------------------------
Sub InsertDTBinMFR()


Dim DeptName As String, wb As String
Dim z As Long, x As Long

wb = Workbook.Name

Workbooks(wb).Activate
Sheets(1).Select

Worksheets.Add Count:=1, After:=Sheets(Sheets.Count)
ActiveSheet.Name = "MTD-DTB"
DeptName = Left(Sheets(1).Range("F1").Text, 5)


Workbooks("drill testing.xls").Activate
Sheets("DTB").Select

For z = 1 To 20000

If Cells(z, "a").Value = DeptName Then

Firstrow = Range("A" & z).Address

For x = 20000 To 1 Step -1

If Range("A" & x).Value = DeptName Then
lastrow = Range("I" & x).Address

Range(Firstrow, lastrow).Select
Selection.Copy

Workbooks(wb).Activate
Sheets(2).Select
Range("A1").Select

Selection.PasteSpecial Paste:=xlValues
End If

Next x

End If

Next z

End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how stop review tool bar always showing up Ian Elliott Excel Discussion (Misc queries) 2 April 10th 07 03:48 PM
vba code to block review toolbar Todd F Excel Programming 7 October 16th 06 12:23 AM
How can I stop the Web and Review toolbar opening up randomly? D Excel Discussion (Misc queries) 1 June 27th 06 07:19 PM
Code Review Eric Excel Programming 3 February 17th 06 06:57 PM
VB6 with Excel Objects: Please review my code Kurt Remlin Excel Programming 6 May 1st 04 03:36 AM


All times are GMT +1. The time now is 08:36 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"