+ Reply to Thread
Results 1 to 4 of 4

Loops using multiple variables

  1. #1
    Valued Forum Contributor
    Join Date
    01-10-2011
    Location
    swindon, england
    MS-Off Ver
    Excel 2010
    Posts
    317

    Smile Loops using multiple variables

    Hi

    I am trying to create a macro which does mutiple things and i am having trouble with it and i was wondering if anyone can help me?

    I have currently got a the macro in the notepad document i have attachted.

    However i need to change this so instead of looping for that strFile variable it loops through the following file names:

    Graphing_MTH_Actual_Curr_Year" & "*.csv
    Graphing_MTH_Actual_Prev_Year" & "*.csv
    Graphing_YTD_Actual_Curr_Year" & "*.csv
    Graphing_YTD_Actual_Prev_Year" & "*.csv
    Graphing_R12_Actual_Curr_Year" & "*.csv
    Graphing_R12_Actual_Prev_Year" & "*.csv

    But i also need to change it so instead of just pasting it into the MTH sheet it pastes it in different sheets depending on the file name. So:
    • If the filename is Graphing_MTH_Actual_Curr_Year" & "*.csv it pastes the data in MTH
    • If the filename is Graphing_MTH_Actual_Prev_Year" & "*.csv it pastes the data in MTHPrevious
    • If the filename is Graphing_YTD_Actual_Curr_Year" & "*.csv it pastes the data in YTD
    • If the filename is Graphing_YTD_Actual_Prev_Year" & "*.csv it pastes the data in YTDPrevious
    • If the filename is Graphing_R12_Actual_Curr_Year" & "*.csv it pastes the data in R12
    • If the filename is Graphing_R12_Actual_Prev_Year" & "*.csv it pastes the data in R12Previous

    I hope this makes sense. I am having really trouble with this as i have got it to loop through the strFldr and lookup strFile and copy and paste the correct data into the MTH tab but i can not work out how to get it to loop through multiple factors. I have thought about using select case statements and loops within loops but i am not sure how either of them work.

    Can anyone help me?

    Thanks

    Jeskit
    Attached Files Attached Files
    Last edited by jeskit; 04-11-2011 at 05:31 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    01-10-2011
    Location
    swindon, england
    MS-Off Ver
    Excel 2010
    Posts
    317

    Smile Re: Loops using multiple variables

    Hi

    I have re-done my macro using a select case statements and i have got it to work to a degree. It works and runs without error except it either does not find the files or is skipping the case statements but i unsure of which. I am not very good with select case statements so i have most likly written it wrong but i am not sure.

    Option Explicit

    Sub AverageGraph()
    Dim i As String
    Dim l As String
    Dim wbCsv As Workbook
    Dim wsMyCsvSheet As Worksheet
    Dim lNextrow As Long
    Dim strFile As String
    Dim strFile1 As String
    Dim strFile2 As String
    Dim strFile3 As String
    Dim strFile4 As String
    Dim strFile5 As String
    Dim strFldr As String

    i = Range("B7").Value
    l = Range("B8").Value

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Application.Workbooks.Open "C:\Documents and Settings\SeymourJ\Desktop\GraphingChartTemplate.xlsx"

    Application.Workbooks.Open "C:\Documents and Settings\SeymourJ\Desktop\Actual_Participation_02_2011.xls"

    Workbooks("Actual_Participation_02_2011.xls").Sheets(1).Range("A2:A1000").Copy Destination:=Workbooks("GraphingChartTemplate.xlsx").Sheets("Graphing").Range("B3")

    Workbooks("Actual_Participation_02_2011.xls").Close

    ActiveWorkbook.Sheets("Settings").Select

    Range("B6").Value = i
    Range("B7").Value = l

    strFldr = "C:\Documents and Settings\SeymourJ\My Documents\Tasks"

    strFile = Dir(strFldr & "Graphing_MTH_Actual_Curr_Year" & "*.CSV")
    strFile1 = Dir(strFldr & "Graphing_MTH_Actual_Prev_Year" & "*.CSV")
    strFile2 = Dir(strFldr & "Graphing_YTD_Actual_Curr_Year" & "*.CSV")
    strFile3 = Dir(strFldr & "Graphing_YTD_Actual_Prev_Year" & "*.CSV")
    strFile4 = Dir(strFldr & "Graphing_R12_Actual_Curr_Year" & "*.CSV")
    strFile5 = Dir(strFldr & "Graphing_R12_Actual_Prev_Year" & "*.CSV")


    Application.Calculation = xlCalculationManual

    lNextrow = 2

    Select Case ActiveCell.Value

    Case 1
    Do
    Set wbCsv = Workbooks.Open(Filename:=strFldr & "\" & strFile)
    Set wsMyCsvSheet = wbCsv.Sheets(1)
    With Workbooks("GraphingChartTemplate.xlsx").Sheets("MTH")
    wsMyCsvSheet.Range("A2:M14").Copy
    Workbooks("GraphingChartTemplate.xlsx").Sheets("MTH").Cells(lNextrow, 2).PasteSpecial

    End With
    lNextrow = lNextrow + 14

    'close it
    wbCsv.Close

    'go to next file
    strFile = Dir
    Application.StatusBar = strFile
    Loop Until Len(strFile) = 0

    Case 2
    Do
    Set wbCsv = Workbooks.Open(Filename:=strFldr & "\" & strFile1)
    Set wsMyCsvSheet = wbCsv.Sheets(1)
    With Workbooks("GraphingChartTemplate.xlsx").Sheets("MTHPrevious")
    wsMyCsvSheet.Range("A2:M14").Copy
    Workbooks("GraphingChartTemplate.xlsx").Sheets("MTHPrevious").Cells(lNextrow, 2).PasteSpecial

    End With
    lNextrow = lNextrow + 14

    'close it
    wbCsv.Close

    'go to next file
    strFile1 = Dir
    Application.StatusBar = strFile1
    Loop Until Len(strFile1) = 0

    Case 3

    Do
    Set wbCsv = Workbooks.Open(Filename:=strFldr & "\" & strFile2)
    Set wsMyCsvSheet = wbCsv.Sheets(1)
    With Workbooks("GraphingChartTemplate.xlsx").Sheets("YTD")
    wsMyCsvSheet.Range("A2:M14").Copy
    Workbooks("GraphingChartTemplate.xlsx").Sheets("YTD").Cells(lNextrow, 2).PasteSpecial

    End With
    lNextrow = lNextrow + 14

    'close it
    wbCsv.Close

    'go to next file
    strFile2 = Dir
    Application.StatusBar = strFile2
    Loop Until Len(strFile2) = 0

    Case 4

    Do
    Set wbCsv = Workbooks.Open(Filename:=strFldr & "\" & strFile3)
    Set wsMyCsvSheet = wbCsv.Sheets(1)
    With Workbooks("GraphingChartTemplate.xlsx").Sheets("YTDPrevious")
    wsMyCsvSheet.Range("A2:M14").Copy
    Workbooks("GraphingChartTemplate.xlsx").Sheets("YTDPrevious").Cells(lNextrow, 2).PasteSpecial

    End With
    lNextrow = lNextrow + 14

    'close it
    wbCsv.Close

    'go to next file
    strFile3 = Dir
    Application.StatusBar = strFile3
    Loop Until Len(strFile3) = 0

    Case 5

    Do
    Set wbCsv = Workbooks.Open(Filename:=strFldr & "\" & strFile4)
    Set wsMyCsvSheet = wbCsv.Sheets(1)
    With Workbooks("GraphingChartTemplate.xlsx").Sheets("R12")
    wsMyCsvSheet.Range("A2:M14").Copy
    Workbooks("GraphingChartTemplate.xlsx").Sheets("R12").Cells(lNextrow, 2).PasteSpecial

    End With
    lNextrow = lNextrow + 14

    'close it
    wbCsv.Close

    'go to next file
    strFile4 = Dir
    Application.StatusBar = strFile4
    Loop Until Len(strFile4) = 0

    Case 6

    Do
    Set wbCsv = Workbooks.Open(Filename:=strFldr & "\" & strFile5)
    Set wsMyCsvSheet = wbCsv.Sheets(1)
    With Workbooks("GraphingChartTemplate.xlsx").Sheets("R12Previous")
    wsMyCsvSheet.Range("A2:M14").Copy
    Workbooks("GraphingChartTemplate.xlsx").Sheets("R12Previous").Cells(lNextrow, 2).PasteSpecial

    End With
    lNextrow = lNextrow + 14

    'close it
    wbCsv.Close

    'go to next file
    strFile5 = Dir
    Application.StatusBar = strFile5
    Loop Until Len(strFile5) = 0

    End Select

    End Sub

    Does anyone know why it is not finding the files or skipping the select case statement?

    Thank you

    Jeskit

  3. #3
    Valued Forum Contributor
    Join Date
    08-26-2006
    Location
    -
    MS-Off Ver
    2010
    Posts
    388

    Re: Loops using multiple variables

    I'd do something like this, far simpler, use 2 arrays and a loop to process each pair:
    Please Login or Register  to view this content.
    Last edited by T-J; 04-17-2011 at 06:09 AM.

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Loops using multiple variables

    jeskit

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

+ 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