Solved

Do loop question

Posted on 2013-12-30
6
244 Views
Last Modified: 2013-12-30
Hi Guys, if I have a Else if statement in a Do Loop and it satisfies the criteria: eg see below
and I want to put a Vlookup Formula in. How do I change the Range C4603 Vlookup  as a variable ?
Dim i As Integer
Worksheets("Rec").Select

i = 5


While Cells(i, 5).Value <> ""

ElseIf Cells(i, 18).Value = "4438272" Then

Cells(i, 27).Formula = "=IF(ISNA(VLOOKUP(C4603,'Bond pricing'!B:M,12,FALSE)),0,VLOOKUP(C4603,'Bond pricing'!B:M,12,FALSE))*0"
0
Comment
Question by:Justincut
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Simon Ball
ID: 39746234
its just an IF statement in the Do ..Loop.

for the 2nd part of the question you'll need to declare a range and set it = to the range you wish, and pass that if your working with the range in VBA...

as you're outputting a string with a range in, you can just declare a string, set it = to the value you require, and concatentate it into the "=if" string

e.g.
dim myVlookup as string
myVlookup = "VLOOKUP(C4603,'Bond pricing'!B:M,12,FALSE)"

dim myCellFormula as string
'CellFormula = "=IF(ISNA(VLOOKUP(C4603,'Bond pricing'!B:M,12,FALSE)),0,VLOOKUP(C4603,'Bond pricing'!B:M,12,FALSE))*0"

myCellFormula = "=IF(ISNA(" & myVlookup  & "),0," & myVlookup  & "))*0"

Cells(i, 27).Formula =myCellFormula 

Open in new window

0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 39746281
Is the C4603 part supposed to relate to the i counter in some way? If so, you can use:
Cells(i, 27).Formula = "=IF(ISNA(VLOOKUP(C" & i & ",'Bond pricing'!B:M,12,FALSE)),0,VLOOKUP(C" & i & ",'Bond pricing'!B:M,12,FALSE))*0"

Open in new window


for example.
0
 

Author Comment

by:Justincut
ID: 39746403
Can't you use offset eg.

While Cells(i, 5).Value <> ""

ElseIf Cells(i, 18).Value = "4438272" Then

Cells(i, 27).Formula = "=IF(ISNA(VLOOKUP(Offset[-24]),'Bond pricing'!B:M,12,FALSE))?

The Vlookup is looking at a range 24 columns to its left thus Offset [-24]
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Justincut
ID: 39746537
The C4603 part has nothing to do with it. Its Looping down Column 27 and the Vlookup is looking at Column C. The row depends on whether the If statement is satisfied or not,
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39746741
That comment makes no sense to me whatsoever, I'm afraid. If the C4603 part has nothing to do with it, why have you replaced it with Offset(-24) in your formula?
0
 

Author Comment

by:Justincut
ID: 39746901
Hi, on my Rec, row 4603 triggered the Macro as the IF statement was satisfied. It put a Vlookup in column 27 which was looking up a range in column 3. Thus AD4603 would have looked up a Range in C4603, which is 24 columns to left.I have to do this Rec everyday and its on a different row everyday, that's why it has to be a variable.

IF(ISNA(VLOOKUP(Offset[-24]),'Bond pricing'!B:M,12,FALSE))?,
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question