Results 1 to 4 of 4

Script out of Range

Threaded View

  1. #1
    Forum Contributor
    Join Date
    04-09-2005
    Location
    Multan. Pakistan
    Posts
    129

    Script out of Range

    Dear Folks

    I have the following codes to generate a stock report in worksheet "CottonRegister" based on Worksheets "CottonPurchase" and "CottonIssue". It is working well in my PC, but when I copy theses worksheets to my PC at office at the same path i.e at Drive C:\ and run the Program it stops at line " Application.WorksheetFunction.SumIf(Workbooks("CottonPurchase").Worksheets("CottonPurchase").Range("d:d"), Cells(x, 1), Workbooks("CottonPurchase").Worksheets("CottonPurchase").Range("g:g"))" saying that Run Time Error 9 and script out of range.

    Private Sub CommandButton1_Click()
    Application.ScreenUpdating = False
    
    Workbooks.Open Filename:="C:\CottonPurchase.xls"
    Workbooks.Open Filename:="C:\CottonIssue.xls"
    Workbooks.Open Filename:="C:\CottonRegister.xls"
    Application.DisplayAlerts = False
    
    Cells.Clear
    Cells.Interior.ColorIndex = 15
    Range("a1:o6").Select
    Selection.Interior.ColorIndex = xlNone
    Call title
    
    'Fill Dates
    Range("a7").Value = CDate(DTPicker1)
    Range("a7").Select
    Selection.DataSeries rowcol:=xlColumns, Type:=xlChronological, Date:=xlDay, step:=1, Stop:=DTPicker2, Trend:=False
    
    Dim x As Long
    Dim y As Long
    
    y = DTPicker2 - DTPicker1
    
    Range("b7").Value = 1180 'Opening Bales
    Range("c7").Value = 188800
    
    For x = 1 To y + 7
    
    If Cells(x, 1).Value <= DTPicker2 And Cells(x, 1).Value >= DTPicker1 Then
    Range(Cells(x, 1), Cells(x, 15)).Select
    Selection.Interior.ColorIndex = xlNone
    
    '---------- Purchases------------
    
    Cells(x, 4).Value = Application.WorksheetFunction.SumIf(Workbooks("CottonPurchase").Worksheets("CottonPurchase").Range("d:d"), Cells(x, 1), Workbooks("CottonPurchase").Worksheets("CottonPurchase").Range("g:g")) 
    Cells(x, 5).Value = Application.WorksheetFunction.SumIf(Workbooks("CottonPurchase").Worksheets("CottonPurchase").Range("d:d"), Cells(x, 1), Workbooks("CottonPurchase").Worksheets("CottonPurchase").Range("h:h")) 
    
    '------------- Total--------------
    
    Cells(x, 6).Value = Cells(x, 2).Value + Cells(x, 4).Value
    Cells(x, 7).Value = Cells(x, 3).Value + Cells(x, 5).Value
    
    '-------------Consumption-----------
    
    Cells(x, 8).Value = Application.WorksheetFunction.SumIf(Workbooks("CottonIssue").Worksheets("cissue").Range("a:a"), Cells(x, 1), Workbooks("CottonIssue").Worksheets("cissue").Range("b:b"))
    Cells(x, 9).Value = (Cells(x, 7) / Cells(x, 6)) * Cells(x, 8)
    
    '----------Fire Loss----------
    
    Cells(x, 10).Value = Application.WorksheetFunction.SumIf(Workbooks("CottonIssue").Worksheets("cissue").Range("a:a"), Cells(x, 1), Workbooks("CottonIssue").Worksheets("cissue").Range("d:d"))
    Cells(x, 11).Value = Application.WorksheetFunction.SumIf(Workbooks("CottonIssue").Worksheets("cissue").Range("a:a"), Cells(x, 1), Workbooks("CottonIssue").Worksheets("cissue").Range("e:e"))
    
    '------------Cotton Sale----------------
    
    Cells(x, 12).Value = Application.WorksheetFunction.SumIf(Workbooks("CottonIssue").Worksheets("cissue").Range("a:a"), Cells(x, 1), Workbooks("CottonIssue").Worksheets("cissue").Range("f:f"))
    Cells(x, 13).Value = Application.WorksheetFunction.SumIf(Workbooks("CottonIssue").Worksheets("cissue").Range("a:a"), Cells(x, 1), Workbooks("CottonIssue").Worksheets("cissue").Range("g:g"))
    
    '---------------Closing---------------
    
    Cells(x, 14).Value = Cells(x, 6).Value - Cells(x, 8).Value - Cells(x, 10).Value - Cells(x, 12).Value
    Cells(x, 15).Value = Cells(x, 7).Value - Cells(x, 9).Value - Cells(x, 11).Value - Cells(x, 13).Value
    
    
    Cells(x + 1, 2).Value = Cells(x, 14).Value
    Cells(x + 1, 3).Value = Cells(x, 15).Value
    End If
    
    Next x
    
    Cells(x, 2).Value = ""
    Cells(x, 3).Value = ""
    Cells(x, 4).Value = Application.WorksheetFunction.Sum(Range("d:d"))
    Cells(x, 5).Value = Application.WorksheetFunction.Sum(Range("e:e"))
    Cells(x, 8).Value = Application.WorksheetFunction.Sum(Range("h:h"))
    Cells(x, 9).Value = Application.WorksheetFunction.Sum(Range("i:i"))
    Cells(x, 10).Value = Application.WorksheetFunction.Sum(Range("j:j"))
    Cells(x, 11).Value = Application.WorksheetFunction.Sum(Range("k:k"))
    Cells(x, 12).Value = Application.WorksheetFunction.Sum(Range("l:l"))
    Cells(x, 13).Value = Application.WorksheetFunction.Sum(Range("m:m"))
    
    Range(Cells(x, 1), Cells(x, 15)).Select
    Selection.font.Bold = True
    Selection.Interior.ColorIndex = 36
    
    
    Workbooks("CottonIssue").Close
    Workbooks("CottonPurchase").Close
    
    Unload Me
    End Sub
    
    Sub title()
    Range("a1").Value = "COMPANY NAME"
    Range("a2").Value = "(Spinning Unit)"
    Range("a3").Value = "COTTON STOCK REGISTER"
    Range("a4").Value = "From " & DTPicker1 & " To " & DTPicker2
    Range("a5").Value = "DATE"
    Range("b5").Value = "OPENING"
    Range("d5").Value = "PURCHASES"
    Range("F5").Value = "TOTAL"
    Range("H5").Value = "CONSUMPTION"
    Range("J5").Value = "FIRE LOSS"
    Range("l5").Value = "SALE"
    Range("n5").Value = "CLOSING"
    Range("b6").Value = "BALES"
    Range("c6").Value = "KGS"
    Range("D6").Value = "BALES"
    Range("E6").Value = "KGS"
    Range("F6").Value = "BALES"
    Range("G6").Value = "KGS"
    Range("H6").Value = "BALES"
    Range("I6").Value = "KGS"
    Range("J6").Value = "BALES"
    Range("K6").Value = "KGS"
    Range("L6").Value = "BALES"
    Range("M6").Value = "KGS"
    Range("N6").Value = "BALES"
    Range("O6").Value = "KGS"
    End Sub
    Last edited by VBA Noob; 09-23-2007 at 06:28 AM.

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