我有代码将表2中的数据发布到表1中可用的第一个空白单元格中(在A,B和C列下)。这段代码效果很好。 但是,在我放置这些单元格的地方,我需要在列D中输入值1000,在A,B和C结束时结束。另外,对于格式化问题,我需要它作为PasteSpecial xlPasteValue进入。 我在下面粘贴了我的代码。我感谢任何帮助。
Sub NextTryThis()
'====Code here works===
LastRow2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
BlankRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row + 1
BlankRowB = Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row + 1
BlankRowC = Sheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row + 1
BlankRowD = Sheets("Sheet1").Cells(Rows.Count, "D").End(xlUp).Row + 1
For i = 2 To LastRow2
Sheets("Sheet2").Range("A" & i).Copy
Worksheets("Sheet1").Range("A" & BlankRow).PasteSpecial xlPasteValues
BlankRow = BlankRow + 1
Next i
For i = 2 To LastRow2
Sheets("Sheet2").Range("B" & i).Copy
Worksheets("Sheet1").Range("B" & BlankRowB).PasteSpecial xlPasteValues
BlankRowB = BlankRowB + 1
Next i
For i = 2 To LastRow2
Sheets("Sheet2").Range("C" & i).Copy
Worksheets("Sheet1").Range("C" & BlankRowC).PasteSpecial xlPasteValues
BlankRowC = BlankRowC + 1
Next i
'========Here is my problem=====
Dim DivideBy As Integer
DivideBy = 1000
Worksheets("Sheet1").Select
Range("D" & BlankRowD).Value = DivideBy
For i = BlankRowD To x '===x is denoting wherever A, B, and C stop==='
Worksheets("Sheet1").Range("D" & BlankRowD).Copy
Worksheets("Sheet1").Range("D" & i).PasteSpecial xlPasteValue
Next i
End Sub
答案 0 :(得分:0)
这样的事情对你有用。设置要复制的范围非常有用,因为这样您就可以调整范围的行和/或列数,这样可以轻松地在D列中放入1000:
Sub NextTryThis()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rngCopy As Range
Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set ws2 = ActiveWorkbook.Sheets("Sheet2")
Set rngCopy = ws2.Range("A2", ws2.Cells(Rows.Count, "A").End(xlUp).Offset(, 2))
If rngCopy.Row < 2 Then Exit Sub 'No data
With ws1.Cells(Rows.Count, "A").End(xlUp).Offset(1)
'Get just the values from rngCopy to the next blank row in ws1
.Resize(rngCopy.Rows.Count, rngCopy.Columns.Count).Value = rngCopy.Value
'Put the desired number in column D for the copied data rows
.Offset(, 3).Resize(rngCopy.Rows.Count).Value = 1000
End With
End Sub