我已经制作了连接3列的代码,即对应于单元格A2的YR,对应于单元格B2的JJ和对应于单元格D2的YA。当我点击一个按钮,我希望它显示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
我如何制作我的代码并感谢先进!
答案 0 :(得分: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
答案 1 :(得分: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