+ Reply to Thread
Results 1 to 5 of 5

Change VBA Code via Button

  1. #1
    Registered User
    Join Date
    11-07-2016
    Location
    Barbados
    MS-Off Ver
    2010
    Posts
    24

    Change VBA Code via Button

    Hello,

    I am fairly new to VBA and coding. However I was able to create a macro to copy data from one file into another, save as an excel file as well as an XML file.
    While the macro works perfectly fine, I wanted to know if there was any way to create a button to change information in the code itself. For example, the date and the title of the file.
    You see, within the code, I have to change the dates to reflect the period of data that I am updating, which should also be reflected in the name of the file. I know I can do a file and replace in the code to change the information to reflect the dates and naming conventions I’d like to use for the updated information.

    But is there any way to change the code via a button?

    For example, have a command box to change the date, and to rename the file without having to manually change this in the code every time.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Change VBA Code via Button

    Why don't you prompt the user for the required dates/values that need to be changed, store those values in variables and then use the variables in the code?

    Or you could enter the dates/values in cells on a worksheet and have the code pull the values from the sheet.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    11-07-2016
    Location
    Barbados
    MS-Off Ver
    2010
    Posts
    24

    Re: Change VBA Code via Button

    Quote Originally Posted by Norie View Post
    Why don't you prompt the user for the required dates/values that need to be changed, store those values in variables and then use the variables in the code?

    Or you could enter the dates/values in cells on a worksheet and have the code pull the values from the sheet.
    Thanks for the response, but how exactly can I do this? Like I said I fairly new to VBA so I'm not exactly sure how to go about doing this.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Change VBA Code via Button

    Can you post the current code and indicate the parts of that need to be changed?

  5. #5
    Registered User
    Join Date
    11-07-2016
    Location
    Barbados
    MS-Off Ver
    2010
    Posts
    24

    Re: Change VBA Code via Button

    Quote Originally Posted by Norie View Post
    Can you post the current code and indicate the parts of that need to be changed?
    Hello again.

    I actually figured out a way to get the date prompt in. I used the below function:

    Dim myValue As Variant
    myValue = InputBox("Enter First Period Date")
    Range("G9:I9").Value = myValue

    This is working perfectly, what I am now struggling with is the prompt to open the source file. The code I was trying to use would open the directory/file path but upon opening the file, (which is a csv file) it won't open. See below code:

    ChDrive "J:\"
    ChDir "J:\TABLES\BAP Loading\Source Files"
    fileToOpen = Application _
    .GetOpenFilename("All Files , *.*")

    I tried changing the .GetOpenFilename("All Files , *.*") to .GetOpenFilename("CSV Files, *.csv") but that won't work. I can't figure out why after I've selected the file it won't open.

    Essentially I want a prompt to open the source file, as its going to change every month, as well as a prompt to save the file name I as choose fit, I found the code to prompt the user to enter the date, but I need something that would help open the source file that I want to utilize and a prompt to save the files with the name I wish. As you can see I’m saving the file as an excel in one location and XML in another.

    See below.

    '
    Workbooks.Open Filename:= _
    "J:\TABLES\BPA Loading\Source Files\October LOADING FILE.csv"

    Workbooks.Open Filename:= _
    "J:\TABLES\BPA Loading\BPA Template.xls"
    Range("G9:I9").Select
    Dim myValue As Variant
    myValue = InputBox("Enter First Period Date")
    Range("G9:I9").Value = myValue
    Sheets("Messages").Select
    Windows("LOADING FILE.csv").Activate
    ActiveWindow.SmallScroll Down:=-6
    Range("A2:V50000").Select
    Selection.Copy
    Windows("BPA Template.xls").Activate
    Range("A10").Select
    ActiveSheet.Paste
    Sheets("Front Sheet").Select
    Application.CutCopyMode = False
    ChDir _
    "J:\TABLES\BPA Template Excel"
    ActiveWorkbook.CheckCompatibility = False
    ActiveWorkbook.SaveAs Filename:= _
    "J:\TABLES\BPA Loading\BPA Template Excel\October_Loading_P1.xls" _
    , FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
    ReadOnlyRecommended:=False, CreateBackup:=False
    ChDir _
    "J:\TABLES\BPA Loading\Converted XML File"
    ActiveWorkbook.SaveAsXMLData Filename:= _
    "J:\TABLES\BPA Loading\Converted XML File\October_Loading_P1.xml" _
    , Map:=ActiveWorkbook.XmlMaps("FormTR-TFA_Map")
    ActiveWindow.Close
    Workbooks.Open Filename:= _
    "J:\TABLES\BPA Loading\STP Template\BPA Template.xls"
    Range("G9:I9").Select
    Dim myValue As Variant
    myValue = InputBox("Enter Second Period Date")
    Range("G9:I9").Value = myValue
    Sheets("Messages").Select
    Windows("October LOADING FILE.csv").Activate
    Range("A50001:V100000").Select
    Selection.Copy
    Windows("BPA Template.xls").Activate
    Range("A10").Select
    ActiveSheet.Paste

    Sheets("Front Sheet").Select
    Application.CutCopyMode = False
    ChDir _
    "J:\TABLES\BPA Loading\BPA Template Excel"
    ActiveWorkbook.CheckCompatibility = False
    ActiveWorkbook.SaveAs Filename:= _
    "J:\TABLES\ BPA Loading\BPA Template Excel\October_Loading_P2.xls" _
    , FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
    ReadOnlyRecommended:=False, CreateBackup:=False
    ChDir _
    "J:\TABLES\BPA Loading\Converted XML File"
    ActiveWorkbook.SaveAsXMLData Filename:= _
    "J:\TABLES\BPA Loading\Converted XML File\October_Loading_P2.xml" _
    , Map:=ActiveWorkbook.XmlMaps("FormTR-TFA_Map")
    ActiveWindow.Close
    ActiveWindow.Close
    End Sub
    Last edited by Livvi; 02-15-2017 at 05:27 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Button code to change offset cell in different sheet
    By 23inzane10 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-17-2016, 02:32 AM
  2. Button sending email code - can any change it with slight variation?
    By ld2x07 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-04-2014, 12:13 PM
  3. Write VBA code to change value of a Spin button
    By sontranhus in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-07-2014, 04:22 AM
  4. code for option button to change label colour
    By 600720544 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-04-2014, 03:23 PM
  5. [SOLVED] How can I change this worksheet code to a button hit?
    By JJBennett in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-28-2013, 11:43 AM
  6. Code to change button
    By ISUCyclones in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-25-2013, 04:12 PM
  7. Code for button errors due to worksheet name change
    By ge0rge in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-15-2008, 12:22 PM

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