+ Reply to Thread
Results 1 to 6 of 6

Check if cell <> selection

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-16-2009
    Location
    Dorset, UK
    MS-Off Ver
    Office 2003, Office 2007
    Posts
    131

    re: Check if cell <> selection

    I'm not sure if there's a better solution, but I can help you with your existing code.

    Rather than using the active cell for everything, you would be better off defining ranges. It makes it much easier to keep track of where you are and means you can 'store' more than one cell at a time. For example:
    Sub CSVCONSOL24()
    
    'Grabs Part TWC Cell
    
    Dim a As Integer, b As Integer, Cl As Range
    
        Rows("1:1").Select
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    
    Let a = Selection.Rows.Count
    
    Cells(1, 2).Select
    
    For b = 1 To a
    Sheets("0309").Activate
    Set Cl = Cells(b, 2)
     If Cl.Value = "TWC  #" Then
     Cl.Offset(1, 0).Select
            
            If ActiveCell.Value = "" Then
            ActiveCell.FormulaR1C1 = "-"
            End If
            
        Sheets("Table").Activate
        Range("Z1").Select
        Selection.End(xlDown).Select
    
    '***************
    If ActiveCell.Value <> Cl.Value Then 
         ActiveCell.Offset(1, 0).Value = Cl.Value
    End If
    '****************
        
    End If
        
    Next b
    
    End Sub
    I have left in most of your ActiveCell references as I'm unsure exactly what some of your tests are for, but as you can see I have defined a range to hold the cell with the value you are comparing.

  2. #2
    Registered User
    Join Date
    04-27-2009
    Location
    Victoria, Australia
    MS-Off Ver
    Excel 2003
    Posts
    5

    re: Check if cell <> selection

    Quote Originally Posted by Kafrin View Post
    I'm not sure if there's a better solution, but I can help you with your existing code.

    Rather than using the active cell for everything, you would be better off defining ranges. It makes it much easier to keep track of where you are and means you can 'store' more than one cell at a time. For example:
    Sub CSVCONSOL24()
    
    'Grabs Part TWC Cell
    
    Dim a As Integer, b As Integer, Cl As Range
    
        Rows("1:1").Select
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    
    Let a = Selection.Rows.Count
    
    Cells(1, 2).Select
    
    For b = 1 To a
    Sheets("0309").Activate
    Set Cl = Cells(b, 2)
     If Cl.Value = "TWC  #" Then
     Cl.Offset(1, 0).Select
            
            If ActiveCell.Value = "" Then
            ActiveCell.FormulaR1C1 = "-"
            End If
            
        Sheets("Table").Activate
        Range("Z1").Select
        Selection.End(xlDown).Select
    
    '***************
    If ActiveCell.Value <> Cl.Value Then 
         ActiveCell.Offset(1, 0).Value = Cl.Value
    End If
    '****************
        
    End If
        
    Next b
    
    End Sub
    I have left in most of your ActiveCell references as I'm unsure exactly what some of your tests are for, but as you can see I have defined a range to hold the cell with the value you are comparing.
    Thanks for that, I gave it a shot just before you replied and got similar result. Here is what I just typed up before I hit the refresh button..

    'Grabs Part TWC Cell
    
    Dim a, b As Integer
    Dim v As String
    
        Rows("1:1").Select
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    
    Let a = Selection.Rows.Count
    
    Cells(1, 2).Select
    
    For b = 1 To a
    Sheets("0309").Activate
    Cells(b, 2).Select
     If ActiveCell.Value = "TWC  #" Then
     ActiveCell.Offset(1, 0).Select
            
            If ActiveCell.Value = "" Then
            ActiveCell.FormulaR1C1 = "-"
            End If
            
            v = ActiveCell.Value
    
        Sheets("Table").Activate
        Range("Z1").Select
        Selection.End(xlDown).Select
        ActiveCell.Select
            If ActiveCell.Value <> v Then
            ActiveCell.Offset(1, 0).Select
            ActiveCell.Value = v
            End If
        
     End If
        
    Next b
    
    End Sub
    Seeing as my code is sloppy, ill give yours a shot.

    Thanks for the help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1