+ Reply to Thread
Results 1 to 3 of 3

VBA Macro with numerous variables

  1. #1
    Registered User
    Join Date
    07-04-2018
    Location
    london
    MS-Off Ver
    2016
    Posts
    4

    VBA Macro with numerous variables

    Hi,

    I am new to macros, I managed to create the below macro which completes a task and seems to do it well.

    Now I want the macro to run another 90 times…but there are a number of variables that change. I have listed these in the macro.

    Any thoughts on how I could get this to work…without copying the macro and chancing the variables 90 times.

    The macro will work down a list in Excel where is will obtain the file names to open.

    Thanks for the help.

    Sub macrotocmpltetasks()

    Application.AskToUpdateLinks = False
    Application.DisplayAlerts = False


    'first one, adjusted from all others

    Dim rf As Workbook
    Set rf = ActiveWorkbook
    Sheets("Paste SUPPLIER").Select
    Columns("A:A").Select
    Selection.Copy
    Windows("AP SUPPLIER Invoices for CIS.xlsx").Activate
    Sheets("OU").Select
    Range("A1").Select
    ActiveSheet.Paste

    Sheets("AP SUPPLIER DETAILS").Select
    Range("A2:G" & Range("G" & Rows.Count).End(xlUp).row).AutoFilter Field:=1, Criteria1:=Sheets("OU").Range("A1").Value

    Range("C2:F5000").Select
    Application.CutCopyMode = False
    Selection.Copy

    rf.Activate
    Dim filelocation1a As String ‘file location changes to 2a
    Dim file As String
    Dim filepathfull1a As String ‘changes to 2a
    Filelocation1a = "B5" ‘ changes to 2a
    File1a = "B1" ‘changes to b2 and works down list
    filepathfull1a = Sheets("prep cis").Range(filelocation1a) & "\" & Sheets("paste Supplier").Range(file1a).Value & ".xlsm" ‘changes to 2a
    Workbooks.Open (filepathfull1a), UpdateLinks:=3 ‘changes to 2a

    Sheets("Rec").Select
    Range("A21").Select
    ActiveSheet.Paste

    Dim wp As Workbook ‘changes to wp2
    Set wp = ActiveWorkbook ‘changes to wp2


    'data detail

    rf.Activate

    Dim Filelocation1b As String ‘changes to 2b
    Dim File1b As String ‘changes to 2b
    Dim Filepathfull1b As String
    Filelocation1b = "F5" ‘changes to 2b
    File1b = "C1" ‘changes to c2 and works down list
    Filepathfull1b = Sheets("paste Supplier").Range(Filelocation1b) & "\" & Sheets("paste Supplier").Range(File1b).Value & ".txt" ‘changes to 2b
    Workbooks.Open (Filepathfull1b) ‘changes to 2b

    Dim dd As Workbook ‘changes to dd2
    Set dd = ActiveWorkbook ‘changes to dd2

    Columns("A:A").Select
    Selection.Copy
    wp.Activate ‘changes to wp2
    Sheets("Data detail").Select
    Range("I1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    dd.Close ‘changes to dd2

    'summary file

    rf.Activate

    Dim File1c As String
    Dim Filepathfull1c As String
    File1c = "D1" ‘changes to 2c and d2, works way down
    Filepathfull1c = Sheets("paste Supplier").Range(Filelocation1b) & "\" & Sheets("paste Supplier").Range(File1c).Value & ".txt" ‘changes to 2c
    Workbooks.Open (Filepathfull1c)

    Dim sf As Workbook ‘changs to sf2
    Set sf = ActiveWorkbook ‘changes to sf2

    Columns("A:A").Select
    Selection.Copy
    wp.Activate
    Sheets("Data summary").Select
    Range("I1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    sf.Close ‘changes to sf2

    wp.Activate ‘changes to wp2

    Sheets("Data detail").Select

    Application.Run "'Rename Files.xlsm'!cistxtcol"

    Dim wB As Workbook
    Dim nPath As String ‘changes to npath2
    nPath = ThisWorkbook.Sheets("Paste SUPPLIER").Range("F10").Value & ThisWorkbook.Sheets("Paste SUPPLIER").Range("E1").Value
    Set wB = Workbooks.Add

    With wB
    .SaveAs Filename:=nPath
    Dim np As Workbook
    Set np = ActiveWorkbook
    End With

    wp.Activate
    Sheets("Rec").Select
    Range("H4").Select
    Selection.Copy

    np.Activate
    Range("A1").Select
    ActiveSheet.Paste
    np.Save
    np.Close

    wp.Activate
    wp.Save
    wp.Close
    '--------------
    '--------------




    Application.AskToUpdateLinks = True
    Application.DisplayAlerts = True

    MsgBox "Task complete."

    End Sub

  2. #2
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: VBA Macro with numerous variables

    You'll be able to loop through a range to repeat the same actions but use the different variables, however a sample workbook would be required
    Please do add reputation where you see fit, it's nice to be nice and we all enjoy a pat on the back

    Please also mark your thread as solved once it has been.

  3. #3
    Forum Contributor
    Join Date
    09-21-2009
    Location
    Belgium - Mechelen
    MS-Off Ver
    Office 365 - version 2310
    Posts
    278

    Re: VBA Macro with numerous variables

    And pls put your code in CODE tags as per forum rule #3 :
    https://www.excelforum.com/forum-rul...rum-rules.html
    Even a little help can be a big help !!
    1. A sample workbook says more then words. Add problem description and solution so we can understand the problem.
    2. Your appreciation is accepted by clicking the star "Add Reputation" at the lower left of the post.
    3. If your problem is solved, mark it as [SOLVED]: See "FAQ : " https://www.excelforum.com/faq.php
    4. Use [CODE] [/CODE] tags to illustrate your code: see here

+ 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] Copy Numerous Sheets to Numerous Unopened Workbooks
    By hchavous in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-23-2018, 04:51 PM
  2. Consolidating Rows Based on Numerous Variables
    By pgwyther in forum Excel General
    Replies: 5
    Last Post: 07-10-2015, 12:20 PM
  3. [SOLVED] Call a macro numerous times
    By tharindudk in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-28-2014, 07:11 AM
  4. [SOLVED] Running a macro over numerous sheets
    By ScabbyDog in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-06-2013, 10:36 AM
  5. Using 1 Macro for numerous Worksheets
    By manutdcronaldo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2012, 11:37 AM
  6. COUNTIF given numerous conditions or use macro?
    By swanseaexcel in forum Excel General
    Replies: 4
    Last Post: 03-25-2011, 06:49 AM
  7. Macro to format numerous sheets into one
    By mccrimmon in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-18-2007, 05:35 AM

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