Sub delete()
speedUp
Dim sql As String, r As Long, c As Long
Dim PK As String: PK = "PK"
r = 3
Do While Cells(r, 3).Value <> ""
sql = "DELETE FROM " + Cells(1, 1).Value + " WHERE 1=1"
c = 3
Do While Cells(2, c).Value <> ""
If Cells(1, c).Value = PK Then
sql = sql + " AND " + Cells(2, c).Value + " = '" + Cells(r, c).Value + "'"
End If
c = c + 1
Loop
Cells(r, 1).Value = sql + ";"
r = r + 1
Loop
undoSpeedUp
End Sub
Sub insert()
speedUp
Dim sqlCol As String, sqlVal As String, r As Long, c As Long
'insert into [table] ([col1],[col2],...) values (
sqlCol = "INSERT INTO " + Cells(1, 2).Value + "." + Cells(1, 1).Value + " ("
c = 3
Do While Cells(2, c).Value <> ""
sqlCol = sqlCol + Cells(2, c).Value + ","
c = c + 1
Loop
sqlCol = Left(sqlCol, Len(sqlCol) - 1) + ") VALUES ("
'[val1],[val2],...);
r = 3
c = 3
Do While Cells(r, 3).Value <> ""
c = 3
sqlVal = ""
Do While Cells(2, c).Value <> ""
sqlVal = sqlVal + "'" + Cells(r, c).Value + "',"
c = c + 1
Loop
'output
Cells(r, 2).Value = sqlCol + Left(sqlVal, Len(sqlVal) - 1) + ");"
r = r + 1
Loop
undoSpeedUp
End Sub
Sub speedUp()
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
End Sub
Sub undoSpeedUp()
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
List of users who liked
00