Multiple If And Then VBA code

Daeksoul

New Member
Joined
May 5, 2017
Messages
22
Hi guys,
New to the forum, and also pretty much a newbie when it comes to Formula/VBA for Excel.

I'm trying to get some VBA code working (as I know I would be unable to do what I need with a formula, due to the size of it) but I'm not sure where to start.

Basically, I'll lay it out logically;

IF C4 = TextValue AND F4 = NumericValue THEN I4 = Value

However, it would be a case of having multiple of the above, for different values for C4 and F4, all outputting to I4, for example;

IF C4 = "Gold" AND F4 = "2" THEN I4 = "12 Hours"
IF C4 = "Silver" AND F4 = "3" THEN I4 = "8 Hours"

If you can see where I'm going with this? It's a little difficult to explain what I need, other than writing it like this..

I'll also need to know how to assign the above VBA code to the cell I4 so that it outputs the right value, unless that would be automatic with it saying I4 = Value?

Any help would be greatly appreciated.

Kind regards,
 
Re: Complicated? Multiple If And Then VBA code

The code I posted, and tested, works for me.

Where exactly did you put it?
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Re: Complicated? Multiple If And Then VBA code

The code I posted, and tested, works for me.

Where exactly did you put it?

Here's a file with the working code in it.

https://app.box.com/s/lwkedxc83sids4elsmm1hhwo2c0jeeht

Note, I made some very minor changes to the code so it doesn't match exactly what I posted.


I put the code in 'Sub Button99_Click()'


I've downloaded your sheet and had a look - Think it might be because I've put the code INSIDE the button, rather than on the sheet.. I'm going to try it that way and see what happens - you may have just figured out the issue, haha. Thanks!
 
Upvote 0
Re: Complicated? Multiple If And Then VBA code

The code in the workbook isn't on the sheet, it's in a standard module and it's been assigned to the button.
 
Upvote 0
Re: Complicated? Multiple If And Then VBA code

The code in the workbook isn't on the sheet, it's in a standard module and it's been assigned to the button.

Aye, I noticed that.

So, I tried to replicate it, but it just outputs 'Nothing Selected' after amending it for my sheet.

Could it possibly be because I have merged cells? I've been wondering this for a while, even though when I select said merged cells (C4 - merged with D4 and F4 merged with G4) is shows them as Just C4 and F4.

Though, when I think about it, it outputs to I4 (which is a merged cell also) perfectly fine.. so maybe not. I'll look at the sheet-stuff again.. I might need to rename it or something.
 
Upvote 0
Re: Complicated? Multiple If And Then VBA code

You should avoid merged cells whenever possible - in this case you could use the Center across selection alignment optiont.

However I can't see how merged cells would be a problem here, in fact I just merged C4:D4 and F4:G4 in the workbook I uploaded an the code still works.

PS Did the uploaded workbook work?
 
Upvote 0
Re: Complicated? Multiple If And Then VBA code

You should avoid merged cells whenever possible - in this case you could use the Center across selection alignment optiont.

However I can't see how merged cells would be a problem here, in fact I just merged C4:D4 and F4:G4 in the workbook I uploaded an the code still works.

PS Did the uploaded workbook work?

The uploaded workbook worked perfectly, precisely what I needed - however when I transfer it over to mine, it just outputs 'Not selected'.
 
Upvote 0
Re: Complicated? Multiple If And Then VBA code

Can you upload your workbook to a file-sharing site like box.net and post a link to it here?
 
Upvote 0
Re: Complicated? Multiple If And Then VBA code

Can you upload your workbook to a file-sharing site like box.net and post a link to it here?

I'm not able to do that unfortunately, due to the data contained on the worksheet - It's a section of 1 sheet of a workbook. :(
 
Upvote 0
Re: Complicated? Multiple If And Then VBA code

Sorry can't really help further without seeing your worbook.

Is there any chance you could remove sensitive date and then upload it somewhere?
 
Upvote 0

Forum statistics

Threads
1,215,330
Messages
6,124,305
Members
449,150
Latest member
NyDarR

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top