I have a macro that works on Worksheet Step 2, but gives me a runtime error 1004 Application-defined or Object-defined error. Here's the code:
All I did was change Worksheet("Step 1") to Worksheet("Step 2"), (this is where I get the data from) and Worksheet("Step 2") to Worksheet("Step 3"), (this is where I put the data). All the Worksheets exist and are named correctly.
Any clues?
Code:
Option Explicit
Public Sub GetDataStep3()
' Goes to MAPICS, retrieves data and places it on Worksheet "Step 3"
Dim sqlstring As String, connstring As String, strPartNums As String
Dim PartNum As Range, LastNum As Range
Dim NextRow As Long, LastRow As Long
Dim qt As Name
' Delete any previous queries and their named ranges
With Worksheets("Step 3")
.Activate
.Range("A:F").Delete
For Each qt In .Names
qt.Delete
Next qt
End With
' Assemble the Part #'s to send to MAPICS query
LastRow = Worksheets("Step 2").Range("B65536").End(xlUp).Row
For Each PartNum In Worksheets("Step 2").Range("B2:B" & LastRow)
strPartNums = strPartNums & Chr(39) & PartNum.Text & Chr(39) & ","
Next PartNum
strPartNums = Left(strPartNums, Len(strPartNums) - 1)
' MAPICS query connection parameters
sqlstring = "SELECT PSTRUC.PINBR, PSTRUC.CINBR, ITEMASA.ITDSC, PSTRUC.QTYPR, ITEMASA.ITTYP FROM S10B0361.AMFLIB6.ITEMASA ITEMASA, S10B0361.AMFLIB6.PSTRUC PSTRUC WHERE ITEMASA.ITNBR = PSTRUC.CINBR AND ((PSTRUC.PINBR IN ( " & strPartNums & ")) AND (ITEMASA.ITDSC NOT LIKE '%TOOL%') AND (ITEMASA.ITDSC NOT LIKE 'MO%')) ORDER BY PSTRUC.PINBR"
connstring = "ODBC;DSN=ddt;Database=amflib6"
' Add the Query results to Worksheet(Step 3)
With Worksheets("Step 3").QueryTables.Add(Connection:=connstring, _
Destination:=Worksheets("Step 3").Range("A1"), Sql:=sqlstring)
.Refresh BackgroundQuery:=False
End With
End Sub
Any clues?