Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:21:07+00:00 2026-06-17T12:21:07+00:00

I am new to VBA and trying to write a simple macro. I have

  • 0

I am new to VBA and trying to write a simple macro. I have pasted my code below. Basically, I have 11 random numbers in cells B5 through B15. If this number is less than 0.65 I would like it to print TRUE in the column next to it. If it is greater than 0.65 I would like it to print FALSE in the column next to it. I think I basically have the code down, however a “Next t” in the Sub PrintandRead, t will have a value 5 through 15 (which is correct) however then it goes up to the markov function where t then returns to t=0. Why is it not carrying the “Next t” value up into the function?

Option Explicit

Function markov(pwd As Double, pww As Double) As Boolean
    Static wetYesterday As Boolean
    pwd = 0.4
    pww = 0.65
    Dim c As Double
    Dim t As Double
    If wetYesterday Then c = t - pww Else c = t - pwd
    If c <= 0 Then
        wetYesterday = True: markov = True
    Else
    wetYesterday = False: markov = False
    End If
End Function


Sub ReadAndPrint()
    Dim t As Double
    Dim p As Double
    Dim z(11) As Double
    Application.ScreenUpdating = False
    Worksheets("Sheet1").Activate
    p = 2
    For t = 5 To 15
        z(t - 4) = Cells(t, p)
    Next t
    p = p + 1
    For t = 5 To 15
    Cells(t, p) = markov(0.4, 0.65)
    'z(t - 4)
    Next t

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:21:08+00:00Added an answer on June 17, 2026 at 12:21 pm

    While you have a variable called t in your markov function, this is a different variable to the one in the main ReadAndPrint method. This is called scope – as you declare each variable within each method, it is scoped to that method. You can use the same name again elsewhere, but its a completely different variable (and could have a different type etc).

    The best thing you could do would be to pass t into the markov method as a parameter by changing the declaration:

    Function markov(pwd As Double, pww As Double,t as Double) As Boolean
    

    and calling it with

    Cells(t, p) = markov(0.4, 0.65, z(t - 4))
    

    You should also delete the Dim t As Double from the markov function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a file using VBA. My code is below. It worked
I am Trying to Transpose a range of cells in Excel through VBA macro
I am new to VBA in Excel. I'm setting up a simple macro Option
I am fairly new to Excel VBA and have been trying to look for
I have a really simple bit of code that I'm trying to in Ms
I'm trying create new object from a module class in VBA, and I have
I have been trying to make a VBA code to do the following. I
I'm trying to write a vba macro for a group tha has one workbook
I am new to vba and I have problem understanding the workbooks in vba
I am new to VBA but have previous experience with PHP programming logic and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.