我正在使用以下VBA代码插入一个空白行,其中excel中缺少顺序值。
$VMMServer = "calo-infvmm-10001.calocosn.local"
$a = ((Get-Date).ToUniversalTime()).ToString("yyyyMMddTHHmmss")
$clouds = Get-SCCloud -VMMServer $VMMServer
$vmHeader = [string]::Concat("VMId",",","SubscriptionId",",","VMName",",","OperatingSystem",",","Memory(MB)",",","CPUCount",",","DiskUsed(GB)",",","DiskAllocated(GB)",",","StorageClassification",",","Owner",",","CreationTime",",","SampleTime",",","Status",",","StartAction",",","StopAction")
Write-Output $vmHeader
foreach ($cloud in $clouds) {
$VMs = Get-SCVirtualMachine -Cloud $cloud
foreach ($VM in $VMs) {
$size = 0
$maxSize = 0
$classification = "Standard"
foreach ($disk in $VM.VirtualHardDisks) {
$classification = $disk.Classification
$size += $disk.Size / 1gb
$parentDisk = $disk.ParentDisk
while ($parentDisk) {
$size += $parentDisk.Size / 1gb
$parentDisk = $parentDisk.ParentDisk
}
$maxSize += $disk.MaximumSize / 1gb
}
$vmText = [string]::Concat($vm.ID,",",$vm.UserRoleID,",",$vm.Name,",",$vm.OperatingSystem,",",$vm.Memory,",",$vm.CPUCount,",",$size,",",$maxSize,",",$classification,",",$vm.Owner,",",$vm.CreationTime,",",$a,",",$vm.Status,",",$vm.StartAction,",",$vm.StopAction)
Write-Output $vmText
}
}
除非缺少最后一个值,否则此方法正常
例如,我填写了5组的空白。
中间数字丢失的地方:
1
2
4
5
代码将为缺失值插入一个空行,使其成为:
1
2
4
5
但是,如果缺少最后一个值5,则不会插入一行
所以:
1
2
4
变为:
1
2
4
有没有办法设置最大值以确保最终值被识别为缺失?
答案 0 :(得分:2)
试试这个......
Sub test()
Dim i As Long, x, r As Range, lMax As Long, lRw As Long
lRw = Range("b" & Rows.Count).End(xlUp).Row + 1
lMax = InputBox("Enter Maximum Value", "Maximum Input Req.", Application.Max(Range("B2:B" & lRw)))
For i = lRw To 2 Step -1
If i = lRw Then
x = lMax - Mid$(Cells(i - 1, "b"), 2)
If x > 1 Then
Cells(i - 1, "b").AutoFill Cells(i - 1, "b").Resize(x + 1), 2
End If
Else
x = Mid$(Cells(i, "b"), 2) - Mid$(Cells(i - 1, "b"), 2)
If x > 1 Then
Rows(i).Resize(x - 1).Insert
Cells(i - 1, "b").AutoFill Cells(i - 1, "b").Resize(x), 2
End If
End If
Next
End Sub
修订代码
Sub test()
Dim i As Long, x, r As Range, lMax As Long, lRw As Long
lRw = Range("b" & Rows.Count).End(xlUp).Row + 1
lMax = InputBox("Enter Maximum Value", "Maximum Input Req.", Application.Max(Range("B2:B" & lRw)))
For i = lRw To 2 Step -1
If i = lRw Then
x = lMax - Cells(i - 1, "b").Value
If x > 1 Then
Cells(i - 1, "b").AutoFill Cells(i - 1, "b").Resize(x + 1), 2
End If
Else
x = Cells(i, "b").Value - Cells(i - 1, "b").Value
If x > 1 Then
Rows(i).Resize(x - 1).Insert
Cells(i - 1, "b").AutoFill Cells(i - 1, "b").Resize(x), 2
End If
End If
Next
End Sub
答案 1 :(得分:0)
这个答案是在另一个论坛上给我的:
http://www.ozgrid.com/forum/showthread.php?t=200184&goto=newpost **
Sub Reply()
i = 1
Do While Cells(i, 2) <> ""
j = Cells(i + 1, 2).Value - Cells(i, 2).Value - 1
If j < 0 Then j = 8 - Cells(i, 2).Value + Cells(i + 1, 2).Value
For k = 1 To j
Rows(i + k).EntireRow.Insert
Next k
i = i + k
Loop
End Sub