Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > Simple loop/resetting variable help


Reply
 
Thread Tools Display Modes
  #1  
Old 06-22-2012, 12:03 PM
smasood smasood is offline
Newcomer
 
Join Date: Jun 2012
Posts: 8
Default Simple loop/resetting variable help


Hi guys,
What I am trying to do is to search sheet1 for every value in column A of sheet2 and once found in sheet1, get the value of column C in sheet1 and put it in sheet2. The only problem I am having is that the variables don't reset. I mean that when it finds the Range of the first value, it stores that value till the program is done looping and doesn't proceed with the for loop.

This is what I have:

Code:
Sub Reset_Used_Range()
a = ActiveSheet.UsedRange.Rows.Count
End Sub

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+G
'


Dim sheet1 As Worksheet
 Dim diffSheet As Worksheet
 Dim LastRow As Integer
 Dim found As Boolean
 

 Set sheet1 = ThisWorkbook.Sheets(1)
 Set diffSheet = ThisWorkbook.Sheets(2)
 
 With sheet1
 diffSheet.Activate
 LastRow = diffSheet.Cells(.Rows.Count, 2).End(xlUp).Row
 sheet1.Activate
 
 
 For rwn = 1 To LastRow
 
FindFTP = diffSheet.Cells(rwn, 1)
 
  

    Dim strTOFIND As String
    strTOFIND = FindFTP
    'MsgBox (strTOFIND)
    Dim rngFound As Range
    Dim rngToDelete As Range
    Dim strFirstAddress As String
    Application.ScreenUpdating = False
    With sheet1.Range("D:D")
        Set rngFound = .Find(What:=FindFTP, _
                            After:=.Cells(1), _
                            LookIn:=xlValues, _
                            Lookat:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False)
                             
            
              'MsgBox (findClinic)
    If rngFound Is Nothing Then
     diffSheet.Cells(rwn, 3).FormulaR1C1 = ""
     Application.ActiveSheet.UsedRange
     Call Reset_Used_Range

     Else
     
            findClinic = sheet1.Cells(ActiveCell.Row, 3)
            
            diffSheet.Cells(rwn, 3).FormulaR1C1 = findClinic
              
                 Application.ScreenUpdating = True
                 Application.ActiveSheet.UsedRange
                 Call Reset_Used_Range
                 findClinic = Null

              End If
    End With
    Next rwn
    End With
  
  

End Sub
Reply With Quote
  #2  
Old 06-23-2012, 06:18 PM
ZKat ZKat is offline
Centurion
 
Join Date: May 2006
Posts: 132
Default

Hi,

I am a little confused on this one. I have not tested the code, but scanning through, it appears that some of your variables are not declared. Do you have Option Explicit at the top, if not add to your code.


Maybe you have other code with all the variables declared?

Last edited by ZKat; 06-24-2012 at 03:21 PM.
Reply With Quote
  #3  
Old 06-26-2012, 06:26 AM
TheRealTinTin's Avatar
TheRealTinTin TheRealTinTin is offline
Junior Contributor
 
Join Date: Nov 2008
Location: Glasgow, UK
Posts: 328
Default

By the sounds of it, there will be only 1 match? If this is true then you could simply use a lookup formula rather than VBA.
__________________
Artificial Intelligence is no match for natural stupidity
Reply With Quote
  #4  
Old 07-01-2012, 08:22 AM
cacody cacody is offline
Freshman
 
Join Date: Apr 2011
Location: Scottsdale AZ
Posts: 37
Default

Smasood
I'm learning VBA also but through trial and error I built this macro that I think does what you want:

Sub copydata()
' This version selects all values in range in column A, and copies data in column C to different sheet with formatting
'
Dim cell As Range
Application.ScreenUpdating = False
'Name = InputBox("Select name to copy: ", Title:="Select Name") 'This will allow you to specify a single value
Range("a1:a10").Select 'You specify the range here
For Each cell In Selection
'If cell(1, 1).Value = Name Then 'Use this to select a variable Name
If cell(1, 1).Value <> "" Then 'Use this to select all values in column A
cell(1, 3).Select
If nextrow = 0 Then
nextrow = Sheets("Sheet2").Range("a30").End(xlUp).Row 'Make sure the range is below the number of values to be copied
Else
nextrow = Sheets("Sheet2").Range("a30").End(xlUp).Row + 1
End If
Selection.Copy Sheets("Sheet2").Cells(nextrow, 1)
End If
Next cell
End Sub
Reply With Quote
  #5  
Old 07-03-2012, 01:31 PM
MPi MPi is offline
Contributor

Forum Leader
* Expert *
 
Join Date: Dec 2001
Location: Quebec
Posts: 707
Default

Hello,

In your code, I think that
findClinic = sheet1.Cells(ActiveCell.Row, 3)
should be
findClinic = sheet1.Cells(rngFound.Row, 3)
__________________
MPiČ
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->