I am trying to create a workbook that allows the user to import some sort of master data, import some data you want to compare to the master data, and ultimately do analysis on these data. Both master and imported data could be a workbook (i.e. multiple sheets).
My idea is to group the worksheets into a master sheet array and an imported sheet array, so that I can refer to them in the analysis code. Currently I have managed to create these two arrays in two separate modules (2 click buttons), but then I have no way of using the arrays in a third module.
Is there a way of doing it? or is my thinking wrong to have used this approach in the first place?
the following would be my codes.
Sub ImportMaster_Click() Dim sImportFile As String, sFile As String, sSheetName As String Dim sThisBk As Workbook Dim wbBk As Workbook Dim wsSht As Worksheet Dim vfilename As Variant Dim Mshtarray() Dim MshtName As String Dim lSheetNumber As Long Dim lshtcount As Long Dim iMshtcount As Integer Dim x As Integer Dim y As Integer 'import data that you want to compare with master data Application.ScreenUpdating = False Application.DisplayAlerts = False Set sThisBk = ActiveWorkbook sImportFile = Application.GetOpenFilename(Title:="Open File") If sImportFile = "False" Then MsgBox "No File Selected" Exit Sub Else vfilename = Split(sImportFile, "\") sFile = vfilename(UBound(vfilename)) Application.Workbooks.Open Filename:=sImportFile Set wbBk = Workbooks(sFile) With wbBk lSheetNumber = wbBk.Worksheets.Count If lSheetNumber > 1 Then x = 0 For iMshtcount = 1 To lSheetNumber x = x + 1 ReDim Preserve Mshtarray(0 To iMshtcount) Mshtarray(x) = wbBk.Sheets(x).Name Next If IsArray(Mshtarray) = True Then For y = 1 To x If Mshtarray(y) <> "Import page" Then lshtcount = sThisBk.Worksheets.Count wbBk.Sheets(Mshtarray(y)).Copy after:=sThisBk.Sheets(lshtcount) End If Next Else MsgBox "Array error" End If sThisBk.Sheets("Import page").Select ElseIf lSheetNumber = 1 Then MshtName = ActiveSheet.Name If SheetExists(MshtName) Then Set wsSht = .Sheets(MshtName) wsSht.Copy after:=sThisBk.Sheets("Import page") Else MsgBox "There is no Sheet with name : in:" & vbCr & .Name End If sThisBk.Sheets("Import page").Select Else MsgBox "Error, no worksheet opened" End If wbBk.Close SaveChanges:=False End With End If Application.ScreenUpdating = True Application.DisplayAlerts = True End Sub Public Function SheetExists(ByVal sWSName As String, Optional wb As Workbook) As Boolean Dim sht As Worksheet If wb Is Nothing Then Set wb = ActiveWorkbook On Error Resume Next Set sht = wb.Sheets(sWSName) On Error GoTo 0 SheetExists = Not sht Is Nothing End Function The module for importing data is similar to the one above. the problem is the third module that I want to compare the data at.
Sub Reporting_Click() Dim wbBk As Workbook Dim wsSht As Worksheet Dim firstRow As Long Dim lastRow As Long Dim lastvisRow As Long Dim readN3 As Integer Dim maxN3 As Integer Dim shtcount As Integer Dim fltrng As Range Dim a As Long Set wbBk = ActiveWorkbook Set wsSht = ThisWorkbook.Sheets 'vMshtname = ThisWorkbook.Sheets(Module1.Mshtarray(y)) <----- I have error for calling such array here 'vImshtname = ThisWorkbook.Sheets(Module3.Imshtarray(j)) Application.DisplayAlerts = False For Each wsSht In wkbk.Worksheets shtcount = ThisWorkbook.Worksheets.Count Set wsSht = ThisWorkbook.Worksheets <---- problem for setting worksheets as well since I can see wsSht is still nothing after running this line with wsSht a = 2 For a = 2 To a = shtcount <---- ****** If ThisWorkbook.Sheets(a).Name <> "Import page" Then wsSht.AutoFilterMode = False lastRow = wsSht.UsedRange.Rows.Count wsSht.Range("D6").AutoFilter Field:=4, Criteria1:=">=0", Operator:=xlAnd, Criteria2:="<>=" Set fltrng = wsSht.AutoFilter.Range.SpecialCells(xlCellTypeVisible) firstRow = fltrng.Range("E1").End(xlUp).Row lastvisRow = fltrng.Range("E1").End(xlDown).Row readN3 = Application.WorksheetFunction.Max(Range("E" & firstRow, "E" & lastvisRow)) maxN3 = 0 If maxN3 < readN3 Then maxN3 = readN3 End If Else MsgBox "No data available for analysis" End If Next End With Even when I just want to try the codes within the for loop, VBA can only run to the line with ****** and then directly skipped to end sub. Can somebody help me out here please?
wb1.sheets("sheet 1").range("A1").value