Excel 2007 :: Do Loop With IF Statement

Nov 12, 2013

I am having an issue getting an IF statement to work inside a DO loop. I have Microsoft Excel 2007 and Windows 7 Entreprise.

In my Excel spreadsheet, I have in column B a list of keywords and in column C the category to which each keyword belongs. For instance, the keyword "boot" goes withe the category "Shoes", "scarf" goes in "Accessories", etc.

In another column, I have a list of product descriptions (like "Black Boots" or "Silk Scarf") and I would like to assign the relevant category to each product. I thought to do this by creating a new function in VBA. This function would go through the whole list of keywords in column B, checking if the keyword is included in the product description. If it is, the function's result would be the relevant category, and if it is not, the function would search the next line in column B, until a relevant keyword is found, or until the end of the list.

Here is the code I have written so far:

Code:
Function SegmentSearch(Item)
Dim i As Integer
i = 1
Do
i = i + 1

If Application.WorksheetFunction.IsNumber(Application.WorksheetFunction.Search(Sheet5.Cells(i, 2), Item)) = "TRUE" Then SegmentSearch = Cells(i, 3)
Loop Until Application.WorksheetFunction.IsNumber(Application.WorksheetFunction.Search(Sheet5.Cells(i, 2), Item)) = "TRUE"
End Function

Without the loop, the function works fine (if "Item" happens to include the first keyword in the list) but as soon as I try putting the loop I get a #VALUE! error.

View 7 Replies


ADVERTISEMENT

Excel 2007 :: Skip Worksheets In For Next Loop

Sep 24, 2013

I'm trying to loop through worksheets as follows:

If ws.Name "DataTable" And ws.Name "Summary" Then ws.Activate
code to do some copy/paste stuff ....
Next ws

Try as I may, I can't get the code to skip the worksheets named "DataTable" and "Summary". Based on other posts, I've tried swapping out the And for an Or. Also tried using LCase(ws.Name), to no avail. Note that I'm acually trying to skip several other sheets as well but only listed two here for simplicity.

I'm using Excel 2007.

View 4 Replies View Related

Excel 2007 :: Consolidate Multiple Loop Statements

Jan 29, 2012

I'm working in XL07 on Win7.

The workbook has two worksheets "Data" & "Crunch". "Data" contains the raw data-range B1:U50. "Crunch" is the processing worksheet. Row 1 is a header row. Columns A-D are empty. Column E (rows 2-81) contain the numbers 1-80.

The macro is intended to run in "Crunch". I need to add the CountIf function in every cell/row filling the range F2:BC81. A loop does the trick for me to fill the rows in each column but I don't know how to move the loop down each column without creating a new loop.

A section of the code is below. Notice a majority of the code is the same, the copy in red is variable from loop to loop. I need to repeat this loop through column "BC" (50 times in total). My guess is there is a way to write this code one time instead of 50 times adjusting the variable components.

Code:
'Find total # of records and then store in variable
totalrecords = ActiveSheet.UsedRange.Rows.Count
'CountIf Statement

[Code]....

View 3 Replies View Related

Excel 2007 :: Copy And Paste A Range Using Do Loop?

Jul 27, 2012

Here is my situation:

Background:
- Excel VBA 2007
- I have a macro that inserts a set of vlookups in range D3:D8.
- When D3:D8 has been filled, I need the macro to COPY the Vlookups in D3:D8, skip down one cell, and paste in the next set of open spaces.
- I have a Do statement that isn't working, and and IF...THEN that quits the loop when a blank cell is found (This means the range of vlookups is no longer needed).

Objectives:
- Fix Do...Loop so it copies D3 throuh D8, skips a cell below D8, and Pastes in the next section? - There will sometimes be only one section to paste in, and sometimes 20. It is dynamic.

Code:

Sub InsertVLookup2()
' This macro inserts the VLookup into cell B2
Dim lastcolumn As Long
Range("D3").Formula = "=VLOOKUP($A2,INDIRECT(""'"" & B$1 & ""'!A:I""),9,FALSE)"
Range("D3").Select

[Code] .......

View 1 Replies View Related

Excel 2007 :: Loop To Search Worksheets For Specific Range Value?

Mar 8, 2012

using Excel 2007. I need a code to identify a worksheet within a workbook by cell/range value. The book is used by various users. They have the rights to add new sheets and all but delete columns in the 'master worksheet'. The sheet names can be changed by the user but I need to rename the master sheet on opening the file. To do this I have put a specific value in a cell within the master worksheet which then should allow me to find the sheet and rename it. (let say Range A1 has a value of "this sheet") I have a mental block on how i can run a loop to search each sheet for the identifying value until the range and value is found and the sheet identified

View 5 Replies View Related

Excel 2007 :: Loop Expression Won't Recognize Values Are Equal?

Apr 20, 2012

(Excel 2007) The issue I'm having is that a loop statement of mine is failing to recognize that a couple values are equal. It works for the first 3 iterations of the loop, then stops recognizing. I tried copying another set of numbers in the failing one's place, and that actually worked. I can't attach anything, so I'm including a link to a detailed screen shot with some notes on it and code that I'll host on my site. Not sure how else to explain the issue, but if you take a look at the screen shot you can see the excel sheet being worked on and the vba code running. I will also include a text file of the code

Here are the links to the screen shot and text file of my code
Text file
Screen Shot

View 3 Replies View Related

Excel 2007 :: VBA - Finding Endless Loop In Long Routine?

May 14, 2014

I have a very long routine, looping through 35,000-ish rows several times to detect and delete unwanted items. Occasionally, it gets locked into an endless loop and CTRL+Break will not stop it - I have to kill Excel through the Task Manager.

How can I find out where the endless loop is so I can detect what's causingit and fix it?

View 6 Replies View Related

Excel 2007 :: Loop Through Every File In Folder And List All Sheet Names?

Oct 9, 2012

I was wondering if anyone had any code to loop through every file in a folder and list the file name along with every sheet name in that file? I'm using Excel 2007.

View 4 Replies View Related

Use IF Statement In DO WHILE Loop

May 18, 2009

I have the following loop that should look through my rows of data for the Value "Y" in column T, for those rows that contain "Y" it should update column K with the variable xdate. For some reason it doesn't seem to work, it changes the first entry it finds and then seems to crash, I have to use CTRL+BREAK to stop it.

View 3 Replies View Related

Using MID On Range In For Each Loop With IF Statement?

Aug 8, 2013

I am trying to loop through a range, checking for a cell to contain one of two characters and then put a value in a cell to the side (well 3 away) of it. Im getting an error for Type mismatch on line:

VB:
If CellContent = "h" Or "v" Then

the data in the cell is an along the lines of '12.34.56.43 som-thi-vh-ng1'

VB:
Dim MyRange, MyCell As Range
Dim CellContent As String
Sheets("Sheet1").Activate

[Code]....

View 1 Replies View Related

How Can I Use An IF Statement To Loop LINEST

Sep 2, 2008

how to get my LINEST function to work properly. Now, what I'd like to do is have it reduce or increase the order of the polynomial until R^2 equals 1.0, or the polynomial reaches 6th order, whichever comes first.

Can I do this with a bunch of nested IF ELSEIF statements, or is there a cleaner way to do this?

View 12 Replies View Related

How To Input If Statement In For Loop

Jul 31, 2013

I have a code like this below

Sub Copydata()
Dim emre As Integer
For emre = 100 To 121
Sheets("CP TB").Select
ActiveSheet.Range("$B$8:$G$10000").AutoFilter Field:=3, Criteria1:=emre
Range("E8:F8").Select

[Code] .....

I want to skip values when the selection is a value I choose. I tried to put a "If" statement but I couldn't make it work.

The IF statement will check the selection after red colored code. if cell value is "a" then it will go for Next emre.

View 6 Replies View Related

Getting Error For For Each Loop With If Statement

Nov 9, 2013

I am getting the Run-time error '380'. Could not set the value propery. Invalid property value.

I have a user form for data entry purpose which is working fine. Now, I am making a code for data editing purpose which pulls out data from the worksheet into the user form.

I am able to do this for text boxes and combo boxes that are not linked with each other. However I am getting a problem in the following case; I have a combo box and a text box that are linked to each other by combo box _change code which is below which may be the reason for the error I am getting for the next code...

Code:
Private Sub cmbColor_Change()
If cmbColor.Value = "Other" Then
lblColor.Visible = True

[Code]....

View 6 Replies View Related

Do While Loop Statement Not Performing

Aug 5, 2008

I have the following code which previously was showing an error on the Do While line. It was suggested that I change the line too Range("B2").Text. However, the loop is not deleting the rows based on "#N/A". Can anyone offer any suggestions?

Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess .....

View 9 Replies View Related

A Write Statement With A For Loop

Jun 15, 2007

So here is my problem:

how do you repeat a write statement four time

form 1234 to 1234123412341234

View 3 Replies View Related

Call Statement In Loop Only Once

Oct 16, 2007

If ComboBox1.Text = " Market 1" Then
StoreNumList = Array ("66", "67", "107", "222") 'obviously array string is much longer than this
For x = LBound(StoreNumList) To UBound (StoreNumList)
StoreNum = StoreNumList(x)
ComboBox1.value = StoreNum
Call btnOK_Click_Process
Next x

Program runs fine first step through. It calls the btnOK_Click_Process sub routine just fine. However on second and subsequent For/Next cycles it does not call btnOK_Click_Process.

View 9 Replies View Related

If Statement Causing Loop To Fail

Dec 28, 2006

My nested loop works as intended without If statements, ie it counts non blank cells in a series of 17 ranges (this loop is called the CountRangesOnEachRow loop) AND then drops down a row and repeats (this loop is called the RowNo loop). The problem is that once my first If statement occurs the RowNo loop fails and it stops at the first row.

The first If statement is designed to do the following:

1. Tests to see if A4 has data. If yes, copy cell contents (pupil's name) to Sheet(2).Range("StudentName")
2. If empty skip the countranges loop and move down a row to A5.
3. Cells A4:A35 need to be tested for data in this manner.

I've included a sample workbook & code with the IF statement comment blocked so you can see what it should do AFTER it tests the A column for data. The macro should end after row 6 as A6:A35 are all blank.

View 9 Replies View Related

Setting Value In For Each Statement Stars Infinite Loop

Sep 17, 2009

Setting value in For Each statement stars infinite loop. I am trying to get the following script to work

View 2 Replies View Related

Nested Next Loop: Evaluate The If Statement Below Over A Range Of Cells

Aug 22, 2008

I think the problem is the order in which I have the "next" loops. I would like to evaluate the if statement below over a range of cells

Sub Analysis()

Dim i As Integer
Dim x As Integer
Dim y As Integer
Dim k As Integer
Dim TheLast As Integer

TheLast = Sheets("Name").Range("c" & Rows.Count).End(xlUp).Row

For i = -7 To -26 Step -1
For x = 24 To 43
For y = 14 To 43
For k = 16 To TheLast.................

View 9 Replies View Related

Excel 2010 :: Use One Set Statement To Set Second Statement?

Feb 14, 2012

I am attempting to use a previously Set variable as part of the next Set statement, pretty unsuccessfully at present.

My purpose is trying to look up

Code:
tb_SelJobID.Value

from a userform in Col Z then look across the row to Cols D,I,N,S & W (different types of work) to see if

Code:
TbSelYr.Value

matches the year selected then insert a formula in the row to the left. Then loop down to the FinalRow.

Currently my Set Found1 statement does not recognise my Found10 value. I know it will be my syntax as it always is. I have cut down the following code to display where the problem areas are, Found1 thru 5.

Code:

Sub CmdGo3_Click()
Dim Row As Range
Dim FinalRow As Long
Dim Found1, Found2, Found3, Found4, Found5, Found10 As Range
Application.ScreenUpdating = False

[code]....

Windows 7 with Excel 2010

View 9 Replies View Related

2007- Loop Through Cells, Paste In Lowest Blank One

Oct 21, 2009

Need to create a simple loop, I have 50 cells (need to paste in lowest cell) - I need this to occur:

-Is cell A1 empty? if yes - paste
-some other stuff happens...
-Is cell A1 empty? no, is cell A2 empty? - paste
-some other stuff happens...
-Is cell A1 empty? no, is cell A2 empty?, no, is call A3 empty? -paste

View 3 Replies View Related

Need First Statement To Loop Until "False"

Jul 27, 2009

I have a worksheet built that obtains info from Sheet1(ACV) and enters it into Sheet2(Payment1), however when I run the process, only Line 10 fills and then it stops. I need to loop the first statement of the code, so that it fills all of column 1 until there is no info to fill then I assume the rest of the code will fill in the worksheet accordingly.

View 3 Replies View Related

Excel 2007 :: Create Visio Diagram From Excel?

May 9, 2014

I need to write a code to create visio flow diagram with excel inputs(Excel 2007). Attached is the requirement.

Requirement_Specification.docx

View 1 Replies View Related

Excel 2010 :: Condensing Dim Statement

May 7, 2013

Windows 8, Excel 2010,

What is a condensed way of saying;

Dim x As Integer
Dim y As Integer
Dim z1 As Integer
Dim z2 As Integer
Dim z3 As Integer
Dim z4 As Integer
Dim AA As Integer

View 9 Replies View Related

Excel 2010 :: ElseIf And Or Statement In VBA

Jun 27, 2013

I have tried to shorten my code by combining common tasks, however it runs but selects the wrong data. In the example below,

The code returns the expected if I select "M135X" and "300 hrs", but unexpected results if "M135X" and "1500 hrs" is selected

My variables are models and hours - the code below is for one model.

Code:
'Next Model
ElseIf Range("D3") = "M135X" And Range("E3") = "1st Service" Then
ThisWorkbook.Worksheets("M100X-135X").Range("B37:D44").Copy
Range("J10").Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

[Code] .........

View 5 Replies View Related

Excel Calculation - If Statement That Can Be Used On 2 Columns?

Oct 2, 2013

I need a If statement that can be used on 2 columns.

If there is a 1 in the first column I want to indicate if the 2nd column is 50% or greater with a number 1.

View 3 Replies View Related

Excel 2010 :: Change Color On If And Then Statement

Nov 20, 2013

I tried Conditional format and no luck. (Office 2010)

What I'm trying to do VB is:

If Cell B:B has value of "402" or "up"
and Cell C:C is less >100
Then Change color on Cell C:C to "yello, red, etc" (if C value is <100 no color change)

View 6 Replies View Related

Excel 2010 :: Runtime Error When Using Like In SQL Statement

Feb 5, 2014

Excel 2010. Windows 7

Run-time error '-2147467259 (80004005)':
An unexpected token ""20*"" was found following "1' AND
PERIOD = LIKE*. Expected tokens may include: "". SQLSTATE=42601
code died at RS.Open SQL, CN
/////////////////////////////////////////////////////////
Sub GET_OSI
Dim sn as String, osi as string, saposi As String

[code]....

View 2 Replies View Related

Excel 2013 :: If Statement To Generate Timestamp?

Feb 20, 2014

I am using Excel 2013 on a windows 8 machine. It may be easier for me to explain the problem, before posting about the formulas. I maintain a retirement residence with alot of phone numbers. I have a list of all the numbers we own in one column and the numbers assigned to the PBX systems in a second column. By using a If formula I can tell which numbers have not been assigned (The true statement). My problem is when a resident passes away, I need to reuse that "phone number", but I dont want to reuse the number too soon. I am hoping to use the "true" from the if statement to generate a time stamp. I plan on using that time stamp against the "today()" in a days formula, I believe this will spit out the number of days a number has been idle. The time stamp would be constantly changing or removed as the if statement would always be changing. So column C gives the True statement and says "Number not Assigned" - this should create a time stamp in Column W

A
B
C

[Code].....

View 4 Replies View Related

Excel Crash When Run Loop

Jun 3, 2012

I have problems with my excel crashing all the time when I try to run my code. It seems to happen in a loop where I open a .txt file and pull data from it, then close it, and open the next one...

There are no error code, just a message that Excel has stopped working and it will be closed, and then it is forced to restart. Just a mess!

This is my loop code:

Code:
Option Explicit
Dim Desktop As String
Sub Open60only()
Desktop = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
Dim which_folder As String, which_file As String, which_string As String, tick As String
Dim lastrow As Long
Dim wb1 As Workbook, wb2 As Workbook

[code].....

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved