获取特定范围Vba

  • 本文关键字:Vba 范围 获取 vba
  • 更新时间 :
  • 英文 :


我有四个数据集,它们组合在一个单独的工作表中。不幸的是,我无法手动将这四个组合在一起。我抓住每个数据集,将它们一个粘贴在另一个上面(数据1,然后数据2,然后数据3,然后数据4(将所有数据粘贴到工作表上后,我需要对每个数据集进行一些更改。我想添加另一列,标记数据的来源(如果它来自数据集1,那么它会说"数据1"(。此刻,我不知道如何抓住合适的范围。我也不能只得到行号,因为每个数据集都是流动的,将来都会添加/删除行。

'copying the data to the report - data 1
cashData.Range("B5:B" & rgCash_lastrow).Copy cnCS.Range("E3")
cnCS.Range("E3:E" & cnCS.Range("G" & Rows.Count).End(xlUp).row) = "Data 1"
Dim report_lastrow As Long
report_lastrow = cnCS.Cells(Cells.Rows.Count, 7).End(xlUp).row
'copying the data to the report - data 2
safeData.Range("B5:B" & rgSafe_lastrow).Copy cnCS.Range("E" & 
report_lastrow + 1)
'What range can i put in to get range = "Data 2"

Dim report_lastrow2 As Long
report_lastrow2 = cnCS.Cells(Cells.Rows.Count, 7).End(xlUp).row
'copying the data to the report - data 3
pictetData.Range("B5:B" & rgPictet_lastrow).Copy cnCS.Range("E" & report_lastrow2 + 1)
'What range can i put in to get range = "Data 3" 
Dim report_lastrow3 As Long
report_lastrow3 = cnCS.Cells(Cells.Rows.Count, 7).End(xlUp).row
'copying the data to the report - data 4
rothData.Range("B5:B" & rgRoth_lastrow).Copy cnCS.Range("E" & report_lastrow3 + 1)
'What range can i put in to get range = "Data 4"

请尝试下一种方式/code。我(仅(假设您尝试将标签放置在列"中;G: G":

Sub writeInARange()
'your existing code where the sheets objects are declared and Set
'...............................
Dim lastRow As Long, report_lastrow As Long

lastRow = cashData.Range("B" & cashData.rows.count).End(xlUp).row
cashData.Range("B5:B" & lastRow).Copy cnCS.Range("E3")
cnCS.Range("G3:G" & cnCS.Range("E" & rows.count).End(xlUp).row).value = "Data 1"

'copying the data to the report - data 2
lastRow = safeData.Range("B" & safeData.rows.count).End(xlUp).row
report_lastrow = cnCS.Range("E" & rows.count).End(xlUp).row
safeData.Range("B5:B" & lastRow).Copy cnCS.Range("E" & report_lastrow + 1)
cnCS.Range("G3:G" & cnCS.Range("E" & rows.count).End(xlUp).row).value = "Data 2"

'copying the data to the report - data 3
lastRow = pictetData.Range("B" & pictetData.rows.count).End(xlUp).row
report_lastrow = cnCS.Range("E" & rows.count).End(xlUp).row
pictetData.Range("B5:B" & lastRow).Copy cnCS.Range("E" & report_lastrow + 1)
cnCS.Range("G3:G" & cnCS.Range("E" & rows.count).End(xlUp).row).value = "Data 3"

'copying the data to the report - data 4
lastRow = rothData.Range("B" & rothData.rows.count).End(xlUp).row
report_lastrow = cnCS.Range("E" & rows.count).End(xlUp).row
rothData.Range("B5:B" & lastRow).Copy cnCS.Range("E" & report_lastrow + 1)
cnCS.Range("G3:G" & cnCS.Range("E" & rows.count).End(xlUp).row).value = "Data 4"
End Sub

没有经过测试,但如果我正确理解您的需求,它应该会起作用。

最新更新