我正在尝试编写一个循环访问列值并在带有字符串的单元格之间应用数字1,2,3 ... n的函数。例如:
数据:
hefew
1
3
2
6
bkifew
3
4
2
1
3
我希望函数将值更改为:
hefew
1
1
1
1
bkifew
2
2
2
2
2
可能有多个字符串,所以最终值可能会达到15左右。
我已经启动了基本功能,但是我对VBA不够熟悉,无法使用逻辑。我使用Python编程,通常会使用该语言执行类似的操作。但是,我不得不将此保留在excel中。
当前工作:
Sub Button2_Click()
Dim rng As Range, cell As Range
cellcount = CountA("A1:A1000")
Set rng = Range("A1:A10")
For Each cell In rng
a = cell.Value
If IsNumeric(a) = True Then
cell.Value = 1
Else
cell.Value = 0
End If
Next cell
End Sub
我认为for循环不可能做到这一点。我可以使用某种搜索和替换功能吗?
答案 0 :(得分:2)
尝试这些,假设数字不是公式的结果。
Sub x()
Dim r As Range, n As Long
For Each r In Columns(1).SpecialCells(xlCellTypeConstants, xlNumbers).Areas
n = n + 1
r.Value = n
Next r
End Sub
答案 1 :(得分:0)
我创建了一个函数,而不是sub,虽然有点混乱,但它可以工作。在我的电脑上测试
Public Function Test(checkrange As Range, checkcell As Range)
Dim cll As Range
Dim arr() As Variant
ReDim Preserve arr(1 To checkrange.Cells.Count)
If IsNumeric(checkcell.Value) = False Then
Test = checkcell.Value
Exit Function
End If
y = 1
For Each cll In checkrange
If IsNumeric(cll.Value) Then
arr(y) = 1
Else
arr(y) = 0
End If
y = y + 1
Next cll
m = 1
For Each cll In checkrange
If cll.Address = checkcell.Address Then
rownumber = m
Exit For
End If
m = m + 1
Next cll
m = 0
For i = LBound(arr) To UBound(arr)
If arr(i) = 0 Then
m = m + 1
End If
If i = rownumber Then Exit For
Next i
Test = m
End Function