简体   繁体   English

数组以循环遍历单元格值Excel VBA

[英]Array to Loop through Cell Values Excel VBA

I have defined cells inside of two separate arrays. 我在两个单独的数组中定义了单元格。 (ie source_arr = ("B4","B5"...) and target_arr = ("B5","B6") (即source_arr = ("B4","B5"...)target_arr = ("B5","B6")

I want to loop through both arrays and set the value of the target workbook cells equal to that of the source workbook cells. 我想遍历两个数组,并将目标工作簿单元格的值设置为等于源工作簿单元格的值。 Right now it sets all the cells equal to one value. 现在,它将所有像元设置为一个值。

 For i = LBound(source_array) To UBound(source_array)
For j = LBound(target_array) To UBound(target_array)
Data = source_workbook.Sheets("Questionnaire").Cells(source_array(i)).Value
target_workbook.Sheets("Questionnaire").Cells(target_array(j)).Value = Data
Next j
Next i

You only need to one loop. 您只需要一个循环。 And you want Range not Cells: 而且您希望范围不是单元格:

For i = LBound(source_array) To UBound(source_array)
    Data = source_workbook.Sheets("Questionnaire").Range(source_array(i)).Value
    target_workbook.Sheets("Questionnaire").Range(target_array(i)).Value = Data
Next i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM