You are here:

Excel/Performing an If check and then entering different values in different cells based on said if.


Hi Tom,

I am able to create a button that prints a selected range on my sheet.
However after it prints I would like the button to also initialize an If statement that checks the value in cell B1, which will be a time in 09:00 AM format, and based on what the time is it will then select the corresponding cell on a different part of the same sheet and in this cell I want it to place the value of cell B4. So if the time is 10am then on the cell that corresponds to ten am call it D4 it should enter whatever was written in cell B4, which will be an address.

Thank you for your help.


Your description lacks most of the details.
Here is some pseudo code that should point you in the right direction.  Hopefully you can adapt it to your needs.

Regardless of the format of B1, I assume that B1 holds an actual Excel time value.

Sub ABC()
Dim dt As Date
dt = Range("B1")
Select Case dt
Case TimeSerial(8, 0, 0) To TimeSerial(8, 59, 59)
  Range("C4").Value = Range("B4").Value
Case TimeSerial(9, 0, 0) To TimeSerial(9, 59, 59)
  Range("D4").Value = Range("B4").Value
Case TimeSerial(10, 0, 0) To TimeSerial(10, 59, 59)
  Range("E4").Value = Range("B4").Value
Case TimeSerial(11, 0, 0) To TimeSerial(11, 59, 59)
  Range("F4").Value = Range("B4").Value
Case TimeSerial(12, 0, 0) To TimeSerial(12, 59, 59)
  Range("G4").Value = Range("B4").Value
Case TimeSerial(13, 0, 0) To TimeSerial(13, 59, 59)
  Range("H4").Value = Range("B4").Value
Case TimeSerial(14, 0, 0) To TimeSerial(14, 59, 59)
  Range("I4").Value = Range("B4").Value
Case TimeSerial(15, 0, 0) To TimeSerial(15, 59, 59)
  Range("J4").Value = Range("B4").Value
Case TimeSerial(16, 0, 0) To TimeSerial(16, 59, 59)
  Range("K4").Value = Range("B4").Value
End Select
End Sub

Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2017 All rights reserved.