• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Code

balaji3081

Member
Guys need help, not sure if this is enough or you need more details -


Code:
Public oApp As Object
Public strDB As String
Public Wbk_Base As Workbook
 
Public strPDF As String
 
Public Sub Initialize()
    Application.DisplayAlerts = False
    strDB = Worksheets("Input").Range("B2")
    'strPDF = Worksheets("Input").Range("B11")
   
End Sub
 
Sub ExportAsPDF()
 
  Dim fileName As String
 
  fileName = strPDF
 
  'ThisWorkbook.Sheets(Array().Select
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
  fileName:=fileName, Quality:=xlQualityStandard, _
  IncludeDocProperties:=True, IgnorePrintAreas:=False, _
  OpenAfterPublish:=True
 
End Sub
 
Public Sub OpenDB(strDB)
    Set oApp = CreateObject("Access.Application")
   
    oApp.Visible = False
   
    '// Open database
    oApp.OpenCurrentDatabase strDB
 
End Sub
 
Public Sub GetData(ParamArray qryParams())
    Dim oQry As Object
    Dim rs As Object
    Dim i As Integer
   
   
    Set oQry = oApp.CurrentDB.Querydefs(qryParams(0))
    j = 1
    For i = 0 To oQry.Parameters.Count - 1
      oQry.Parameters(i).Value = qryParams(j)
        j = j + 1
    Next
   
    Set rs = oQry.Openrecordset
    s = ActiveSheet.Range("B4")
    Set Wbk_Base = Workbooks.Open(ThisWorkbook.Path & "\Base File.xlsx")
    Wbk_Base.Sheets(s).Select
    'Wbk_Base.Sheets(s).Range("A2:Y119").ClearContents
    Range("A2", Range("Y2").End(xlDown)).Clear
   
    Wbk_Base.Sheets(s).Range("A2").CopyFromRecordset rs
    rngrow = 1
    rngcolumn = 1
   
    For i = 1 To rs.Fields.Count
        Wbk_Base.ActiveSheet.Cells(rngrow, rngcolumn).Value = rs.Fields(i - 1).Name
        rngcolumn = rngcolumn + 1
    Next i
       
    oQry.Close
    rs.Close
   
    Set oQry = Nothing
    Set rs = Nothing
End Sub
Public Sub Main()
 
    Dim nRows As Integer
    Dim WbBase As Workbook
    Dim sOutPutPath As String
    Dim sOutPutFile As String
    Dim ppRecepient, ppCCRecepient, ppAttachment As String
    Dim pRecepient, pCCRecepient, pSubject, pMessage, pAttachment, pSendMsg As String
    Dim colAttachments As Collection
   
    Dim vProject, vCurrency, vProjectDept, vProjGOCOff As String
   
    Call Initialize
    Call OpenDB(strDB)
    '*****
   
    nRows = Worksheets("Input").Range("A9").CurrentRegion.Rows.Count + 7
   
    With ThisWorkbook.Worksheets("Input")
        sOutPutPath = .Range("B6") & "\"
        'pMessage = .Range("G2")
        'pSendMsg = .Range("G4")
    End With
   
   
    For i = 9 To nRows
       
        If Sheet1.Range("A" & i) <> "" Then
            Call GetData(Sheet1.Range("B3"), Sheet1.Range("A" & i), Sheet1.Range("B" & i), Sheet1.Range("C" & i), Sheet1.Range("D" & i), Sheet1.Range("E" & i))
        End If
       
        Wbk_Base.Worksheets("Current Month Summary").Select
       
        With ThisWorkbook.Worksheets("Input")
           
            sOutPutFile = sOutPutPath & .Range("F" & i)
           
        End With
       
        vL6 = Sheet1.Range("A" & i)
        vStart_Period = Sheet1.Range("B" & i)
        vEnd_period = Sheet1.Range("C" & i)
       
        On Error Resume Next
        With Wbk_Base.Sheets("Current Month Summary")
            .PivotTables("PivotTable1").PivotCache.Refresh
            .Range("B6") = IIf(vMASTER_NAME <> "", vMASTER_NAME, "(All)")
            .Range("B7") = IIf(vOFFICE <> "", vOFFICE, "(All)")
            .Range("B8") = IIf(vLOCATION <> "", vLOCATION, "(All)")
            .Range("B9") = IIf(vMTH <> "", vMTH, "(All)")
            .PivotTables("PivotTable1").PivotCache.Refresh
       
        With Wbk_Base.Sheets("YTD Summary")
            .PivotTables("PivotTable1").PivotCache.Refresh
            .Range("B6") = IIf(vMASTER_NAME <> "", vMASTER_NAME, "(All)")
            .Range("B7") = IIf(vOFFICE <> "", vOFFICE, "(All)")
            .Range("B8") = IIf(vLOCATION <> "", vLOCATION, "(All)")
            .Range("B9") = IIf(vYTD <> "", vYTD, "(All)")
            .PivotTables("PivotTable2").PivotCache.Refresh
           
        With Wbk_Base.Sheets("12 Month Rolling Summary")
            .PivotTables("PivotTable1").PivotCache.Refresh
            .Range("B6") = IIf(vMASTER_NAME <> "", vMASTER_NAME, "(All)")
            .Range("B7") = IIf(vOFFICE <> "", vOFFICE, "(All)")
            .Range("B8") = IIf(vLOCATION <> "", vLOCATION, "(All)")
            .PivotTables("PivotTable3").PivotCache.Refresh
        End With
         
       
        If Err.Number = 0 Then
         
          Wbk_Base.SaveAs sOutPutFile
          Wbk_Base.Close
          'Call SendMail(pSubject, pRecepient, pCCRecepient, pMessage, pAttachment, pSendMsg)
        Else
            Err.Number = 0
            Wbk_Base.Close
        End If
       
     
    Next i
   
    oApp.Application.Quit
    Set oApp = Nothing
    Application.DisplayAlerts = True
    MsgBox "Reports Generated successfully!!!", vbInformation
   
End Sub
 
 
Sub browse_Click()
 
    Dim dlgPickFiles As Office.FileDialog
    Dim strList As String
    Set dlgPickFiles = Application.FileDialog(msoFileDialogFolderPicker)
    With dlgPickFiles
        .AllowMultiSelect = False
        With .Filters
            .Clear
        End With
        .Show
        'Me.Range("C5") = .SelectedItems(1)
        On Error Resume Next
        ActiveCell.Value = .SelectedItems(1)
               
        'Range("C5") = .SelectedItems(1)
    End With
 
    Set dlgPickFiles = Nothing
 
End Sub
 
Hello balaji,

It appears your 'With' blocks are missing End Withs.
With Wbk_Base.Sheets("Current Month Summary")
missing End With afterwards.

With Wbk_Base.Sheets("YTD Summary")
missing End With afterwards.

For some reason the VBA compiler lists this error as part of the for-next loop, which your With statements are nested in. Whenever you leave one of these nested levels open, it says the problem is in the previous nest. I'm not sure if it's a problem with the way the compiler handles for-next loops or just a counterintuitive error message.
 
In the Main macro, you were missing a couple of "End With" statements.
Code:
        With Wbk_Base.Sheets("Current Month Summary")
            .PivotTables("PivotTable1").PivotCache.Refresh
            .Range("B6") = IIf(vMASTER_NAME <> "", vMASTER_NAME, "(All)")
            .Range("B7") = IIf(vOFFICE <> "", vOFFICE, "(All)")
            .Range("B8") = IIf(vLOCATION <> "", vLOCATION, "(All)")
            .Range("B9") = IIf(vMTH <> "", vMTH, "(All)")
            .PivotTables("PivotTable1").PivotCache.Refresh
        End With  '<-- MISSING THIS ONE
       
        With Wbk_Base.Sheets("YTD Summary")
            .PivotTables("PivotTable1").PivotCache.Refresh
            .Range("B6") = IIf(vMASTER_NAME <> "", vMASTER_NAME, "(All)")
            .Range("B7") = IIf(vOFFICE <> "", vOFFICE, "(All)")
            .Range("B8") = IIf(vLOCATION <> "", vLOCATION, "(All)")
            .Range("B9") = IIf(vYTD <> "", vYTD, "(All)")
            .PivotTables("PivotTable2").PivotCache.Refresh
        End With   '<--- AND THIS ONE
           
        With Wbk_Base.Sheets("12 Month Rolling Summary")
            .PivotTables("PivotTable1").PivotCache.Refresh
            .Range("B6") = IIf(vMASTER_NAME <> "", vMASTER_NAME, "(All)")
            .Range("B7") = IIf(vOFFICE <> "", vOFFICE, "(All)")
            .Range("B8") = IIf(vLOCATION <> "", vLOCATION, "(All)")
            .PivotTables("PivotTable3").PivotCache.Refresh
        End With
 
Basic changes done..

_____________________________________________
Code:
Public oApp As Object
Public strDB As String
Public Wbk_Base As Workbook
 
Public strPDF As String
 
Public Sub Initialize()
Application.DisplayAlerts = False
strDB = Worksheets("Input").Range("B2")
'strPDF = Worksheets("Input").Range("B11")
 
End Sub
 
Sub ExportAsPDF()
 
Dim fileName As String
 
fileName = strPDF
 
'ThisWorkbook.Sheets(Array().Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
fileName:=fileName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
 
End Sub
 
Public Sub OpenDB(strDB)
Set oApp = CreateObject("Access.Application")
 
oApp.Visible = False
 
'// Open database
oApp.OpenCurrentDatabase strDB
 
End Sub
 
Public Sub GetData(ParamArray qryParams())
Dim oQry As Object
Dim rs As Object
Dim i As Integer
 
 
Set oQry = oApp.CurrentDB.Querydefs(qryParams(0))
j = 1
For i = 0 To oQry.Parameters.Count - 1
oQry.Parameters(i).Value = qryParams(j)
j = j + 1
[COLOR=#0000ff] Next i[/COLOR]
 
Set rs = oQry.Openrecordset
s = ActiveSheet.Range("B4")
Set Wbk_Base = Workbooks.Open(ThisWorkbook.Path & "\Base File.xlsx")
Wbk_Base.Sheets(s).Select
'Wbk_Base.Sheets(s).Range("A2:Y119").ClearContents
Range("A2", Range("Y2").End(xlDown)).Clear
 
Wbk_Base.Sheets(s).Range("A2").CopyFromRecordset rs
rngrow = 1
rngcolumn = 1
 
For i = 1 To rs.Fields.Count
Wbk_Base.ActiveSheet.Cells(rngrow, rngcolumn).Value = rs.Fields(i - 1).Name
rngcolumn = rngcolumn + 1
Next i
 
oQry.Close
rs.Close
 
Set oQry = Nothing
Set rs = Nothing
End Sub
Public Sub Main()
 
Dim nRows As Integer
Dim WbBase As Workbook
Dim sOutPutPath As String
Dim sOutPutFile As String
Dim ppRecepient, ppCCRecepient, ppAttachment As String
Dim pRecepient, pCCRecepient, pSubject, pMessage, pAttachment, pSendMsg As String
Dim colAttachments As Collection
 
Dim vProject, vCurrency, vProjectDept, vProjGOCOff As String
 
Call Initialize
Call OpenDB(strDB)
'*****
 
nRows = Worksheets("Input").Range("A9").CurrentRegion.Rows.Count + 7
 
With ThisWorkbook.Worksheets("Input")
sOutPutPath = .Range("B6") & "\"
'pMessage = .Range("G2")
'pSendMsg = .Range("G4")
End With
 
 
For i = 9 To nRows
 
If Sheet1.Range("A" & i) <> "" Then
Call GetData(Sheet1.Range("B3"), Sheet1.Range("A" & i), Sheet1.Range("B" & i), Sheet1.Range("C" & i), Sheet1.Range("D" & i), Sheet1.Range("E" & i))
End If
 
Wbk_Base.Worksheets("Current Month Summary").Select
 
With ThisWorkbook.Worksheets("Input")
 
sOutPutFile = sOutPutPath & .Range("F" & i)
 
End With
 
vL6 = Sheet1.Range("A" & i)
vStart_Period = Sheet1.Range("B" & i)
vEnd_period = Sheet1.Range("C" & i)
 
On Error Resume Next
With Wbk_Base.Sheets("Current Month Summary")
.PivotTables("PivotTable1").PivotCache.Refresh
.Range("B6") = IIf(vMASTER_NAME <> "", vMASTER_NAME, "(All)")
.Range("B7") = IIf(vOFFICE <> "", vOFFICE, "(All)")
.Range("B8") = IIf(vLOCATION <> "", vLOCATION, "(All)")
.Range("B9") = IIf(vMTH <> "", vMTH, "(All)")
.PivotTables("PivotTable1").PivotCache.Refresh
 End With ' <- Add this line
With Wbk_Base.Sheets("YTD Summary")
.PivotTables("PivotTable1").PivotCache.Refresh
.Range("B6") = IIf(vMASTER_NAME <> "", vMASTER_NAME, "(All)")
.Range("B7") = IIf(vOFFICE <> "", vOFFICE, "(All)")
.Range("B8") = IIf(vLOCATION <> "", vLOCATION, "(All)")
.Range("B9") = IIf(vYTD <> "", vYTD, "(All)")
.PivotTables("PivotTable2").PivotCache.Refresh
End With  ' <- Add this line
With Wbk_Base.Sheets("12 Month Rolling Summary")
.PivotTables("PivotTable1").PivotCache.Refresh
.Range("B6") = IIf(vMASTER_NAME <> "", vMASTER_NAME, "(All)")
.Range("B7") = IIf(vOFFICE <> "", vOFFICE, "(All)")
.Range("B8") = IIf(vLOCATION <> "", vLOCATION, "(All)")
.PivotTables("PivotTable3").PivotCache.Refresh
End With
 
 
If Err.Number = 0 Then
 
Wbk_Base.SaveAs sOutPutFile
Wbk_Base.Close
'Call SendMail(pSubject, pRecepient, pCCRecepient, pMessage, pAttachment, pSendMsg)
Else
Err.Number = 0
Wbk_Base.Close
End If
 
 
Next i
 
oApp.Application.Quit
Set oApp = Nothing
Application.DisplayAlerts = True
MsgBox "Reports Generated successfully!!!", vbInformation
 
End Sub
 
 
Sub browse_Click()
 
Dim dlgPickFiles As Office.FileDialog
Dim strList As String
Set dlgPickFiles = Application.FileDialog(msoFileDialogFolderPicker)
With dlgPickFiles
.AllowMultiSelect = False
With .Filters
.Clear
End With
.Show
'Me.Range("C5") = .SelectedItems(1)
On Error Resume Next
ActiveCell.Value = .SelectedItems(1)
 
'Range("C5") = .SelectedItems(1)
End With
 
Set dlgPickFiles = Nothing
 
End Sub
 
@Debraj(ex-Roy)
Hi!
It was a joke, wasn't it?:oops:
What about posting code within the code pane? Ingenious, isn't it?
Regards!
 
@ SirJ(ames)B(ond)007,

The word "quick help needed" force me to do this Ingenious fault.. :mad:
and highlighting any line with color, not available with code tag.. :confused:

will take care next time..
 
Hey Guys, this was no test, you pple are just amazing, you know how it is when you are about to present a report and the file doesn't run.....well had that experience yesterday.....thanks to you guys......or else the joke would have been on me.....

However I have more come on this will start a new thread.....Thanks again
 
Back
Top