简体   繁体   中英

Excel vba get a column's range with only column's number

I need to get a range of cells from a column number k with rows from e to f . Variables may vary.

Is there a way to do so without converting column's number to a string and doing other string manipulations ?

So far that's what I found:

Sub testRanges()
    Dim tempRange As Range
    Set tempRange = ThisWorkbook.Worksheets("setup").Columns(2)
  '  tempRange.Select  'it works fine
    Dim cell As Range
    Dim i As Integer
    i = 1
    For Each cell In tempRange.Cells
        ThisWorkbook.Worksheets("setup").Cells(i, 7).value = cell.value 'works
        i = i + 1
    Next
End Sub

However I don't know the syntax to make it pick only rows 5-80

Dim e as Integer;
Dim f As Integer;
Dim k As Integer;

e=5;
f=80;
k=6;

in sixth Columns(k) . Any ideas?

Example: you're interested in Cells 5 and 6 of column 2. You could use:

With ThisWorkbook.Worksheets("setup")
    For Each cell In .Range(.Cells(5, 2), .Cells(6, 2))
        '...
    Next
End With

Any number above can be replaced by a numeric variable, typically of type Long.

If i understand you correct you want the range to be F5:F80.(unless you have 1R1C table format)

Sub testy()
  Dim e As Integer
  Dim f As Integer
  Dim k As Integer

  e = 5
  f = 80
  k = 6


  Range(Cells(e, k), Cells(f, k)).Select
End Sub

If you have to take the intersection of rows and columns, in VBA the command for this is Intersect() .

Try this:

Sub Test
    Dim k as range
    set k =intersect(Rows("3:4"),columns("E"))
    k.select
End Sub

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