我是VBA的新手,我正在尝试编写一个基本宏,以便将Ctrl + A / C网页中的数据转换成容易复制到Word文档中的行。数据位之一作为表存在,并且行数可以变化。桌子后面有数据,所以我不能使用lastrow并且我的代码不起作用。非常感谢您提供一些帮助。
Sub Fill()
Dim wsi As Worksheet
Dim wscpy As Worksheet
Set wsi = Sheets("Raw Data Info")
Set wsa = Sheets("Raw Data Accts")
Set wscpy = Sheets("Copy to fund details")
wscpy.Range("A1").Value = "Name: " & wsi.Range("A36").Value
wscpy.Range("A2").Value = "D.O.B.: " & wsi.Range("B92").Value
wscpy.Range("A3").Value = "Address: " & wsi.Range("C42") & ", " & wsi.Range("C44") & ", " & wsi.Range("C45") & " " & wsi.Range("C47") & " " & wsi.Range("C46")
'...blah blah...
wscpy.Range("A13").Value = "Gross Monthly Income: " & wsi.Range("B89")
wscpy.Range("A14").Value = "Accounts held: "
'This is the bit I have problems with
wscpy.Range("A15").Value = wsa.Range("A43", wsa.Range("A43").End(xlDown))
wscpy.Range("A16").Value = wsa.Range("A43", wsa.Range("A43").End(xlDown))
End Sub
因此,accounts表基本上是这样的
https://gyazo.com/6355ebfe0ebdaf4f0396fc1e632dd34d
我如何编写代码以列出wscpy中其对应单元格中的数据(帐户和号码)?
一如既往,在此先感谢您!
答案 0 :(得分:0)
我认为您需要从wsa获取“最后一行”。
Sub Fill()
Dim wsi As Worksheet
Dim wscpy As Worksheet
Dim LastRow as Long
Set wsi = Sheets("Raw Data Info")
Set wsa = Sheets("Raw Data Accts")
Set wscpy = Sheets("Copy to fund details")
wscpy.Range("A1").Value = "Name: " & wsi.Range("A36").Value
wscpy.Range("A2").Value = "D.O.B.: " & wsi.Range("B92").Value
wscpy.Range("A3").Value = "Address: " & wsi.Range("C42") & ", " & wsi.Range("C44") & ", " & wsi.Range("C45") & " " & wsi.Range("C47") & " " & wsi.Range("C46")
'...blah blah...
wscpy.Range("A13").Value = "Gross Monthly Income: " & wsi.Range("B89")
wscpy.Range("A14").Value = "Accounts held: "
'you can get LastRow here
'LastRow = wsa.Range("A43").End(xlDown).Row
'or would be better if you use this code
LastRow = wsa.Cells(Rows.Count,1).End(xlUp).Row
wscpy.Range("A15:A" & LastRow -28 ).Value = wsa.Range("A43:A" & LastRow).Value
'I am not sure why you need this
'wscpy.Range("A16:A" & LastRow -27").Value = wsa.Range("A43:A" & LastRow).Value
End Sub
希望获得帮助。
谢谢