ActiveSheet.Range(“ B3:B26”)。Value

时间:2018-07-17 13:14:13

标签: excel vba excel-vba

我对其他人所做的事情相当简单,但对我自己而言却很复杂。

我编写了接受CSV的代码,将其打开并另存为XLSX。很好,但我需要它来拾取B3:B26中某些文件夹的位置

   'Assign Variables
Dim objFSO As Object, objPickup As Object, objDropoff As Object, objFile As Object
Dim wb As Workbook, Dropoff As String, Pickup As String
Dim B3 As String, B26 As String
Dim LastRowMonthly46 As Long, b As Long, c As Long
Dim ADay As Integer, AMonth As Integer, AYear As Integer, myDate As Date
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Go to worksheet Menu
Worksheets("Menu").Activate
'Make variable Pickup equal to value of B3
Pickup = ActiveSheet.Range(B3 & ":" & B26).Value

'Make variable Dropoff equal to value of B6
Dropoff = ActiveSheet.Range("B28").Value
'Go to worksheet Report
Worksheets("Report").Activate
'Make tab report visible
Worksheets("Report").Visible = True
'Make tab Menu invisible
Worksheets("Menu").Visible = False
'Get the folder object associated with the directory
Set objPickup = objFSO.GetFolder(Pickup)
Set objDropoff = objFSO.GetFolder(Dropoff)
'Set values for cells A1,B1 and C1 and align text
Worksheets("Report").Range("A1").Value = "The files found in " & objPickup.Name & " are:"

我正在考虑for循环,但是我的Pickup变量不会让我做一些简单的事情吗? 谢谢

1 个答案:

答案 0 :(得分:0)

Public Sub TestMe()

    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim B3 As Range: Set B3 = ws.Range("B3")
    Dim B26 As Range: Set B26 = ws.Range("B26")

    Dim pickUp As Variant
    With Application
        pickUp = .Transpose(ws.Range(B3, B26))
    End With

    Dim i As Long
    For i = LBound(pickUp) To UBound(pickUp)
        Debug.Print pickUp(i)
    Next i

End Sub
  • 使用Application.Transpose可以很容易地将范围转换为数组,如果它是单行,例如B3至B26;

  • B3B26被声明为范围,因此ws.Range(B3,B26)被传递到pickUp数组;

  • 遍历数组并显示其值可以通过引用数组的LBoundUBound来完成;