非连续命名范围到数组中,然后进入不同工作表中的行

Gre*_*reg 7 arrays excel vba excel-vba

我正在尝试将从非连续范围发布的数据放入单独工作表中的行中.在我构建非连续范围之前,这段代码完美无缺.我已经尝试了几个循环,但我尝试的任何东西都不会起作用.它不会复制远程数据.多年以来,我实际上已经完成了任何编码,而且我的重新学习曲线似乎阻碍了我......逻辑不是来找我.救命!

Sub UpdateLogWorksheet()

Dim historyWks As Worksheet
Dim inputWks As Worksheet

Dim nextRow As Long
Dim oCol As Long

Dim myCopy As Range
Dim myTest As Range
Dim myData As Range

Dim lRsp As Long

Set inputWks = Worksheets("Input")
Set historyWks = Worksheets("DataEntry")
oCol = 3 'order info is pasted on data sheet, starting in this column

'check for duplicate VIN in database
If inputWks.Range("CheckVIN") = True Then
  lRsp = MsgBox("VIN already in database. Update record?", vbQuestion + vbYesNo, "Duplicate VIN")
  If lRsp = vbYes Then
    UpdateLogRecord
  Else
    MsgBox "Please change VIN to a unique number."
  End If

Else

  'cells to copy from Input sheet - some contain formulas

  Set myCopy = inputWks.Range("VehicleEntry") 'non-contiguous named range

  With historyWks
      nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
  End With

  With inputWks
      'mandatory fields are tested in hidden column
      Set myTest = myCopy.Offset(0, 2)

      If Application.Count(myTest) > 0 Then
          MsgBox "Please fill in all the cells!"
          Exit Sub
      End If
  End With

  With historyWks
      'enter date and time stamp in record
      With .Cells(nextRow, "A")
          .Value = Now
          .NumberFormat = "mm/dd/yyyy hh:mm:ss"
      End With
      'enter user name in column B
      .Cells(nextRow, "B").Value = Application.UserName
      'copy the vehicle data and paste onto data sheet

      myCopy.Copy
      .Cells(nextRow, oCol).PasteSpecial Paste:=xlPasteValues, Transpose:=True
      Application.CutCopyMode = False
  End With

  'clear input cells that contain constants
  Clear
End If

End Sub
Run Code Online (Sandbox Code Playgroud)

Sid*_*out 7

这是一个例子来解释如何实现你想要的.请修改代码以满足您的需求.

让我们说,我有一个Sheet1如下所示的样子.有色细胞由我不连续的范围构成.

在此输入图像描述

现在将下面给出的代码粘贴到模块中并运行它.输出将在Sheet2和中生成Sheet3

Sub Sample()
    Dim rng As Range, aCell As Range
    Dim MyAr() As Variant
    Dim n As Long, i As Long

    '~~> Change this to the relevant sheet
    With Sheet1
        '~~> Non Contiguous range
        Set rng = .Range("A1:C1,B3:D3,C5:G5")

        '~~> Get the count of cells in that range
        n = rng.Cells.Count

        '~~> Resize the array to hold the data
        ReDim MyAr(1 To n)

        n = 1

        '~~> Store the values from that range into
        '~~> the array
        For Each aCell In rng.Cells
            MyAr(n) = aCell.Value
            n = n + 1
        Next aCell
    End With

    '~~> Output the data in Sheet

    '~~> Vertically Output to sheet 2
    Sheet2.Cells(1, 1).Resize(UBound(MyAr), 1).Value = _
    Application.WorksheetFunction.Transpose(MyAr)

    '~~> Horizontally Output to sheet 3
    Sheet3.Cells(1, 1).Resize(1, UBound(MyAr)).Value = _
    MyAr
End Sub
Run Code Online (Sandbox Code Playgroud)

垂直输出

在此输入图像描述

水平输出

在此输入图像描述

希望上面的例子可以帮助您实现您想要的目标.

  • 好一个席德!VBA中的容器对我来说是陌生的,您已经为我缩小了“范围”和“数组”之间的距离。这使我可以将来自多个工作簿的数据与具有不连续数据的多个工作表合并。MSDN提供的一些帮助。我觉得一个例子应该尽可能地笼统,然后从那里开始。 (2认同)