1

我编写了一个代码来连接 3 列,即 YR 对应于单元格 A2,JJ 对应于单元格 B2 和 YA 对应于单元格 D2。当我单击一个按钮时,我希望它显示 G2 的答案。这是我的代码:

Sub Button1_Click()
Dim YR As String
Dim JJ As String
Dim YA As String
Dim RF As String

YR = Range("A2")
JJ = Range("B2")
YA = Range("D2")
RF = YR & "." & JJ & "." & YA
Range("G2") = RF

End Sub

我现在的问题是我怎样才能让这个代码工作,直到有一个空白单元格?我已经搜索了一个关于此的代码:

Range("A1").Select 
Do Until IsEmpty(ActiveCell.Value) 
‘codes here
ActiveCell.Offset(1,0).Select 
Loop

我怎样才能制作我的代码并提前感谢!

4

2 回答 2

3

如果您不想循环,请尝试以下代码。

Sub Button1_Click()

    Dim lastRow As Long
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    Range("G2:G" & lastRow).FormulaR1C1 = "=RC[-6] & ""."" & RC[-5] & ""."" & RC[-3]"

End Sub

在此处输入图像描述

于 2013-05-19T12:22:31.293 回答
0

假设我已经理解了你的问题,下面的代码可能会有所帮助(虽然我没有测试过)

Sub catrow(row as integer)
Dim YR As String
Dim JJ As String
Dim YA As String
Dim RF As String

YR = Range("A" & row).value2
JJ = Range("B" & row).value2
YA = Range("D" & row).value2
RF = YR & "." & JJ & "." & YA
Range("G" & row) = RF
End Sub

Sub Button1_Click()
Dim Target As Range
Dim Row As Integer
Row = 1
Set Target = ActiveSheet.Range("A" & Row)
Do Until IsEmpty(Target.Value)
    catrow(row)
    Set Target = Target.Offset(RowOffset:=1)
Loop
End Sub
于 2013-05-19T11:21:23.350 回答