我正在尝试将值从sheet1复制到sheet2。 sheet1的行数不同,所以我需要将它存储在变量中。我需要这样的东西:
Worksheets("Sheet1").Range("A2:Ab").Select
,其中“b”是存储Sheet1中行数的变量。
谢谢。
答案 0 :(得分:4)
您实际上可以存储工作表的UsedRange
。或者直接复制到另一张表。 e.g。
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))
答案 1 :(得分: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)