1

I am trying to copy values from sheet1 to sheet2. The number of rows of sheet1 varies so I need to store it in a variable. I need something like:

Worksheets("Sheet1").Range("A2:Ab").Select

, where "b" is a variable that stores the number of rows in Sheet1.

Thank you.

4

2 回答 2

4

您实际上可以存储UsedRange工作表的。或者直接复制到另一张纸上。例如

Set oWS1 = Worksheets("Sheet1")
Set oWS2 = Worksheets("Sheet2")
oWS2.UsedRange.Clear ' Clear used range in Sheet2
oWS1.UsedRange.Copy oWS2.Range("A1") ' Copies used range of Sheet1 to A1 of Sheet2

'Set oRng = oWS1.UsedRange ' Sets a Range reference to UsedRange of Sheet1

要获取工作表的最后一行:

lLastRow = oWS1.UsedRange.SpecialCells(xlLastCell).Row

编辑(从 UsedRange 的地址获取最后一行):

Dim sAddr as String, lLastRow As Long
sAddr = oWS1.UsedRange.Address
lLastRow = CLng(Mid(sAddr, InStrRev(sAddr , "$") + 1))
于 2013-11-08T01:49:54.803 回答
0

一种方法是通过连接列和行标识符,然后使用 Range(Cell1,Cell2) 重载来动态创建单元格名称,如下所示:

Dim rng As Range
Dim intStart As Integer
Dim intEnd As Integer
Dim strStart As String
Dim strEnd As String

'Let's say your range is A2 to A10
intStart = 2
intEnd = 10
strStart = "A" & intStart
strEnd = "A" & intEnd

Set x = Range(strStart, strEnd)
于 2013-11-08T01:53:28.923 回答