excelmacros将单元格中的值添加到表格中

我正在寻找使用macros每次按下一个button时,将工作表中某个单元格的值添加到另一个工作表中的一个表。 例如,第一次按下button时,我希望工作表2中的单元格A1的值等于工作表1中的单元格C3,下一次工作表2中的B1等于工作表1中的C3,依此类推。 如果该macros被添加到的单元格为空,该macros应该只向表中添加一个值。

这是我迄今为止:

Sub Button32_ClickandUpdate myVariable = Worksheets("Worksheet1").Range("C3") For i = 1 To 6 If IsEmpty(Cells(1, i)) Then Worksheets("Worksheet2").Range(Cells(1,i)) = myVariable Exit Sub End If Next i End Sub 

任何帮助将不胜感激。

尝试下面的代码,一次更新一个单元格(当单元格为空时)

 Sub Button32_ClickandUpdate() For i = 1 To 6 If IsEmpty(Sheets("Worksheet2").Cells(1, i).Value) Then Sheets("Worksheet2").Cells(1, i).Value = Sheets("Worksheet1").Range("C3").Value Exit Sub End If Next i End Sub 

评论 :你可以使用这个也没有.Value

尝试这个:

 Sub Button32_ClickandUpdate myVariable = Worksheets("Worksheet1").Range("C3") For i = 1 To 6 If IsEmpty(Worksheets("Worksheet2").Cells(1, i)) Then Worksheets("Worksheet2").Cells(1, i) = myVariable 'Exit Sub (remove or comment this line to update all cells at the sime time) End If Next i End Sub 

编辑根据新的描述给出:

 Sub Button32_ClickandUpdate myVariable = Worksheets("Worksheet1").Range("C3") i = 1 While i < 7 If IsEmpty(Worksheets("Worksheet2").Cells(1, i)) Then Worksheets("Worksheet2").Cells(1, i) = myVariable End If i = i + 1 Wend End Sub