+ Reply to Thread
Results 1 to 6 of 6

Having a changing date inside a macro or VBA code

  1. #1
    Registered User
    Join Date
    02-08-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    5

    Having a changing date inside a macro or VBA code

    Hello,

    New to the forum and to VBA in general. Have a question about how to have a cell have a variable date that then changes the macro run depending on that date? For example....lets say the date is in B2 and I am pulling information from a website that is date dependent, then how do I do that macro. I will paste the code I have below and hopefully someone can help me in deciding what is wrong with it. When I try and run it it says Variable not found on "B2". Not sure if there are other problems in the code but I do know that if I put in a actual date where I have the text (B2, etc) field then the code works. Any help would be much appreciated!

    Sub Button1_Click()

    ' Losses Macro
    ' MLC for whatever day
    '

    Workbooks.Open Filename:= _
    "https://www.midwestiso.org/Library/Repository/Market%20Reports/" & Text(B2, "yyyymmdd") & "_da_lmp.csv"
    ActiveWindow.Visible = False
    Windows(" & Text(B2, "yyyymmdd") & "_da_lmp.csv").Visible = True

    Rows("3532:5294").Select
    Selection.Copy
    Windows("Monthly MLC avg.xlsm").Activate
    ActiveWindow.WindowState = xlNormal
    ActiveWindow.WindowState = xlNormal
    ActiveWindow.WindowState = xlMaximized
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("AD17").Select
    End Sub

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Having a changing date inside a macro or VBA code

    Instead of B2 mention it as Range("B2").Value

    Please Login or Register  to view this content.


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Registered User
    Join Date
    02-08-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Having a changing date inside a macro or VBA code

    Thank you Sixthsense for you help. I am still having issues as when I leave the word text in there I keep getting a error saying that the sub or function is not defined. When I take the text word out it gives me a compile error of expected: named parameter around the date formatting. Not sure if you could help me get around this issue? Thank you again for your help. I pasted the code below that seems to be close.

    Sub Button1_Click()
    '
    ' Losses Macro
    ' MLC for whatever day
    '


    Workbooks.Open Filename:= _
    "https://www.midwestiso.org/Library/Repository/Market%20Reports/" & Range("B2").Value, "yyyymmdd") & "_da_lmp.csv"
    ActiveWindow.Visible = False
    Windows(Format(Range("B2"), "yyyymmdd") & "_da_lmp.csv").Visible = True

    Rows("3532:5294").Select
    Selection.Copy
    Windows("Monthly MLC avg.xlsm").Activate
    ActiveWindow.WindowState = xlNormal
    ActiveWindow.WindowState = xlNormal
    ActiveWindow.WindowState = xlMaximized
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("AD17").Select
    End Sub

  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Having a changing date inside a macro or VBA code

    Missed the Format Function in your above code refer the below code

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-08-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Having a changing date inside a macro or VBA code

    Thank you SO much sixthsense! This issue is solved.

  6. #6
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Having a changing date inside a macro or VBA code

    Glad it helps you and thanks for the feedback

+ 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