MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 30th, 2004, 07:09 PM   #1
jcvoth
 
Join Date: Feb 2004
Posts: 198
Default For Each question

Greetings all!

I am having a problem with a loop and I hope someone can help.

Essentially, my goal is this. When a command button is clicked, the user is prompted to enter a 3 digit code. When that code is entered, the macro will search through the list of data and put a 1 in a cell for the row that contains the code and do nothing to the rows that contain a different code. I am close, but for some reason, the value does not seem to equal the information I entered in the inputbox. My theory is that I need to format the result from the inputbox to equal the format in the row data, but I cannot figure out how to do it.

Below is the code I have so far. (I have commented out the pieces that are not relevant to this portion of the testing)

Sub discprice()
mytimer = Timer
master = "E:\Sales Accounting\D F I\DFI Master File.xls"
'Workbooks.Open Filename:=(master)
'ActiveWindow.ActivateNext
lastrow = Range("A65536").End(xlUp).Row
cctr = InputBox("What Cost Center should be updated?")
'Set lookuprange = Workbooks("DFI Master File.xls").Worksheets("Price Lists").Range("a2:e9000")
For i = 2 To lastrow

If Range("v" & i).Value <> cctr Then Range("H" & i).Value = 0

Next i
'Workbooks("DFI Master File.xls").Close SaveChanges:=False
MsgBox ("It took " & Timer - mytimer & " seconds to update the discount price for " & cctr & "." & vbCrLf & vbCrLf & "You have " & lastrow - 1 & " records in this file.")
End Sub


Any assistance is greatly appreciated.

-Jarrod
jcvoth is offline   Reply With Quote
Old Mar 30th, 2004, 07:11 PM   #2
Joe4
MrExcel MVP
Moderator
 
Joe4's Avatar
 
Join Date: Aug 2002
Posts: 14,220
Default Re: For Each question

Try declaring your variables at the beginning of your code (which is something you should be doing anyway!).

i.e.

Dim cctr as String

or

Dim cctr as Integer
__________________
TIPS FOR FINDING EXCEL SOLUTIONS
1. Use the built-in Help that comes with Excel/Access
2. Use the Search functionality on this board
3. A lot of VBA code can be acquired by using the Macro Recorder.
Joe4 is offline   Reply With Quote
Old Mar 30th, 2004, 07:14 PM   #3
jcvoth
 
Join Date: Feb 2004
Posts: 198
Default Re: For Each question

Brilliant!

Although I am a little embarassed it was something so simple as a variable declaration...



Thank you!
-Jarrod
jcvoth is offline   Reply With Quote
Old Mar 30th, 2004, 07:20 PM   #4
Joe4
MrExcel MVP
Moderator
 
Joe4's Avatar
 
Join Date: Aug 2002
Posts: 14,220
Default Re: For Each question

As you get more into programming, you may want to "force" yourself to declare all variables by turning on the "Option Explicit" command. It may seem like a nuisance in the beginning, but most experienced programmers will tell you that it saves a lot of trouble when trying to debug code (especially if you have a lot of code)!

For a good write-up on the benefits of this, check out Tom Urtis's response in this thread: http://www.mrexcel.com/board2/viewto...ption+explicit

Good luck and happy Excel-ling!
__________________
TIPS FOR FINDING EXCEL SOLUTIONS
1. Use the built-in Help that comes with Excel/Access
2. Use the Search functionality on this board
3. A lot of VBA code can be acquired by using the Macro Recorder.
Joe4 is offline   Reply With Quote
Reply

Bookmarks

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 On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 04:25 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.