简体   繁体   English

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

[英]ActiveSheet.Range(“B3:B26”).Value

I am doing something rather simple to anyone else but complex to myself. 我对其他人所做的事情相当简单,但对我自己却复杂。

I have written code that takes a CSV, opens it and saves as a XLSX. 我编写了接受CSV的代码,将其打开并另存为XLSX。 All fine but i need it to pick up the location of certain folders which are in B3:B26 很好,但我需要它来拾取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:"

I am thinking a for loop but is my Pickup variable not going to allow me to do something simple? 我在考虑for循环,但是我的Pickup变量不会允许我做一些简单的事情吗? Thanks 谢谢

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
  • Converting range to array is easily done with Application.Transpose , if it is a single row, like in the case B3 to B26; 如果它是单行,则可以使用Application.Transpose轻松地将范围转换为数组,例如B3至B26;

  • B3 and B26 are declared as ranges and thus the ws.Range(B3,B26) is passed to the pickUp array; B3B26被声明为范围,因此ws.Range(B3,B26)被传递给pickUp数组;

  • looping through array and showing its values can be done with referring the LBound and the UBound of the array; 遍历数组并显示其值可以通过引用数组的LBoundUBound来完成;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM