+ Reply to Thread
Results 1 to 3 of 3

How can I write this code in VBA

  1. #1
    Registered User
    Join Date
    01-03-2005
    Posts
    8

    How can I write this code in VBA

    The code below works but is limited in its current form for what I need to do. Can anyone either show me the correct way to write this in VBA or point me in the right direction.

    myVal =IF(AND(B8>='TimePeriod-table'!A1,B8<'TimePeriod-table'!B1),'TimePeriod-table'!C1,"")

    Basically its saying if the value in current worksheet "mycell-A" is greater than or equal to "mycell-A" and less than "Mycell-B" in another worksheet, then value of myVal = "mycell-C".

    I can't believe that this can beat the best of you!!!!

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Adventurous,

    With Worksheets("Time Period Table")
    ValA = .Range("A1").Value
    ValB = .Range("B1").Value
    ValC = .Range("C1").Value
    End With

    TestVal = ActiveSheet.Range("B8")

    If TestVal >= ValA And TestVal <= ValB Then
    TestVal = ValC
    Else
    TestVal = ""
    End If

    _________________________________________________________________

    There's your answer,
    Leith Ross
    Last edited by Leith Ross; 02-20-2005 at 08:01 AM.

  3. #3
    Registered User
    Join Date
    01-03-2005
    Posts
    8

    Thanks, this is how my macro looks

    but needs to be faster, any suggestions?

    Dim CheckSh As Variant
    Dim TargetSh As Variant
    Dim SearchVal As Variant
    Dim rw1 As Long
    Dim rw2 As Long
    Dim myMsg As Variant

    CheckSh = "TimePeriods"
    TargetSh = "Trades"

    For rw1 = 3 To Worksheets(TargetSh).Cells(65536, 3).End(xlUp).Row

    SearchVal = Worksheets(TargetSh).Range("C" & rw1).Value


    For rw2 = 1 To Worksheets(CheckSh).Cells(65536, 1).End(xlUp).Row

    If SearchVal >= Worksheets(CheckSh).Range("A" & rw2) And SearchVal < Worksheets(CheckSh).Range("B" & rw2) Then Worksheets(CheckSh).Range("C" & rw2).Copy

    Worksheets(TargetSh).Range("A" & rw1).Select
    Worksheets(TargetSh).Paste

    End If

    Next rw2


    Next rw1

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1