I am getting an error as "Object required" while running the macro which i have assigned to a text box.
I have copied the macro from another excel which has only one sheet. This is working fine there and generating the insert script based on the data in the sheet.
This macro i am inserting it in another workbook in the 4th sheet (this workbook has 4 sheets). Below is the macro used :
Sub GetInsertSQL()
Dim wsSrc As Worksheet: Set wsSrc = ActiveSheet
Dim LastRow As Long: LastRow = wsSrc.UsedRange.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim LastCol As Long: LastCol = wsSrc.UsedRange.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Dim i As Long, j As Long
Dim strQuery As String
Dim strOutput As String
strQuery = ""
For j = 1 To LastCol
strQuery = strQuery + "[" + CStr(wsSrc.Cells(1, j)) + "], "
Next j
strQuery = Left(strQuery, Len(strQuery) - 2)
strQuery = "insert into [" + wsSrc.Name + "] (" + strQuery + ")"
strOutput = strQuery + Chr(13) + Chr(10) + "values"
For i = 2 To LastRow
strQuery = ""
'Adding this if condition to check if value in 1st column is New
If (LCase(wsSrc.Cells(i, 1).Text) = "new") Then
For j = 2 To LastCol
strQuery = strQuery + "'" + Replace(CStr(wsSrc.Cells(i, j).Text), "'", "''") + "', "
Next j
strQuery = "(" + Left(strQuery, Len(strQuery) - 2) + "), "
strOutput = strOutput + Chr(13) + Chr(10) + strQuery
End If
Next i
strOutput = Left(strOutput, Len(strOutput) - 2)
OutputForm.txtOutput.Text = strOutput
OutputForm.Show vbModal
End Sub
End Sub
Is it something with the way i am doing a copy and paste ?
Bookmarks