你好StackOverFlow社区,
不久之前,我开始使用excel vba,对于一个有点复杂的问题,我确实需要一些帮助。
我有一个电子表格,其中列有"Prime“部件及其下面的”可选“部分。我需要创建一个宏,它将将变量替换部分转到其关联的Prime部分的右侧。例如,在A栏中,"P“是素数,"A”是Altenates:
A _x
1P _x
1A
1A
1A
2P _x
2A
2A
3P区
3A x
我试图创建一个宏,它将给我以下结果:
A
1P _ 1A _ 1A _ 1A _1-1A
1A
1A
1A
2P _ 2A _ 2A
2A
2A
3P _ 3A
3A x
下面是我能够想出的代码,但是所有的备用部分都合并成一个范围,并转到列表的第一个主要部分。我知道这可能不是我想要完成的最好的方法。我愿意听取所有建议,并期待着听到一些令人敬畏的解决方案。
请注意,上面示例中粗体的素数部分实际上是在我的电子表格中突出显示的,这将解释代码中的"colorindex = 6“
Sub NewHope()
Dim cell As Range
Dim LastRow As Long
Dim Prime As Range
Dim alt As Range
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For Each cell In Range("A2:A" & LastRow)
If cell.Interior.ColorIndex = 6 Then
If Prime Is Nothing Then
Set Prime = cell
End If
Else
If alt Is Nothing Then
Set alt = cell
Else
Set alt = Union(alt, cell)
End If
End If
Next
alt.Copy
Prime.Offset(0, 4).PasteSpecial Transpose:=True
End sub发布于 2017-11-24 21:50:09
试试下面的代码:
Sub test()
Dim cell As Range
Dim LastRow As Long
Dim PrimeRow As Long
Dim PrimeColumn As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For Each cell In Range("A2:A" & LastRow)
If cell.Interior.ColorIndex = 6 Then
PrimeRow = cell.Row
PrimeColumn = cell.Column + 1
Else
Cells(PrimeRow, PrimeColumn).Value = cell.Value
PrimeColumn = PrimeColumn + 1
End If
Next
End Sub发布于 2017-11-24 21:50:14
If Prime Is Nothing Then上面的代码似乎不能满足您的要求;它不会重置‘质数’单元格,因为在第一个‘质数’单元格的第一个位置之后,Prime将不再是一个空单元。
dim r as long, pr as long
For r=2 to Range("A" & Rows.Count).End(xlUp).Row
If cells(r, "A").Interior.ColorIndex = 6 Then
pr = r
Else
cells(pr, columns.count).end(xltoleft).offset(0,1) = cells(r, "A").value
End If
Next使用正确引用的父工作表引用会更好地使用此代码。
发布于 2017-11-24 23:29:41
该解决方案使用AutoFilter、Range.Areas和Arrays,以避免遍历每个单元,提高处理速度。
Sub TEST_Transpose_Alternates_To_Prime()
Dim wsTrg As Worksheet, rgTrg As Range
Dim rgPrime As Range, rgAlter As Range
Dim rgArea As Range, aAlternates As Variant
Dim L As Long
Set wsTrg = ThisWorkbook.Worksheets("DATA") 'Change as required
With wsTrg
Application.Goto .Cells(1), 1
If Not (.AutoFilter Is Nothing) Then .Cells(1).AutoFilter
Set rgTrg = .Cells(6, 2).CurrentRegion.Columns(1) 'Change as required
End With
Rem Set Off Application Properties to improve speed
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With rgTrg
Rem Set Primes Range
.AutoFilter Field:=1, Criteria1:="=*P"
Set rgPrime = .Offset(1).Resize(-1 + .Rows.Count).SpecialCells(xlCellTypeVisible)
Rem Set Alternates Range
.AutoFilter Field:=1, Criteria1:="=*A"
Set rgAlter = .Offset(1).Resize(-1 + .Rows.Count).SpecialCells(xlCellTypeVisible)
Rem Clear Filters
.AutoFilter
End With
Rem Validate Prime & Alternate Ranges
If rgPrime.Areas.Count <> rgAlter.Areas.Count Then Exit Sub
Rem Post Alternates besides each Prime
rgTrg.Cells(1).Offset(0, 1).Value = "Alternates..."
For Each rgArea In rgAlter.Areas
With rgPrime
L = 1 + L
aAlternates = rgArea.Value2
If rgArea.Cells.Count > 1 Then
aAlternates = WorksheetFunction.Transpose(aAlternates)
.Areas(L).Cells(1).Offset(0, 1).Resize(1, UBound(aAlternates)).Value = aAlternates
Else
.Areas(L).Cells(1).Offset(0, 1).Value = aAlternates
End If: End With: Next
Rem Refresh Application Properties
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End Subhttps://stackoverflow.com/questions/47480022
复制相似问题