Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:如何转换这种单元格?_Excel_Vba - Fatal编程技术网

Excel VBA:如何转换这种单元格?

Excel VBA:如何转换这种单元格?,excel,vba,Excel,Vba,我不确定标题是否正确。如果你有更好的主意,请纠正我 这是我的问题:请看图片。 这个excel表格只包含一列,比如ColumnA。在A列中,有些单元格在连续单元格中重复自身两到三次(甚至更多) 我想让excel表格根据这些重复的单元格进行转换。对于重复三次或三次以上的项目,只保留其中两项 [如图右侧所示。原来有三个基站,目标是只保留两个基站并删除其余基站。] 这对我来说是一项非常困难的任务。为了简化操作,转换后不需要删除空行 我们将非常感谢您的任何帮助。谢谢 # 更新: 请看图片。请不要删除再次

我不确定标题是否正确。如果你有更好的主意,请纠正我

这是我的问题:请看图片。

这个excel表格只包含一列,比如ColumnA。在A列中,有些单元格在连续单元格中重复自身两到三次(甚至更多)

我想让excel表格根据这些重复的单元格进行转换。对于重复三次或三次以上的项目,只保留其中两项

[如图右侧所示。原来有三个基站,目标是只保留两个基站并删除其余基站。]

这对我来说是一项非常困难的任务。为了简化操作,转换后不需要删除空行

我们将非常感谢您的任何帮助。谢谢

# 更新:

请看图片。请不要删除再次显示的项目。。。

如果可以删除具有两个以上计数的值,那么我建议这可能会起作用:

Sub count_macro()

Dim a As Integer
Dim b As Integer

a = 1

While Cells(a, 1) <> ""

    b = WorksheetFunction.CountIf(Range("A1:A1000"), Cells(a, 1))

    If b > 2 Then
        Cells(a, 1).Delete Shift:=xlUp
    End If

    b = 0
    a = a + 1

Wend

End Sub
子计数_宏()
将a变暗为整数
作为整数的Dim b
a=1
而单元格(a,1)”
b=工作表函数.CountIf(范围(“A1:A1000”),单元格(a,1))
如果b>2,则
单元格(a,1)。删除移位:=xlUp
如果结束
b=0
a=a+1
温德
端接头

如果可以删除具有两个以上计数的值,则我建议这样做可能有效:

Sub count_macro()

Dim a As Integer
Dim b As Integer

a = 1

While Cells(a, 1) <> ""

    b = WorksheetFunction.CountIf(Range("A1:A1000"), Cells(a, 1))

    If b > 2 Then
        Cells(a, 1).Delete Shift:=xlUp
    End If

    b = 0
    a = a + 1

Wend

End Sub
子计数_宏()
将a变暗为整数
作为整数的Dim b
a=1
而单元格(a,1)”
b=工作表函数.CountIf(范围(“A1:A1000”),单元格(a,1))
如果b>2,则
单元格(a,1)。删除移位:=xlUp
如果结束
b=0
a=a+1
温德
端接头

这应该可以。它从第2行开始接受A列中的输入,直到结束,并忽略2个以上相同的连续值。然后它将它们复制到集合中,并将它们转置粘贴。如果数据位于不同的列和行中,请相应地更改
sourceRange
变量和
i
变量

Sub SETranspose()

Application.ScreenUpdating = False

Dim sourceRange As range
Dim copyRange As range
Dim myCell As range



Set sourceRange = range("A2", Cells(Rows.count, 1).End(xlUp))

Dim startCell As range

Set startCell = sourceRange(1, 1)

Dim i As Integer
Dim haveTwo As Boolean
haveTwo = True

For i = 3 To Cells(Rows.count, 1).End(xlUp).Row + 1

If Cells(i, 1).Value = startCell.Value Then
    If haveTwo Then
        range(startCell, Cells(i, 1)).Copy
        startCell.Offset(0, 4).PasteSpecial Transpose:=True
        Application.CutCopyMode = False
        haveTwo = False
    End If
    End If
    'if the letter changes or end of set, then copy the set over
    'If LCase(Left(Cells(i, 1).Value, 1)) <> LCase(startCell.Value) Or _
                'i = Cells(Rows.count, 1).End(xlUp).Row + 1 Then
    If Len(Cells(i, 1).Value) > 1 Then
        Set copyRange = Cells(i, 1)
        copyRange.Copy
        Cells(startCell.Row, Columns.count).End(xlToLeft).Offset(0, 1).PasteSpecial
        Application.CutCopyMode = False
        'Set startCell = sourceRange(i - 1, 1)
    ElseIf Len(Cells(i, 1).Value) = 1 And Cells(i, 1).Value <> startCell.Value Then
        Set startCell = sourceRange(i - 1, 1)
        haveTwo = True
    End If

Next i

'clear up data
Set sourceRange = Nothing
Set copyRange = Nothing
Set startCell = Nothing

Application.ScreenUpdating = True

End Sub
Sub-settranspose()
Application.ScreenUpdating=False
将源范围变暗为范围
暗拷贝范围作为范围
暗淡的迈塞尔山脉
设置sourceRange=range(“A2”,单元格(Rows.count,1)。结束(xlUp))
暗淡的startCell As范围
设置startCell=sourceRange(1,1)
作为整数的Dim i
Dim haveTwo作为布尔值
haveTwo=True
对于i=3到单元格(Rows.count,1)。结束(xlUp)。Row+1
如果单元格(i,1).Value=startCell.Value,则
如果有两个那么
范围(起始单元格,单元格(i,1))。复制
startCell.Offset(0,4).paste特殊转置:=True
Application.CutCopyMode=False
haveTwo=False
如果结束
如果结束
'如果字母改变或集合结束,则复制集合
'如果LCase(左(单元格(i,1).Value,1))LCase(起始单元格.Value)或_
'i=单元格(Rows.count,1)。结束(xlUp)。然后行+1
如果Len(单元格(i,1).Value)>1,则
设置copyRange=单元格(i,1)
复制范围,复制
单元格(startCell.Row,Columns.count).结束(xlToLeft).偏移量(0,1).粘贴特殊
Application.CutCopyMode=False
'设置startCell=sourceRange(i-1,1)
ElseIf Len(Cells(i,1).Value)=1和Cells(i,1).Value startCell.Value然后
设置startCell=sourceRange(i-1,1)
haveTwo=True
如果结束
接下来我
"清理数据",
设置sourceRange=Nothing
设置copyRange=Nothing
设置startCell=无
Application.ScreenUpdating=True
端接头

这应该可以。它从第2行开始接受A列中的输入,直到结束,并忽略2个以上相同的连续值。然后它将它们复制到集合中,并将它们转置粘贴。如果数据位于不同的列和行中,请相应地更改
sourceRange
变量和
i
变量

Sub SETranspose()

Application.ScreenUpdating = False

Dim sourceRange As range
Dim copyRange As range
Dim myCell As range



Set sourceRange = range("A2", Cells(Rows.count, 1).End(xlUp))

Dim startCell As range

Set startCell = sourceRange(1, 1)

Dim i As Integer
Dim haveTwo As Boolean
haveTwo = True

For i = 3 To Cells(Rows.count, 1).End(xlUp).Row + 1

If Cells(i, 1).Value = startCell.Value Then
    If haveTwo Then
        range(startCell, Cells(i, 1)).Copy
        startCell.Offset(0, 4).PasteSpecial Transpose:=True
        Application.CutCopyMode = False
        haveTwo = False
    End If
    End If
    'if the letter changes or end of set, then copy the set over
    'If LCase(Left(Cells(i, 1).Value, 1)) <> LCase(startCell.Value) Or _
                'i = Cells(Rows.count, 1).End(xlUp).Row + 1 Then
    If Len(Cells(i, 1).Value) > 1 Then
        Set copyRange = Cells(i, 1)
        copyRange.Copy
        Cells(startCell.Row, Columns.count).End(xlToLeft).Offset(0, 1).PasteSpecial
        Application.CutCopyMode = False
        'Set startCell = sourceRange(i - 1, 1)
    ElseIf Len(Cells(i, 1).Value) = 1 And Cells(i, 1).Value <> startCell.Value Then
        Set startCell = sourceRange(i - 1, 1)
        haveTwo = True
    End If

Next i

'clear up data
Set sourceRange = Nothing
Set copyRange = Nothing
Set startCell = Nothing

Application.ScreenUpdating = True

End Sub
Sub-settranspose()
Application.ScreenUpdating=False
将源范围变暗为范围
暗拷贝范围作为范围
暗淡的迈塞尔山脉
设置sourceRange=range(“A2”,单元格(Rows.count,1)。结束(xlUp))
暗淡的startCell As范围
设置startCell=sourceRange(1,1)
作为整数的Dim i
Dim haveTwo作为布尔值
haveTwo=True
对于i=3到单元格(Rows.count,1)。结束(xlUp)。Row+1
如果单元格(i,1).Value=startCell.Value,则
如果有两个那么
范围(起始单元格,单元格(i,1))。复制
startCell.Offset(0,4).paste特殊转置:=True
Application.CutCopyMode=False
haveTwo=False
如果结束
如果结束
'如果字母改变或集合结束,则复制集合
'如果LCase(左(单元格(i,1).Value,1))LCase(起始单元格.Value)或_
'i=单元格(Rows.count,1)。结束(xlUp)。然后行+1
如果Len(单元格(i,1).Value)>1,则
设置copyRange=单元格(i,1)
复制范围,复制
单元格(startCell.Row,Columns.count).结束(xlToLeft).偏移量(0,1).粘贴特殊
Application.CutCopyMode=False
'设置startCell=sourceRange(i-1,1)
ElseIf Len(Cells(i,1).Value)=1和Cells(i,1).Value startCell.Value然后
设置startCell=sourceRange(i-1,1)
haveTwo=True
如果结束
接下来我
"清理数据",
设置sourceRange=Nothing
设置copyRange=Nothing
设置startCell=无
Application.ScreenUpdating=True
端接头

已编辑-请参见下文。假设数据在“表1”中,有序数据写入“结果”。我将重复数据(A、B、C等)命名为sMarker,中间的值命名为sInsideTheMarker。如果标记不连续,代码将失败

Private Sub ReOrderData()
Dim lLastRow As Long
Dim i As Integer
Dim a As Integer
Dim j As Integer
Dim sMarker As String
Dim sInsideTheMarker As String

'Get number of rows with data:
lLastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

j = 0
k = 1
a = 2
'Scan all rows with data:
For i = 1 To lLastRow
    If (Worksheets("Sheet1").Cells(i + 1, 1).Value = Worksheets("Sheet1").Cells(i, 1).Value) Then 'If two consecutive cells holds the same value
        j = j + 1
        If j = 1 Then
            k = k + 1
            a = 2
            sMarker = Worksheets("Sheet1").Cells(i, 1).Value
            Worksheets("Results").Cells(k, 1).Value = sMarker
        End If
    Else 'If not same values in consecutive cells
        sInsideTheMarker = Worksheets("Sheet1").Cells(i, 1).Value
        Worksheets("Results").Cells(k, a).Value = sInsideTheMarker
        a = a + 1
        j = 0
    End If
Next i
End Sub
EDITION:如果希望结果在同一张工作表(“Sheet1”)中,并保留空行以使结果与您的问题完全一致,请尝试以下操作

Private Sub ReOrderData()
Dim lLastRow As Long
Dim i As Integer
Dim a As Integer
Dim j As Integer
Dim sMarker As String
Dim sInsideTheMarker As String

'Get number of rows with data:
lLastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

j = 0
k = 1
a = 5
'Scan all rows with data:
For i = 1 To lLastRow
    If (Worksheets("Sheet1").Cells(i + 1, 1).Value = Worksheets("Sheet1").Cells(i, 1).Value) Then 'If two consecutive cells holds the same value
        j = j + 1
        If j = 1 Then
            k = i
            a = 5
            sMarker = Worksheets("Sheet1").Cells(i, 1).Value
            Worksheets("Sheet1").Cells(k, 4).Value = sMarker
        End If
    Else 'If not same values in consecutive cells
        sInsideTheMarker = Worksheets("Sheet1").Cells(i, 1).Value
        Worksheets("Sheet1").Cells(k, a).Value = sInsideTheMarker
        a = a + 1
        j = 0
    End If
Next i
End Sub

已编辑-请参见下面的尝试此操作。假设数据在“表1”中,有序数据写入“结果”。我将重复数据(A、B、C等)命名为sMarker,中间的值命名为sInsideTheMarker。如果标记不连续,代码将失败

Private Sub ReOrderData()
Dim lLastRow As Long
Dim i As Integer
Dim a As Integer
Dim j As Integer
Dim sMarker As String
Dim sInsideTheMarker As String

'Get number of rows with data:
lLastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

j = 0
k = 1
a = 2
'Scan all rows with data:
For i = 1 To lLastRow
    If (Worksheets("Sheet1").Cells(i + 1, 1).Value = Worksheets("Sheet1").Cells(i, 1).Value) Then 'If two consecutive cells holds the same value
        j = j + 1
        If j = 1 Then
            k = k + 1
            a = 2
            sMarker = Worksheets("Sheet1").Cells(i, 1).Value
            Worksheets("Results").Cells(k, 1).Value = sMarker
        End If
    Else 'If not same values in consecutive cells
        sInsideTheMarker = Worksheets("Sheet1").Cells(i, 1).Value
        Worksheets("Results").Cells(k, a).Value = sInsideTheMarker
        a = a + 1
        j = 0
    End If
Next i
End Sub
EDITION:如果您希望结果在同一张工作表(“Sheet1”)中,并保留空行以使结果与您的问题完全一致,tr