简体   繁体   中英

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. All fine but i need it to pick up the location of certain folders which are in 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? 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;

  • B3 and B26 are declared as ranges and thus the ws.Range(B3,B26) is passed to the pickUp array;

  • looping through array and showing its values can be done with referring the LBound and the UBound of the array;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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