Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 13th, 2011, 03:06 PM
Registered User
 
Join Date: Jun 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code runs right first time but not second time

I have the following code that looks to see if there is a duplicate row of data (based on first 4 cells of first row) in the database. If there isn't a row, it copies it to the first unused row, otherwise it asks the user if they want to replace it or cancel. It runs right the first time to replace a row, but if I run it again it doesn't present the messagebox and instead, selects the first row in the database and replaces that row. If I close out the workbook and run the code when I reopen the workbook, it replaces the duplicate row just fine. There must be something that is not resetting but I can't figure it out. Any ideas?

Code:
 
Option Explicit
Dim iRow As Long
Dim ws As Worksheet
Dim Answer As Integer
Dim DATA As Range
Dim Row As Range
Private Sub results_copy()

Set ws = ActiveWorkbook.Worksheets("Support Levels")

ws.Select
'set Data range minus row with column labels
With Range("DATA")
    Set DATA = .Offset(1, 0).Resize(.Rows.Count - 1)
End With
'sort to find any rows matching first five cells in row that I want to add
Call Find_Match
'If there are no records found, then copy row to last row
    If ws.Range("A2").Value = 0 Then 'A2 us subtotal of found rows
        Call Reset_Autofilter
        Call DataEnter
        ActiveWorkbook.Worksheets("INPUT").Select
        Exit Sub
    Else
        Call Calc_Manual 'turn off autoCalc to make search faster
        For Each Row In DATA.Rows
'Message Box to prompt user that there is already a matching Record on the Support Levels tab
            If Row.EntireRow.Hidden = False Then Answer = MsgBox(Prompt:="Matching record found, replace it?", Buttons:=vbOKCancel + vbQuestion, Title:="MATCHING RECORD FOUND!")
                If Answer = vbOK Then
                    Row.EntireRow.Select 'select row that is not hidden and replace with row I need to add
                    ws.Range("A1").EntireRow.Copy
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    Exit For
                End If
        Next Row
    End If
    'reset autofilter and auto calc and return to input page
Call Reset_Autofilter
Call Calc_Auto
ActiveWorkbook.Worksheets("INPUT").Select

End Sub
Private Sub DataEnter()
Set ws = Worksheets("Support Levels")
'find first empty row in database
iRow = ws.Range("A3").End(xlDown).Offset(1, 0).Row

'Row to be copied
    ws.Cells(iRow, 1).Select
    ws.Range("A1").EntireRow.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    
End Sub
Private Sub Find_Match()
Set ws = ActiveWorkbook.Worksheets("Support Levels")
' Find_Match Macro
' Macro recorded 5/26/2011 by Dean_hess

    ws.Range("A3").Select
    Selection.AutoFilter Field:=1, Criteria1:=Range("A1").Value
    Selection.AutoFilter Field:=2, Criteria1:=Range("B1").Value
    Selection.AutoFilter Field:=3, Criteria1:=Range("C1").Value
    Selection.AutoFilter Field:=4, Criteria1:=Range("D1").Value
    Selection.AutoFilter Field:=5, Criteria1:=Range("E1").Value
End Sub
Private Sub Reset_Autofilter()

Worksheets("Support Levels").Select
With ActiveSheet
    If .AutoFilterMode Then
        If .FilterMode Then
            .ShowAllData
        End If
    End If
End With

End Sub
Private Sub Calc_Manual()

With Application
    .Calculation = xlManual
    .MaxChange = 0.001
End With

End Sub
Private Sub Calc_Auto()

With Application
    .Calculation = xlAutomatic
    .MaxChange = 0.001
End With

End Sub
 
Old June 14th, 2011, 09:26 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

If Row.EntireRow.Hidden = False is the culprit

Either you need to take this out or reset

Answer = vbOK to Answer = ""

inside the If loop

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old June 14th, 2011, 01:45 PM
Registered User
 
Join Date: Jun 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your answer and that was the culprit.
At first it gave me the error message, "Run-Time error 13, Type mismatch", so I changed the Answer data type to Boolean and it runs perfect.
Thank you!
 
Old October 16th, 2012, 02:37 PM
Registered User
 
Join Date: Jun 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Calculation turned off

I also had Auto calculate turned off so some of the code that was dependent on a calculation was not working properly. After Find match I made it calculate once.





Similar Threads
Thread Thread Starter Forum Replies Last Post
synchronizing pocket pc time with desktop time bobbyrayudu83 C# 1 April 2nd, 2011 04:28 AM
Do vb exe files time out after a certain period of time? sanderson Visual Basic 2008 Essentials 0 June 11th, 2009 06:48 PM
Time Shift time in minus time out lechalas Beginning VB 6 1 August 11th, 2008 01:56 PM
time zone & day light time rajn ASP.NET 1.0 and 1.1 Professional 0 August 7th, 2007 05:02 PM
Using xs:time to generate time in desired format krayan001 XSLT 0 June 27th, 2005 04:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.