我有以下代码:
Dim alpha As String
Sub CopyDataDump()
Dim rSelected As Range
Dim c As Range
Dim bCol As Boolean
Dim bRow As Boolean
Dim vbAnswer As VbMsgBoxResult
Dim text As String
Dim text2 As String
Dim text3 As String
Dim First As String
Dim Second As String
Dim third As String
Dim Fourth As String
Dim ToRange As String
Dim ToBeta As String
'Set variables
bCol = False
bRow = False
'Prompt user to select cells for formula
On Error Resume Next
Set rSelected = Application.InputBox(prompt:= _
"Select Range to Copy", _
Title:=sTitle & " Creator", Type:=8)
On Error GoTo 0
'Only run if cells were selected and cancel button was not pressed
If Not rSelected Is Nothing Then
'Create string of cell references
For Each c In rSelected.Cells
alpha = c.address(bRow, bCol)
text = c
First = Left(text, 2)
text2 = WorksheetFunction.Replace(text, 1, 2, "")
Second = Left(text2, 2)
text3 = WorksheetFunction.Replace(text2, 1, 2, "")
third = Left(text3, 2)
Fourth = Right(text3, 2)
ActiveWorkbook.ActiveSheet.Range(c.address(bRow, bCol)) = Fourth & third & Second & First
Next
End If
ToBeta = Replace(alpha, "A", "f")
Application.ScreenUpdating = False
rSelected.Copy ActiveWorkbook.Worksheets("Hex_Dump_Flash").Range(ToBeta)
End Sub
我想做的...保留alpha中的第一个地址,而不是最后一个地址。它始终保留最后一个地址。 如果我的范围是A4:A8,则我希望该Alpha保留A4,而不保留A8。
非常感谢!
答案 0 :(得分:3)
您正在用每个新c覆盖For Each中的alpha(c成为循环中范围中的下一个单元格)。如果不覆盖,则需要在循环外使用它。要只获取第一个单元格,请使用
alpha = rSelected.Cells(1,1)
答案 1 :(得分:2)
您必须在For Each循环的第一个迭代中检测是否 字母
有一个值。然后,用第一个地址填充它,否则在循环中保留此值。您能用下面的这个块替换您的集团代码并尝试一下吗:
'Only run if cells were selected and cancel button was not pressed
alpha = ""
If Not rSelected Is Nothing Then
'Create string of cell references
For Each c In rSelected.Cells
If alpha = "" Then alpha = c.Address(bRow, bCol)
text = c
希望这可以为您提供帮助!