473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Range...Excel how to replicate same code

this code works very well for the one cell B12, where selection made reflects message change in C12.
i want to do it for series of rows.
Like B13, C13 and then B14, C14 , .....and so on.

can someone please help


If Range("B12") = "N/A" Then ' Drop down value selection
Range("C12") = "Details required"
ElseIf Range("B12") = "NO" Then
Range("C12") = "Details required"
ElseIf Range("B12") = "YES" Then
Range("C12") = " "
Else
Range("C12") = " "
End If
Jun 21 '10 #1
11 1619
patjones
931 Expert 512MB
I would just open a module window in VBA and write out this function:

Expand|Select|Wrap|Line Numbers
  1. Public Function GetMessage(strRange) As String
  2.  
  3. Select Case Range(strRange)
  4.     Case "NO", "N/A"
  5.         GetMessage = "Details Required"
  6.     Case "YES"
  7.         GetMessage = ""
  8. End Select
  9.  
  10. End Function

Then in the cells in question you can call this function. For instance, in cell C12 you'll write =GetMessage("B12"), and so on. In order to make this work I had to do Application.CalculateFull in the Worksheet_Change event for the sheet that I did this on.

Pat
Jun 21 '10 #2
i dont understand what do you mean by Application.calculate.....
Jun 21 '10 #3
patjones
931 Expert 512MB
Whenever you make a change to one or more cells on an Excel worksheet, this triggers the worksheet's Change event. Inserting the line Application.CalculateFull inside the Change event as such:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.  
  3. Application.CalculateFull
  4.  
  5. End Sub

...causes everything on the worksheet to be re-calculated, including user-defined functions. If you didn't include this, then Excel would not, for example, re-evaluate cell C12 after making a new selection in cell B12.

Pat
Jun 21 '10 #4
@zepphead80
i am calling the funtion but getting #name error
Jun 22 '10 #5
patjones
931 Expert 512MB
Are you writing =GetMessage("B12"), =GetMessage("B13"), and so on?
Jun 22 '10 #6
@zepphead80
i am writing
=GetMessage(B12) and then i have tried =GetMessage("B12")

both gives me #Name error.
I have added necessary Add-Ins but still getting same error
Jun 22 '10 #7
patjones
931 Expert 512MB
When you wrote out the function definition did you put it in a VBA module called "Module 1" and compile the code?
Jun 22 '10 #8
@zepphead80
i created Module 1 and wrote the following (and compiled)


Public Function GetMessage(strRange) As String

Select Case Range(strRange)
Case "NO", "N/A"
GetMessage = "Details Required"
Case "YES"
GetMessage = ""
End Select

End Function


Private Sub Worksheet_Change(ByVal Target As Range)

Application.CalculateFull

End Sub
Jun 22 '10 #9
patjones
931 Expert 512MB
OK, this code

Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.  
  3. Application.CalculateFull
  4.  
  5. End Sub 

needs to be put in the code window for Sheet1, not in Module1 with the function definition. Also, you might be having a problem with macros not being enabled for your file, though I think this is less likely to be the problem.

Pat
Jun 22 '10 #10
Thanks so much zepphead80, you were so helpfull. its working now. I learned alot from it and it refreshed my knowledge also.
thanks again
Jun 23 '10 #11
patjones
931 Expert 512MB
It's no problem at all. I'm happy to have helped!

Pat
Jun 24 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ShyGuy | last post by:
Is it possible to export a report to an excel spreadsheet using code. Thanks for any help.
3
by: Mansi | last post by:
I'm trying to use excel automation (from visual c# .NET) to fill data in a range by using arrays. I am using Microsoft's Knowledge Base Article 302096 as a reference and using the example listed...
2
by: Maik | last post by:
Hi everybody, I want to automate an Excel97 application using C#. I use the excel8.olb file as COM interface. How can I check the country code of the installed Excel application? I know that there...
1
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access...
1
by: MAdcock | last post by:
I have been having major problems trying to write a piece of code to basically tally (add) values in a table (column B) that have the same product name (column A). The problem is that the table is...
2
by: dan NY | last post by:
I'm a struggling new VB Applications user that has what may be a simple question, but I've been struggling with it. I'm using a response to a message box YesNo question to cause one of two...
1
by: =?Utf-8?B?QnJpYW5ESA==?= | last post by:
Guys I am working with an excel workbook and have 99% complete however there is one line that is keeping me from fully automating this proecess. Code: Excel.Application excelApp = new...
0
by: Shikha1234 | last post by:
Requirement - Excel sheet should be populated with data from the database Issue - Excel sheet dialog box is opening with Open, save, Cancel. But, there is again a dialog box which says that the...
0
by: vbanewb14 | last post by:
Hi, I am new to programming so please bear with me. I have pieced together a code which accomplished what I want it to do (creates bar graphs with a target line). Now I am just trying to simplify the...
0
by: maheshhs2001 | last post by:
For example, Iam opened an Access file, internet explorer, and an excel file, What I required is which one of the file is currently active and we need to record how much time the window was active ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.