I have some VB in Excel 2007 which has stopped working since it moved from Excel 2003.
I think the problem is that the QueryTable has not been imported by the time the rest of the code has run but I don't know how to pause!
The variable myProd comes back with 0 and then the For loop does very little!
If I put a break point in the code and then step through a couple of lines it all works fine.
Any pointers would be great:
Expand|Select|Wrap|Line Numbers
- Private Sub CommandButton1_Click()
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Workbooks.Add 'Create new file for all data
- newname = ActiveWorkbook.Name
- Sheets("Sheet1").Select
- With ActiveSheet.QueryTables.Add(Connection:= _
- "ODBC;DRIVER=SQL Server;SERVER=LASSQL01;UID=;APP=Microsoft Office 2003;WSID=PURCHASING5;DATABASE=123_UK;Trusted_Connection=Yes" _
- , Destination:=Range("A1"))
- .CommandText = "exec Lascar.Stock_Profile_Report_Richard"
- .Name = "Query from LascarLive"
- .FieldNames = False
- .RowNumbers = False
- .FillAdjacentFormulas = False
- .PreserveFormatting = True
- .RefreshOnFileOpen = False
- .BackgroundQuery = True
- .RefreshStyle = xlInsertDeleteCells
- .SavePassword = False
- .SaveData = True
- .AdjustColumnWidth = True
- .RefreshPeriod = 0
- .PreserveColumnInfo = True
- .Refresh BackgroundQuery = False
- ' .Refresh BackgroundQuery:=False
- End With
- myProd = (Application.CountA(Range("A:A")) - 1) / 8 'Number of distinct products to check
- For i = 0 To myProd
- Various Formatting Lines
- Next i
- End Sub