Excel 尝试偏移列并将数据添加到预定义的列数中

Excel 尝试偏移列并将数据添加到预定义的列数中,excel,vba,Excel,Vba,我将数据从存储为变量复制到工作表中。我需要将数据偏移2列(右侧),并能够在第一列中添加日期,在下一列中添加预定义的数字(存储为字符串) 虽然我可以偏移主数据块,但我无法确定如何将所需信息添加到所有偏移单元格中。我设法让它复制到数据的第一行,如果有帮助,我将v的行数存储为一个值(RowCounter)。只是为了添加背景信息,这是一个累积工作表,因此数据将不断添加 代码的当前部分: With SupSheet .Cells(.Rows.Count, "A").End(xlUp).Of

我将数据从存储为变量复制到工作表中。我需要将数据偏移2列(右侧),并能够在第一列中添加日期,在下一列中添加预定义的数字(存储为字符串)

虽然我可以偏移主数据块,但我无法确定如何将所需信息添加到所有偏移单元格中。我设法让它复制到数据的第一行,如果有帮助,我将
v
的行数存储为一个值(
RowCounter
)。只是为了添加背景信息,这是一个累积工作表,因此数据将不断添加

代码的当前部分:

  With SupSheet
      .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = Date
      .Cells(.Rows.Count, "A").End(xlUp).Offset(, 1).Value = SupCount
      .Cells(.Rows.Count, "A").End(xlUp).Offset(, 2).Resize(UBound(v, 1), UBound(v, 2)).Value = v
  End With

我设法在特定列中获得所需的单元格,用一个简单的小循环来填充,该循环在
LastRow+1
LastRow+RowCounter
之间添加数据。下面是我的全部代码。此线程涉及的部分介于“复制到相关匹配表”和“删除复制行”之间

Option Explicit

Public Sub CreateRETfile()
   Dim rawDataSheet, SupSheet As Worksheet
   Dim newBook As Workbook
   Dim v As Variant
   Dim cc As Variant
   Dim supID, SupCount, NamePrefix As String
   Dim footerCount, Rowcounter, i, j, k As Integer
   Dim lastrow As Long

'Store Supplier ID
Set rawDataSheet = ThisWorkbook.Worksheets("Raw Data")
supID = Trim(rawDataSheet.Range("A2").Value)

'Check for records
If IsEmpty(rawDataSheet.Cells(2, 2)) Then
MsgBox ("Error!" & vbNewLine & _
"No records found to create RET file." _
& vbNewLine & "Please add at least 1 record.")
Exit Sub
End If

 'Check for SupID as sheet and if it doesn't exist create it
On Error Resume Next
Set SupSheet = ThisWorkbook.Worksheets(supID)
lastrow = SupSheet.Cells(SupSheet.Rows.Count, "A").End(xlUp).Row + 1
On Error GoTo 0
If SupSheet Is Nothing Then
    With ThisWorkbook.Worksheets
        Set SupSheet = .Add(After:=.Item(.Count))
    End With
    SupSheet.Name = supID
    With ThisWorkbook.Worksheets(supID)
        .Range("A1").Resize(, 17) = Array( _
        "RET", "Init Supplier", "Init Agent", "Unique ID", "MPRN", _
        "House Name/Number", "Street", "Postcode", "Cust Name", "Cust Tele", _
        "No Contact", "CoS Date", "CC Date", "Reason", "Status", "Asso Supplier", "Asso Agent")
        .Range("BA1").NumberFormat = "00000"
        .Range("BA1").Value2 = "10000"
    End With
End If

ThisWorkbook.Worksheets(supID).Range("BA1").Value = ThisWorkbook.Worksheets(supID).Range("BA1").Value + 1
SupCount = Trim(ThisWorkbook.Worksheets(supID).Range("BA1").Text)
NamePrefix = "CTO" & supID & SupCount

'Count Rows to be Copied
cc = rawDataSheet.Range("A2:A10").Value
For j = 1 To UBound(cc, 1)
    If cc(j, 1) = Range("A2") Then Rowcounter = Rowcounter + 1
Next

'Create workbook
Set newBook = Workbooks.Add

'Copy Records
v = rawDataSheet.Range("B2:X" & Rowcounter + 1).Value
For i = 1 To UBound(v, 1)
    If v(i, 1) = "RET" Then footerCount = footerCount + 1
Next

'Write new sheet
With newBook.Worksheets(1)
    'Values
    .Range("A2").Resize(UBound(v, 1), UBound(v, 2)).Value = v
    'Header
    .Range("A1").Resize(, 13) = Array( _
        "ZHF", "CTO", "RET", supID, "RET", "RET", "6", "PROD", _
        NamePrefix & "RET.CSV", NamePrefix, _
        Format(Date, "ddmmyyyy"), "Unknown", "1")
    'Footer
    .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(, 3).Value = Array( _
        "ZFV", "BATCH" & SupCount, footerCount)
    'Name
    .Name = NamePrefix & "RET"

    'Save
    .SaveAs ThisWorkbook.Path & "\" & NamePrefix & "RET.CSV"
End With
newBook.Close savechanges:=False

'Copy to relevant matching sheet
With SupSheet
    .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 2).Resize(UBound(v, 1), UBound(v, 2)).Value = v
End With

'Fill in date and ID column
For k = lastrow To lastrow + Rowcounter
    If SupSheet.Cells(k, 3).Value = "RET" Then SupSheet.Cells(k, 1).Value = Date
    If SupSheet.Cells(k, 3).Value = "RET" Then SupSheet.Cells(k, 2).Value = SupCount
Next

'Remove copied rows
rawDataSheet.Range("B2:X" & Rowcounter + 1).EntireRow.Delete

End Sub

你能在
.cells(.Rows.count,“A”).End(xlUp).Offset(1,2).Resize(UBound(v,1),UBound(v,2)).Value=v
是的,但我已经有了这个数字(行数与
v
的大小相同)作为变量
RowCounter
存储在我的代码中。我需要获取列a xlup+1:列a+
RowCounter
=日期和列B xlup+1:列B+
RowCounter
=
SupCount
,但我不知道如何获取