产生sql语句的vba
//清除空列
Sub ClearNullMacro1() Dim j As Integer Dim colCount As Integer colCount = Cells(2, Columns.Count).End(xlToLeft).Column For j = 1 To colCount Dim v As String v = Cells(2, j).value ' MsgBox v If v = "" Then Columns(j).Delete Dim PauseTime PauseTime = Timer + 1.5 colCount = Cells(2, Columns.Count).End(xlToLeft).Column End If Next MsgBox "successed" & Cells(2, Columns.Count).End(xlToLeft).Column End Sub //创建sql语句 Sub GenerateSqlMacro1() For Each Sh In Worksheets Dim j As Integer Dim colCount As Integer Dim keys As String Dim values As String colCount = Sh.Cells(2, Columns.Count).End(xlToLeft).Column For j = 1 To colCount keys = keys & Sh.Cells(1, j).value & ", " & Chr(10) values = values & "'" & Sh.Cells(2, j).value & "', " & Chr(10) Next Sh.Cells(5, 1).value = "insert into " & Sh.Name & "(" & Chr(10) & keys & ")" Sh.Cells(7, 1).value = "values (" & Chr(10) & values & ")" keys = "" values = "" Next MsgBox "successed" End Sub
//替换星 Sub HandleXing() For Each Sh In Worksheets Dim j As Integer Dim colCount As Integer Dim keys As String Dim values As String Dim parms As String colCount = Sh.Cells(2, Columns.Count).End(xlToLeft).Column For j = 1 To colCount keys = keys & Sh.Cells(1, j).value & ", " & Chr(10) If InStr(Sh.Cells(2, j).value, "*") <= 0 Then values = values & "'" & Sh.Cells(2, j).value & "', " & Chr(10) Else Dim value As String value = Replace(Sh.Cells(2, j).value, "*", "@") values = values & value & ", " & Chr(10) parms = parms & "declare " & value & " nvarchar(200) " & Chr(10) End If Next Sh.Cells(11, 1).value = "insert into " & Sh.Name & "(" & Chr(10) & keys & ")" Sh.Cells(12, 1).value = "values (" & Chr(10) & values & ")" Sh.Cells(13, 1).value = parms keys = "" values = "" parms = "" Next MsgBox "successed" End Sub