+ Reply to Thread
Results 1 to 34 of 34

Do/Loop Until using variables declared

Hybrid View

LLL0422 Do/Loop Until using variables... 06-03-2014, 09:16 AM
Norie Re: Do/Loop Until using... 06-03-2014, 09:26 AM
LLL0422 Re: Do/Loop Until using... 06-03-2014, 09:54 AM
Norie Re: Do/Loop Until using... 06-03-2014, 10:00 AM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 09:02 AM
Norie Re: Do/Loop Until using... 06-04-2014, 09:06 AM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 09:16 AM
Norie Re: Do/Loop Until using... 06-04-2014, 10:03 AM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 10:18 AM
Norie Re: Do/Loop Until using... 06-04-2014, 10:27 AM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 10:32 AM
Norie Re: Do/Loop Until using... 06-04-2014, 10:37 AM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 10:46 AM
Norie Re: Do/Loop Until using... 06-04-2014, 11:03 AM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 11:10 AM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 11:29 AM
Norie Re: Do/Loop Until using... 06-04-2014, 11:41 AM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 11:51 AM
Norie Re: Do/Loop Until using... 06-04-2014, 11:57 AM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 11:58 AM
Norie Re: Do/Loop Until using... 06-04-2014, 12:01 PM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 12:08 PM
Norie Re: Do/Loop Until using... 06-04-2014, 12:11 PM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 12:13 PM
Norie Re: Do/Loop Until using... 06-04-2014, 12:18 PM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 12:22 PM
Norie Re: Do/Loop Until using... 06-04-2014, 12:30 PM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 12:36 PM
Norie Re: Do/Loop Until using... 06-04-2014, 12:41 PM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 12:45 PM
LLL0422 Re: Do/Loop Until using... 06-04-2014, 02:54 PM
Norie Re: Do/Loop Until using... 06-04-2014, 03:52 PM
LLL0422 Re: Do/Loop Until using... 06-05-2014, 08:48 AM
Norie Re: Do/Loop Until using... 06-05-2014, 09:01 AM
  1. #1
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Do/Loop Until using variables declared

    How do I structure a Do/Loop to loop all rows starting from row 3? I declared a variable for the rows (intRegion), but I don't know how to make the loop run through all of the rows without typing in the input boxes by hand. My rows are sorted by state, but I would like it to loop for all of one state, save the file, and then repeat the same process for all other states.


    intState = InputBox("Please enter starting row.")
    intlast = InputBox("Please enter ending row.")
    
    strState = Sheets(strSheet).Cells(intState, 3).Value
    strH = Sheets(strSheet).Cells(intState, 6).Value
    strT = Sheets(strSheet).Cells(intState, 7).Value
    dtEff = Sheets(strSheet).Cells(intState, 8).Value
    dtEnd = Sheets(strSheet).Cells(intState, 9).Value
    strToB = Sheets(strSheet).Cells(intState, 10).Value
    strAge = Sheets(strSheet).Cells(intState, 11).Value
    
    
    For intRegion = intState To intlast
        Windows(strIDFile).Activate
        strArea = "Area " & (Sheets(strSheet).Cells(intRegion, 5).Value)
        For intP = 1 To 8
        
            Windows(strIDFile).Activate
            strPlanID = Sheets(strSheet).Cells(intRegion, (intPlan * 3) + 9).Value
            dblAdultRate = Sheets(strSheet).Cells(intRegion, (intPlan * 3) + 10).Value
            dblChildRate = Sheets(strSheet).Cells(intRegion, (intPlan * 3) + 11).Value
                    
            If intRegion = intState And intPlan = 1 Then
            
           
                Workbooks.Open Filename:=strDirectory & strTemplate
                Windows(strTemplate).Activate
                Sheets("Rate Table").Activate
                    
                Range("B6").Select
                Selection.FormulaR1C1 = strH
                Range("B7").Select
                Selection.FormulaR1C1 = strT
                Range("B8").Select
                Selection.FormulaR1C1 = dtEff
                Range("B9").Select
                Selection.FormulaR1C1 = dtEnd
                                    
                intRowKnt = 14
            End If
                
            Windows(strTemplate).Activate
            Sheets("Rate Table").Activate
            
            strRange = "A" & intRowKnt
            Range(strRange).Select
            Selection.FormulaR1C1 = strPID
            strRange = "B" & intRowKnt
            Range(strRange).Select
            Selection.FormulaR1C1 = strArea
            strRange = "C" & intRowKnt
            Range(strRange).Select
            Selection.FormulaR1C1 = strTob
            strRange = "D" & intRowKnt
            Range(strRange).Select
            Selection.FormulaR1C1 = strAge
            strRange = "E" & intRowKnt
            Range(strRange).Select
            Selection.FormulaR1C1 = dblChild
            strRange = "E" & intRowKnt + 1 & ":E" & intRowKnt + 45
            Range(strRange).Select
            Selection.FormulaR1C1 = dblAdult
            
            intRowKnt = intRowKnt + 46
            End If
        Next intPlan
    Next intRegion
    
    Windows(strTemplate).Activate
    
    Range("A14:A25000").Select
    Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete Shift:=xlUp
    
    
    strFilename = "plan" & strstate.xls"
    ActiveWorkbook.SaveAs Filename:="C:\Desktop\" & strFilename _
            , FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
            ReadOnlyRecommended:=False, CreateBackup:=False
    ActiveWorkbook.Close False
    Windows(strIDFile).Activate
    
    End Sub
    Thanks for the help!!

  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: Do/Loop Until using variables declared

    How will VBA be able to tell when the data for a state ends and the data for the next state starts?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Thanks for your reply. That's what I've been having trouble with. My idea is to loop until, for example, cell A3 <> A4. And when that happens, I'd like it to save as a new file and then continuing doing the same thing with the next state.

    As of now, I've been using an input box to tell vba when to start and stop, but that's not too efficient if I need to run the code multiple times.

  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: Do/Loop Until using variables declared

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  5. #5
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Hi,


    I'm not able to upload workbook, but hopefully this may look a little better to show the structure of my code...Sorry for the inconvenience

    I have declared all of my variables but I just have an issue of how to use them with a Do Loop Until statement

    intState = 14
    intLast = 17
    
    Do
    
    For A = intState to intLast
    'activate workbook
    
       For intPlan = 1 To 8
          'variables defined in this manner: Sheets(strSheet).Cells(intState, 3).Value
          'fill in template
    
          If A = intState And intPlan = 1 Then
            'open and activate template
            'fill in template
    
          End If
    
       Next intPlan
    
    Next A
    
    Do Until Sheets(strSheet).Cells(intState, 3).Value <> Sheets(strSheet).Cells(intState + 1, 3).Value
    'Save File
    
    Repeat loop for next set of rows
    intState to intLast are looking at rows 14 to 17, but is there a way to make it loop through all of my rows using a Do Until Loop with having to define the rows I want it to look at (so essentially getting rid of the variables, intState and intLast)? Or is there a better way for me to look at this? The bold in my code shows what I would like to perform.

    Thanks!
    Last edited by LLL0422; 06-04-2014 at 09:05 AM.

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

    Re: Do/Loop Until using variables declared

    You could use a loop to go through all the rows but without seeing how your data is structured it's hard to give specifics.

    Even knowing what column the state is in would help.

  7. #7
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Sorry, the states are listed in the 3rd column, and the rows are sorted by state. So ideally, I'd like the code to loop until the state changes, save the template, and then go through loop again until state changes, save the template, etc. I hope that helps... Initially I had an input box which was only able to loop one state at a time, but I hit an obstacle when I tried to loop through multiple states because the code couldn't recognize when the state changes.
    Last edited by LLL0422; 06-04-2014 at 09:18 AM.

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

    Re: Do/Loop Until using variables declared

    All you really need is a loop to go through all the rows.
    For Each cl In Range("C3", Range("C" & Rows.Count).End(xlUp).Row
    
        ' code to copy to template
    
        If cl.Value<>cl.Offset(1).Value Then
            ' new state is encountered, save template
        End If
    
        ' continue looping
    
    Next cl

  9. #9
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Thanks so much! So what I am filling out used to follow this code:

    dtEnd = Sheets(strSheet).Cells(intState, 9).Value
    Would it be correct to have it like this?:

    dtEnd = Sheets(strSheet).Cells(rState, 9).Value
    Because I am pulling info from each row and then moving to the next row, is rState able to specify what row I'm using?

    Also, I am getting runtime error 1004: "Method 'Range' of 'object' '_Global' failed" for the "For Each" code

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

    Re: Do/Loop Until using variables declared

    Sorry I'm not really sure what you are asking - for example, what's rState?

    In the code I posted cl is the cell that has the state, you can get it's row using cl.Row.

  11. #11
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Sorry, instead of cl, I changed it to rState.

    Is this what you mean by cl.Row? And is this okay to specify what row it should look at?

    dtEnd = Sheets(strSheet).Cells(cl.Row, 9).Value
    Sorry for confusion, I'm super new at VBA and am still trying to find my way to understand what to do.

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

    Re: Do/Loop Until using variables declared

    That would do it, though you could also use this.
    dtEnd = rState.Offset(,6).Value
    This tells VBA to put the value that is offset 6 columns to the right of rState into dtEnd.

    So if rState was referencing C10, rState.Offset(,6) would reference I10.

    By the way, when using Cells you can use either the letter or number to specify the column.

    So you could use this.
    dtEnd =Sheets(strSheet).Cells(rState.Row, "I").Value

  13. #13
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Good to know!

    Also within my "For each" loop, I am also looping what I'm calling intPlan, which loops from columns 12 to 35.

    Is this okay to specify the columns?:

    rState.Offset(, (intPlan * 3) + 9).Value
    And is there a way to fix the range error that i get from this?:
    For Each rState In Range("C3", Range("C" & Rows.Count).End(xlUp).Row)

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

    Re: Do/Loop Until using variables declared

    If that gives you the right columns then that should be fine.

    The error is my fault, the parentheses aren't closed properly, Row shouldn't be there and no sheet is referenced.

    I'm not quite sure which sheet you are working with but if it's Sheets(strSheet) then you could try something like this.
    With Sheets(strSheet)
        Set colStates = .Range("C3", .Range("C" & Rows.Count).End(xlUp)) ' set reference to state column
    End With
    
    For Each rState In colStates.Cells

  15. #15
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Ah okay, I'll remove Row from it.

  16. #16
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    The loop works great for the first row, but is there a way to make it not open my template again when its populating for the other rows?
    Also, the loop for "intPlan" also keeps recognizing that intRowKnt = 14 when its reading a new row, but it should be continuing where it left off.

                
    For Each rState In Range("C3", Range("C" & Rows.Count).End(xlUp))    
       Windows(strIDFile).Activate
    
               For intPlan = 1 To 8
                     Windows(strIDFile).Activate
                     strPlanID = rState.Offset(, (intPlan * 3) + 9).Value
                     dblAdult = rState.Offset(, (intPlan * 3) + 10).Value
                     dblChild = rState.Offset(, (intPlan * 3) + 11).Value
                     
                         Windows(strTemplate).Activate
                         Sheets("Rate Table").Activate
                             
                         Range("B6").Select
                         Selection.FormulaR1C1 = strH
                         Range("B7").Select
                         Selection.FormulaR1C1 = strT
                         Range("B8").Select
                         Selection.FormulaR1C1 = dtEff
                         Range("B9").Select
                         Selection.FormulaR1C1 = dtEnd
                         
                         intRowKnt = 14
                         
                     Windows(strTemplate).Activate
                     Sheets("Table").Activate
                     
                     strRange = "A" & intRowKnt
                     Range(strRange).Select
                     Selection.FormulaR1C1 = strPlanID
                     strRange = "B" & intRowKnt
                     Range(strRange).Select
                     Selection.FormulaR1C1 = strArea
                     strRange = "C" & intRowKnt
                     Range(strRange).Select
                     Selection.FormulaR1C1 = strTobacco
                     strRange = "D" & intRowKnt
                     Range(strRange).Select
                     Selection.FormulaR1C1 = strAge
                     strRange = "E" & intRowKnt
                     Range(strRange).Select
                     Selection.FormulaR1C1 = dblChildRate
                     strRange = "E" & intRowKnt + 1 & ":E" & intRowKnt + 45
                     Range(strRange).Select
                     Selection.FormulaR1C1 = dblAdultRate
                     
                     intRowKnt = intRowKnt + 46
              
                 Next intPlan
    Next rState
    Last edited by LLL0422; 06-04-2014 at 11:35 AM.

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

    Re: Do/Loop Until using variables declared

    There's no code that's 'opening' the template, but there is this,
    Windows(strTemplate).Activate
    which activates the template.

    There's no need to activate a workbook/worksheet to copy/paste data.

    I would have something like this before the loop, perhaps even right at the top of the code.
    Dim wbTemplate As Workbook
    
        Set wbTemplate = Workbooks(strTemplate)
    Then you can use something like this.
      
        For intPlan = 1 To 8
    
            strPlanID = rState.Offset(, (intPlan * 3) + 9).Value
            dblAdult = rState.Offset(, (intPlan * 3) + 10).Value
            dblChild = rState.Offset(, (intPlan * 3) + 11).Value
    
            With wbTemplate.Sheets("Rate Table")
                .Range("B6").Value = strH
                .Range("B7").Value = strT
                .Range("B8").Value = dtEff
                .Range("B9").Value = dtEnd
            End With
    
            intRowKnt = 14
    
            With wbTemplate.Sheets("Table")
                strRange = "A" & intRowKnt
                .Range(strRange).Value = strPlanID
                strRange = "B" & intRowKnt
                .Range(strRange).Value = strArea
                strRange = "C" & intRowKnt
                .Range(strRange).Value = strTobacco
                strRange = "D" & intRowKnt
                .Range(strRange).Value = strAge
                strRange = "E" & intRowKnt
                .Range(strRange).Value = dblChildRate
                strRange = "E" & intRowKnt + 1 & ":E" & intRowKnt + 45
                .Range(strRange).Value = dblAdultRate
            End With
            
            intRowKnt = intRowKnt + 46
    
        Next intPlan
    By the way, I removed this because, as far as I can see, it wasn't really doing anything.
    Windows(strIDFile).Activate

  18. #18
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    It seems like
    Dim wbTemplate As Workbook
    
        Set wbTemplate = Workbooks(strTemplate)
    is giving me a mismatch error for some reason..

    strTemplate = "File.xls"
    Last edited by LLL0422; 06-04-2014 at 11:55 AM.

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

    Re: Do/Loop Until using variables declared

    Definitely a type mismatch error and not a subscript out of range?

  20. #20
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Yep, definitely Type mismatch.

    Sub Testing4()
    
    Application.EnableCancelKey = xlDisabled
    
    Dim strDirectory As String, strTemplate As String, strIDFile As String, strSheet As String
    Dim strState As String, strH As String, strT As String
    Dim dtEff As Date, dtEnd As Date
    Dim strArea As String, strTob As String, strAge As String, strPlanID As String, strRange As String
    intPlan As Integer, intRowKnt As Integer
    Dim dblAdult As Double, dblChild As Double
    Dim strFilename As String
    Dim strProd As String
    
    Dim rState As Range
    
    Dim wbTemplate As Workbook
        Set wbTemplate = Workbooks(strTemplate)
    
    strDirectory = "K:\Users\Desktop\"
    strTemplate = "Template.xls"
    strIDFile = "IDFile.xlsm"
    strSheet = "All"
    
    
        For Each rState In Range("C3", Range("C" & Rows.Count).End(xlUp))
        
                strState = rState.Offset(, 0).Value
                strH = rState.Offset(, 3).Value
                strT = rState.Offset(, 4).Value
                dtEff = rState.Offset(, 5).Value
                dtEnd = rState.Offset(, 6).Value
                strTob = rState.Offset(, 7).Value
                strAge = rState.Offset(, 8).Value
                strProd = rState.Offset(, 1).Value
                strArea = rState.Offset(, 2).Value
                  
                 If rState.Value <> rState.Offset(1).Value Then
                 
                     With wbTemplate.Sheets("Rate Table")
    
                     
                     Range("A14:A25000").Select
                     Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete Shift:=xlUp
                     
                     strFilename = "Template_" & strState & "_" & strH & "_" & strProd & "_" & Format(Date, "yyyymmdd") & ".xls"
                     ActiveWorkbook.SaveAs Filename:="K:\Users\Desktop\" & strFilename _
                     , FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
                     ReadOnlyRecommended:=False, CreateBackup:=False
                     ActiveWorkbook.Close False
                     Windows(strIDFile).Activate
                     
                     End With        
                 End If
             
    
                 
                 For intPlan = 1 To 8
                     Windows(strIDFile).Activate
                     strPlanID = rState.Offset(, (intPlan * 3) + 9).Value
                     dblAdultRate = rState.Offset(, (intPlan * 3) + 10).Value
                     dblChildRate = rState.Offset(, (intPlan * 3) + 11).Value
                         
                      With wbTemplate.Sheets("Rate Table")
    
           
                         Range("B6").Select
                         Selection.FormulaR1C1 = strHIOS
                         Range("B7").Select
                         Selection.FormulaR1C1 = strTIN
                         Range("B8").Select
                         Selection.FormulaR1C1 = dtEff
                         Range("B9").Select
                         Selection.FormulaR1C1 = dtEnd
                      End With
                      
                         intRowKnt = 14
                         
                    With wbTemplate.Sheets("Rate Table")
                     
                    strRange = "A" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strPlanID
                    strRange = "B" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strArea
                    strRange = "C" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strTobacco
                    strRange = "D" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strAge
                    strRange = "E" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = dblChildRate
                    strRange = "E" & intRowKnt + 1 & ":E" & intRowKnt + 45
                    Range(strRange).Select
                    Selection.FormulaR1C1 = dblAdultRate
                    
                    intRowKnt = intRowKnt + 46
                    
                    End With
                 Next intPlan
    
        Next rState
        
    End Sub
    This is the code that I have so far. Hope this helps.
    Last edited by LLL0422; 06-04-2014 at 12:07 PM.

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

    Re: Do/Loop Until using variables declared

    Sorry I can't see why that's happening, it works fine for me.

    How have you declared strTemplate?

  22. #22
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Could there be something wrong with my code?

    Sub Testing4()
    
    Application.EnableCancelKey = xlDisabled
    
    Dim strDirectory As String, strTemplate As String, strIDFile As String, strSheet As String
    Dim strState As String, strH As String, strT As String
    Dim dtEff As Date, dtEnd As Date
    Dim strArea As String, strTob As String, strAge As String, strPlanID As String, strRange As String
    intPlan As Integer, intRowKnt As Integer
    Dim dblAdult As Double, dblChild As Double
    Dim strFilename As String
    Dim strProd As String
    
    Dim rState As Range
    
    Dim wbTemplate As Workbook
        Set wbTemplate = Workbooks(strTemplate)
    
    strDirectory = "K:\Users\Desktop\"
    strTemplate = "Template.xls"
    strIDFile = "IDFile.xlsm"
    strSheet = "All"
    
    
        For Each rState In Range("C3", Range("C" & Rows.Count).End(xlUp))
        
                strState = rState.Offset(, 0).Value
                strH = rState.Offset(, 3).Value
                strT = rState.Offset(, 4).Value
                dtEff = rState.Offset(, 5).Value
                dtEnd = rState.Offset(, 6).Value
                strTob = rState.Offset(, 7).Value
                strAge = rState.Offset(, 8).Value
                strProd = rState.Offset(, 1).Value
                strArea = rState.Offset(, 2).Value
                  
                 If rState.Value <> rState.Offset(1).Value Then
                 
                     With wbTemplate.Sheets("Rate Table")
    
                     
                     Range("A14:A25000").Select
                     Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete Shift:=xlUp
                     
                     strFilename = "Template_" & strState & "_" & strH & "_" & strProd & "_" & Format(Date, "yyyymmdd") & ".xls"
                     ActiveWorkbook.SaveAs Filename:="K:\Users\Desktop\" & strFilename _
                     , FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
                     ReadOnlyRecommended:=False, CreateBackup:=False
                     ActiveWorkbook.Close False
                     Windows(strIDFile).Activate
                     
                     End With        
                 End If
             
    
                 
                 For intPlan = 1 To 8
                     Windows(strIDFile).Activate
                     strPlanID = rState.Offset(, (intPlan * 3) + 9).Value
                     dblAdultRate = rState.Offset(, (intPlan * 3) + 10).Value
                     dblChildRate = rState.Offset(, (intPlan * 3) + 11).Value
                         
                      With wbTemplate.Sheets("Rate Table")
    
           
                         Range("B6").Select
                         Selection.FormulaR1C1 = strHIOS
                         Range("B7").Select
                         Selection.FormulaR1C1 = strTIN
                         Range("B8").Select
                         Selection.FormulaR1C1 = dtEff
                         Range("B9").Select
                         Selection.FormulaR1C1 = dtEnd
                      End With
                      
                         intRowKnt = 14
                         
                    With wbTemplate.Sheets("Rate Table")
                     
                    strRange = "A" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strPlanID
                    strRange = "B" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strArea
                    strRange = "C" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strTobacco
                    strRange = "D" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strAge
                    strRange = "E" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = dblChildRate
                    strRange = "E" & intRowKnt + 1 & ":E" & intRowKnt + 45
                    Range(strRange).Select
                    Selection.FormulaR1C1 = dblAdultRate
                    
                    intRowKnt = intRowKnt + 46
                    
                    End With
                 Next intPlan
    
        Next rState
        
    End Sub

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

    Re: Do/Loop Until using variables declared

    The problem could be that strTemplate doesn't actually have a value until a little bit further down in the code.

    By the way, this won't work - see the code in post #17.
    With wbTemplate.Sheets("Rate Table")
    
           
                         Range("B6").Select
                         Selection.FormulaR1C1 = strHIOS
                         Range("B7").Select
                         Selection.FormulaR1C1 = strTIN
                         Range("B8").Select
                         Selection.FormulaR1C1 = dtEff
                         Range("B9").Select
                         Selection.FormulaR1C1 = dtEnd
                      End With

  24. #24
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Ah, so should it look something like this?

    
    Set wbTemplate = Workbooks(strTemplate)
    
    If rState.Value <> rState.Offset(1).Value Then
                 
                     With wbTemplate.Sheets("Rate Table")
    
                     
                     Range("A14:A25000").Select
                     Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete Shift:=xlUp
                     
                     strFilename = "Template_" & strState & "_" & strH & "_" & strProd & "_" & Format(Date, "yyyymmdd") & ".xls"
                     ActiveWorkbook.SaveAs Filename:="K:\Users\Desktop\" & strFilename _
                     , FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
                     ReadOnlyRecommended:=False, CreateBackup:=False
                     ActiveWorkbook.Close False
                     Windows(strIDFile).Activate
                     
                     End With

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

    Re: Do/Loop Until using variables declared

    No, more like this.
    Dim wbTemplate As Workbook
    
        strTemplate = "Template.xls"
        
        Set wbTemplate = Workbooks(strTemplate)
    Or even this.
    Dim wbTemplate As Workbook
    
         Set wbTemplate = Workbooks("Template.xls")
    By the way, which worksheet/workbook is column C in here?
    For Each rState In Range("C3", Range("C" & Rows.Count).End(xlUp))

  26. #26
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    It is in the IDFile workbook. Template is a separate workbook.

    Ah now it says out of range if I put it after declaring what strTemplate is.

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

    Re: Do/Loop Until using variables declared

    Is the template workbook open?

    This code will refer to column C in the active sheet, whatever that is.
    For Each rState In Range("C3", Range("C" & Rows.Count).End(xlUp))
    As will all the ranges in this section of code.
     Range("B6").Select
                         Selection.FormulaR1C1 = strHIOS
                         Range("B7").Select
                         Selection.FormulaR1C1 = strTIN
                         Range("B8").Select
                         Selection.FormulaR1C1 = dtEff
                         Range("B9").Select
                         Selection.FormulaR1C1 = dtEnd

  28. #28
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    The code should open the template workbook.

    My issue was that when it loops the plans, it would keep trying to reopen the template.
    How do I condition the code so that it only opens once per state?

    The code used to be:

    For intPlan = 1 To 8
                 
                     Windows(strIDFile).Activate
                     strPlanID = rState.Offset(, (intPlan * 3) + 9).Value
                     dblAdultRate = rState.Offset(, (intPlan * 3) + 10).Value
                     dblChildRate = rState.Offset(, (intPlan * 3) + 11).Value
                         
                        Workbooks.Open Filename:=strDirectory & strTemplate
           
                        Range("B6").Select
                        Selection.FormulaR1C1 = strHIOS
                        Range("B7").Select
                        Selection.FormulaR1C1 = strTIN
                        Range("B8").Select
                        Selection.FormulaR1C1 = dtEff
                        Range("B9").Select
                        Selection.FormulaR1C1 = dtEnd
                      
                      
                         intRowKnt = 14
                         
                     
                    strRange = "A" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strPlanID
                    strRange = "B" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strArea
                    strRange = "C" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strTobacco
                    strRange = "D" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = strAge
                    strRange = "E" & intRowKnt
                    Range(strRange).Select
                    Selection.FormulaR1C1 = dblChildRate
                    strRange = "E" & intRowKnt + 1 & ":E" & intRowKnt + 45
                    Range(strRange).Select
                    Selection.FormulaR1C1 = dblAdultRate
                    
                    intRowKnt = intRowKnt + 46
                    
                 Next intPlan
    Last edited by LLL0422; 06-04-2014 at 12:38 PM.

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

    Re: Do/Loop Until using variables declared

    That wasn't in the earlier code where you mentioned the template being opened again.

    Try putting this at the top of the code.
    
    strDirectory = "K:\Users\Desktop\"
    strTemplate = "Template.xls"
    strIDFile = "IDFile.xlsm"
    strSheet = "All"
    
    Set wbTemplate = Workbooks.Open(Filename:=strDirectory & strTemplate)
    It might also be an idea to create a reference to the IDFile workbook

    For example, if the IDFile is open.
    Set wbIDFile = Workbooks(strIDFile)
    Then you can make sure you are referring to the correct workbook and worksheet here.
    For Each rState In wbIDFile.Sheets(strSheet).Range("C3", wbIDFile.Sheets(strSheet).Range("C" & Rows.Count).End(xlUp))

  30. #30
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Sorry, I made some adjustments and then posted the code that I had at the moment. I will try what you suggested.

  31. #31
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    I found a different approach where I can use a function to test if the sheet is already open and it seems to work! But I did Dim wbTemplate as Workbook also.

    One last thing..hopefully,

    If rState.Value <> rState.Offset(1).Value Then
    C3: AL
    C4: AL
    C5: FL

    This works great, except that once the code recognized that C4 <> C5, it goes straight to saving the file rather than completing the template with C4's information.

    Thank you for bearing with me!!

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

    Re: Do/Loop Until using variables declared

    Is there no way you could mock up an example data workbook and template and attach them here?

    That would make it a lot easier to give help with the code.

    As it is I'm going on the code you are posting.

    PS If the whole point of the code is to split up the data by state then there could be other(better?) methods.:l

  33. #33
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    I'm really sorry, I can't upload an example workbook at the moment.

    As for my data, all of the states have to be on the same worksheet, and I need to somehow make it create a separate template for each state all at once. Originally, I would run the code state by state, but for time's sake, that's not too efficient. Sorry for trouble!

    I think I've gotten most of my questions answered, but I will make new thread for other questions not related to this one.

    Thanks so much for your help! I really appreciate it!

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

    Re: Do/Loop Until using variables declared

    This should actually be a very straightforward task and you'll find plenty of examples if you search the forum.

+ 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. Problem with public global declared variables
    By vonb3ta in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-03-2013, 07:20 AM
  2. Macro for autofilter using variables declared in worksheet
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-06-2005, 07:05 AM
  3. [SOLVED] Macro for autofilter using variables declared in worksheet
    By Jeff in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  4. Macro for autofilter using variables declared in worksheet
    By Jeff in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  5. Macro for autofilter using variables declared in worksheet
    By Jeff in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-13-2005, 11:05 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