首先,我是一个完整的VBA磨砂膏。我正在学习,因为我们在工作中使用了很多excel,而且通常表单都是基本的,没有任何智能。我正在尝试创建一个简单的宏,它在工作表的顶部创建一个新行,然后用一些空白和一些基于前面行的信息填充它。到目前为止它一直很好,但我现在正处于我希望它填写下一个IP的位置,它应该从前一行读取。这是我的代码到目前为止,我正在努力在Range(“G2”)之后填写什么。值=为了使它增加G3中的IP fround。
Sub newloc()
Range("A2").EntireRow.Insert
Range("D2").Value = "Odense C"
Range("F2").Formula = "=F3 + 1"
Range("G2").Value =
Range("H2").Formula = "=H3 + 1"
End Sub
正如您所看到的,代码非常简单,而且我对VBA脚本的了解还不多,请提供简单明了的答案。最好是我可以复制粘贴的东西,然后对功能进行解释,这样我就可以理解为什么它会突然发挥作用。
答案 0 :(得分:0)
CHANGED!
放置此功能:
Function getNextIp(getIp As String) As String
Dim numArr As Variant
numArr = Split(getIp, ".")
If numArr(3) = 255 Then
If numArr(2) = 255 Then
If numArr(1) = 255 Then
getNextIp = (numArr(0) + 1) & "." & "1" & "." & "1" & "." & "1"
Else
getNextIp = numArr(0) & "." & (numArr(1) + 1) & "." & "1" & "." & "1"
End If
Else
getNextIp = numArr(0) & "." & numArr(1) & "." & (numArr(2) + 1) & "." & "1"
End If
Else
getNextIp = numArr(0) & "." & numArr(1) & "." & numArr(2) & "." & (numArr(3) + 1)
End If
End Function
之后你的代码将是这样的:
Dim IpText As String
Sub newloc()
Range("A2").EntireRow.Insert
Range("D2").Value = "Odense C"
Range("F2").Formula = "=F3 + 1"
IpText = Range("G3").Value
Range("G2").Value = getNextIp(IpText)
Range("H2").Formula = "=H3 + 1"
End Sub
答案 1 :(得分:0)
编辑
您可能想要使用此功能:
Optiion Explicit
Function GetNextIPAddress(baseIP As String) As String
Dim numbersArr As Variant
Dim iPos As Long, iNum As Long
numbersArr = Split(baseIP, ".") '<--| get all numbers delimited by a "." from passed string into an array
For iNum = UBound(numbersArr) To 0 Step -1
If numbersArr(iNum) <> "254" Then Exit For
Next iNum
If iNum = -1 Then
GetNextIPAddress = "Error"
Else
iPos = InStrRev(baseIP, numbersArr(iNum))
GetNextIPAddress = Left(baseIP, iPos - 1) & numbersArr(iNum) + 1 & Right(baseIP, Len(baseIP) - (iPos + Len(numbersArr(iNum))) + 1)
End If
End Function
在您的mnain代码中被利用如下
Range("G2").Value = GetNextIPAddress(Range("G3").Value)
答案 2 :(得分:0)
试试这个。它最多可达254,然后在下一个时间内获得“+1”。
/**
* Applies a supplemental hash function to a given hashCode, which
* defends against poor quality hash functions. This is critical
* because HashMap uses power-of-two length hash tables, that
* otherwise encounter collisions for hashCodes that do not differ
* in lower bits. Note: Null keys always map to hash 0, thus index 0.
*/
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
在上图中,我编辑了单元格以模拟更大的范围。