VBA循环-将单元格复制并粘贴到下一列,直到单元格x等于单元格y



我需要一些帮助循环。我有一段时间没有使用VBA,并开始再次学习。我记得这个社区过去帮助了我很多,所以任何帮助都很感激。

我想将单元格H12复制到下一个以i12开始的空列中,然后是J12,以此类推。所以我要继续循环直到粘贴的数组的数目等于单元格D12中的数目。如果单元格D12 = 20我想继续这个循环复制H12直到AB12。

一旦完成,我想移动到下一行H13并做同样的事情。在这个例子中,D13 = 15,所以我们像上面一样复制H13,直到我们得到R13。

任何帮助都是非常感激的。我已经尝试了一些循环,但没有成功。

假设您选择的单元格是H12,它右边的单元格是空的,D12用一个正数值填充,下面的代码应该可以工作:

Sub CopyToRange()
Dim ThisCol As Integer, ThisRow As Long, CurS As Worksheet, CurRg As Range, InfCol As Integer
Set CurS = ActiveSheet
ThisRow = ActiveCell.Row
ThisCol = ActiveCell.Column
InfCol = 4 'column 'D'
Set CurRg = Range(CurS.Cells(ThisRow, ThisCol + 1), CurS.Cells(ThisRow, ThisCol + CurS.Cells(ThisRow, InfCol).Value))
ActiveCell.Copy
CurRg.PasteSpecial (xlPasteAll)
End Sub

如果您选择具有相同前提条件的下一行,它也会起作用

复制单元格值

用法(OP)

  • 将所有代码复制到标准模块中,例如Module1
  • 调整常量部分的值

How to Test (Anyone)

  • 添加一个新的工作簿(或者只打开Excel)。在VBE中添加一个新的标准模块并将代码复制到其中。在Excel中,在工作表Sheet1中,在D列中,从单元格D12开始,添加一些正整数(整数),在H列中各自的单元格中添加需要复制的值。执行DuplicateCellValues程序

代码

Option Explicit
Sub DuplicateCellValues()
' Needs the 'RefColumn' function.
Const ProcTitle As String = "Duplicate Cell Values"

Const wsName As String = "Sheet1"
Const sFirst As String = "D12" ' Column 'D': number of duplicates.
Const dfCol As String = "H" ' Column 'H': value to duplicate.

' Create a reference to the workbook ('wb').
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Create a reference to the worksheet ('ws').
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
' Create a reference to the Source First Cell ('sfCell').
Dim sfCell As Range: Set sfCell = ws.Range(sFirst)
' Create a reference to the Source Column Range ('scrg').
Dim scrg As Range: Set scrg = RefColumn(sfCell)
' Check if no data in the Source Column Range was found.
If scrg Is Nothing Then
' Inform and exit.
MsgBox "There is no data in the one-column range '" _
& sfCell.Resize(ws.Rows.Count - sfCell.Row + 1).Address(0, 0) _
& "'.", vbCritical, ProcTitle
Exit Sub
End If

Application.ScreenUpdating = False

Dim sCell As Range ' Current Source Cell
Dim drrg As Range ' Destination Row Range
Dim dfCell As Range ' Destination First Cell

' Loop through the cells ('sCell') of Source Column Range.
For Each sCell In scrg.Cells
' Create a reference to the current Destination First Cell.
Set dfCell = sCell.EntireRow.Columns(dfCol)
' Attempt to create a reference to the Destination Row Range.
' It may fail if there is no whole number in the current Source Cell,
' or if the number is too small, or if it is too big,... etc.
On Error Resume Next
Set drrg = dfCell.Offset(0, 1).Resize(1, sCell.Value)
On Error GoTo 0
' If the reference was created...
If Not drrg Is Nothing Then ' *** Destination Row Range referenced.
' Write the value from the current First Destination Cell
' to the cells of the Destination Row Range.
drrg.Value = dfCell.Value
' Dereference the Destination Row Range for the 'On Error Resume Next'
' to work 'correctly'.
Set drrg = Nothing
'Else ' *** Destination Row Range NOT referenced.
End If
Next sCell

Application.ScreenUpdating = True

' Inform.
MsgBox "Cells duplicated.", vbInformation, ProcTitle

End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Creates a reference to the one-column range from the first cell
'               of a range ('FirstCell') to the bottom-most non-empty cell
'               of the first cell's worksheet column.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RefColumn( _
ByVal FirstCell As Range) _
As Range
If FirstCell Is Nothing Then Exit Function

With FirstCell.Cells(1)
Dim lCell As Range
Set lCell = .Resize(.Worksheet.Rows.Count - .Row + 1) _
.Find("*", , xlFormulas, , , xlPrevious)
If lCell Is Nothing Then Exit Function
Set RefColumn = .Resize(lCell.Row - .Row + 1)
End With
End Function

试试这个:

Option Explicit
Sub duplicate()
Dim arr, LastRow As Long
With Sheet8
LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
arr = .Range(.Cells(12, 4), .Cells(LastRow, 100)).Value2
End With

Dim j As Long, i As Long, ii As Long: ii = 1
For j = 1 To UBound(arr)
For i = 6 To 5 + (arr(j, 1) * ii)
arr(j, i) = arr(j, 5)
Next i
Next j

With Sheet8
.Range(.Cells(12, 4), .Cells(LastRow, 100)) = arr 'dump updated array to invoice sheet
End With
End Sub

最新更新