我们有一个代码来清理然后将Excel数据复制到选项卡中,并在其中累积。 A列:H。
我们需要这些数据在下一栏(I)中有月份。 对于所有新行,意味着范围(Ix:Ix),其中长度是从第I列的最后一行到新粘贴数据的最后一行。
守则如下:
Sub Trial_Fix()
' Trial_Fix Macro
Dim LastRow As Long
' Deleting columns and adding columns of the raw data.
Sheets("FP Data dump").Select
Range("A:I,K:R,V:W").Select
Selection.Delete Shift:=xlToLeft
Columns("C:D").Select
Selection.Insert Shift:=xlToRight
Range("A1").Select
Sheets("CF Data Dump").Select
Range("A:C,E:E,H:H,J:J,M:O,Q:S,G:G").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select
Sheets("Fuel Data Dump").Select
Range("A:C,E:G,I:J,N:O,Q:Q,S:AC").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select
' Pasting CF Data
' Pasting FP + Data
' Pasting Fuel Data
Sheets("Fuel Data Dump").Select
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("A1:H" & LastRow).Select
Selection.Copy
Sheets("CLEAN FUEL DATA").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
答案 0 :(得分:0)
如果您继续使用.Select,.Activate和Application.Selection属性,则可以直接在Range.PasteSpecial method命令下使用以下内容。
rst = stmt.executeQuery("SELECT *
FROM invoicename
ORDER BY name DESC
LIMIT 1;");
if (rst.next()) {
String str = rst.getString("name");
rst1 = stmt.executeQuery("SELECT sum(price)
FROM invoices
WHERE invoicename='" + str + "';");
if (rst1.next()) {
int s = rst1.getInt(1);
if (rst1.wasNull()) {
s = 0;
}
stmt.executeUpdate("INSERT INTO invoice
VALUES('" + str + "',curdate(),curtime(),'" + s + "')");
}
但是,使用上述方法和属性充其量是不可靠的,并且可能均匀地模糊您的数据。请考虑以下重写,它使用显式引用来处理Range和Worksheet个对象。
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
With Selection
With .Resize(.Rows.Count, 1).Offset(0, .Columns.Count)
.Value = Date
.NumberFormat = "mmmm"
End With
End With
以上内容使用With ... End With statement来引用.Parent工作表。 With ... End With中的任何内容都通过添加句点(又名句号或Sub Trial_Fix()
' Trial_Fix Macro
Dim lrs As Long, lrd As Long
Dim wsCFD As Worksheet
With Worksheets("FP Data dump")
.Range("A:I,K:R,V:W").Delete Shift:=xlToLeft
.Columns("C:D").Insert Shift:=xlToRight
' Pasting FP + Data
'?????????????
End With
With Worksheets("CF Data Dump")
.Range("A:C,E:G,I:J,N:O,Q:Q,S:AC").Delete Shift:=xlToLeft
' Pasting CF Data
'?????????????
End With
Set wsCFD = Worksheets("CLEAN FUEL DATA")
With Worksheets("Fuel Data Dump")
'get rid of unwanted columns
.Range("A:C,E:E,G:H,J:J,M:O,Q:S").Delete Shift:=xlToLeft
'last populated row in source
lrs = .Cells(Rows.Count, 1).End(xlUp).Row
'first blank cell in destination
lrd = wsCFD.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'direct value transfer is faster than copy/paste values and does not involve the clip board
With .Range("A1:H" & lrs)
wsCFD.Cells(lrd, 1).Resize(.Rows.Count, .Columns.Count) = .Value
wsCFD.Cells(lrd, 1).Resize(.Rows.Count, 1).Offset(0, .Columns.Count) = Date
wsCFD.Cells(lrd, 1).Resize(.Rows.Count, 1).Offset(0, .Columns.Count).NumberFormat = "mmmm"
End With
End With
End Sub
)作为每个Range object的前缀来获取其继承的父引用。