Excel宏引用单元格文本

时间:2016-10-21 10:49:24

标签: excel excel-vba vba

我正在组合一个基本宏来格式化列以包含引用字母。例如,一列有1,2,3,并且有一个单元格,用户可以在其中输入一些字母并单击按钮。例如ABC。这在工作时应将1,2,3格式化为ABC1ABC2ABC3等。

我已经实现了这一点,但它只适用于字母A.见下文:

Sub Macro4()

    Range("A3:A60").Select

    Selection.NumberFormat = Range("k11").Text & "0" & "0" & "0"

End Sub

1 个答案:

答案 0 :(得分:0)

这是我的尝试。我确信有更好的方法:

Option Explicit

Sub TestMacro()

Dim MyRange As Range
Dim MyReference As Range
Dim MyArray() As Variant
Dim Counter As Long
Dim wf As WorksheetFunction
Dim Cell As Range
Dim val As Integer

Application.ScreenUpdating = False
Set wf = Application.WorksheetFunction
Set MyRange = Range("A3:A60")
For Each Cell In MyRange
    val = Application.Evaluate("=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9}," & Cell.Address & "&" & """0,1,2,3,4,5,6,7,8,9""" & "))")
    Cell = CInt(Mid(Cell, val, Len(Cell) - val + 1))
Next Cell

Set MyReference = Range("B3")
MyArray = Application.Transpose(MyRange)
For Counter = LBound(MyArray) To UBound(MyArray)
    MyArray(Counter) = MyReference & CStr(MyArray(Counter))
Next Counter

MyRange = Application.Transpose(MyArray)
Application.ScreenUpdating = True

End Sub