String 如何在excel中合并虚线
我有一个包含数千行的excel表格。第一列中的某些行已断开。无折线始终具有相邻列,其中包含数据。断线是不行的。换句话说,如何转换以下内容: 为此: 如果我们从以下内容开始: 然后运行以下短宏:String 如何在excel中合并虚线,string,vba,excel,String,Vba,Excel,我有一个包含数千行的excel表格。第一列中的某些行已断开。无折线始终具有相邻列,其中包含数据。断线是不行的。换句话说,如何转换以下内容: 为此: 如果我们从以下内容开始: 然后运行以下短宏: Sub Kompaktor() Dim i As Long, N As Long, IDidSomething As Boolean With Application.WorksheetFunction N = Cells(Rows.Count, 1).End(xlUp
Sub Kompaktor()
Dim i As Long, N As Long, IDidSomething As Boolean
With Application.WorksheetFunction
N = Cells(Rows.Count, 1).End(xlUp).Row
IDidSomething = True
While IDidSomething
IDidSomething = False
For i = N To 2 Step -1
If Cells(i, 1).Value <> "" And .CountA(Range(Cells(i, 2), Cells(i, 7))) = 0 Then
IDidSomething = True
Cells(i - 1, 1).Value = Cells(i - 1, 1).Value & " " & Cells(i, 1).Value
Cells(i, 1).Value = ""
End If
Next i
Wend
End With
End Sub
Sub-Kompaktor()
Dim i为Long,N为Long,idids为Boolean
使用Application.WorksheetFunction
N=单元格(Rows.Count,1)。结束(xlUp)。行
IDidSomething=True
当我做某事时
IDidSomething=False
对于i=N到2步骤-1
如果单元格(i,1).Value“”和.CountA(范围(单元格(i,2),单元格(i,7))=0,则
IDidSomething=True
单元格(i-1,1).Value=单元格(i-1,1).Value&&&&单元格(i,1).Value
单元格(i,1)。Value=“”
如果结束
接下来我
温德
以
端接头
我们将以以下方式结束:
以下是您希望遵循的步骤。在执行任何操作之前,您应该制作电子表格的备份副本
- 打开Excel电子表格
- 确保要处理的工作表是活动工作表
- 这将是一个很好的时间来确保您有原始数据文件的备份
- 单击文件/选项/自定义功能区
- 在右侧的复选框列表中,如果未选中“开发人员”复选框,请选中它
- 单击[确定]
- 单击“开发人员”选项卡
- 双击Visual Basic图标
- 在弹出的窗口中,右键单击当前工作簿名称下的Microsoft Excel对象条目。然后从弹出菜单中选择插入->模块。它应该是这样的:
- 插入工作簿后,此树将更像:
- 您还将有一个子窗口,可能标题为[工作簿名称]-Module1(代码)。这是放置VBA代码的位置。复制以下代码:
- 将复制的代码粘贴到空的Module1窗口中
- 单击行
Dim Ptr As Integer
- 按F5
- 等几秒钟
- 关闭模块。如果您想保存代码,这取决于您,但是您可能永远不会在本工作簿中再次需要它。只需保存对这个问题的引用。。。以防万一
- 关闭Microsoft Visual Basic for Applications窗口
- 验证您的数据
Option Explicit
Sub main()
Dim cell As Range
With Range("A1", Cells(Rows.count, 1).End(xlUp)).SpecialCells(xlCellTypeConstants, xlTextValues)
For Each cell In .Offset(, 1).SpecialCells(xlCellTypeBlanks).Offset(, -1)
With IIf(IsEmpty(cell.Offset(-1)), cell.End(xlUp), cell.Offset(-1))
.Value = IIf(Right(.Value, 1) = "-", Left(.Value, Len(.Value) - 1), .Value & " ") & cell.Value
End With
cell.ClearContents
Next cell
End With
End Sub
这是包含您请求的更改的代码块。我决定替换整个代码块,因为(1)您不知道VBA编码,以及(2)我不知道您实际知道的编码是什么(如果有的话)。对于没有编码经验的人来说,完全替换比编辑更容易
Dim MaxRow As Integer
Sub MergeRows()
Dim Ptr As Integer
Dim I As Integer
Dim WorkStr As String
Dim S As String
Dim Space As String
ActiveSheet.Cells(1, 1).Activate ' Move to the first cell
GetMaxRow ' Get the last row in the worksheet
ActiveSheet.Cells(1, 1).Activate ' Move to the first cell
Ptr = 0
I = 0
For I = 1 To MaxRow
If ActiveSheet.Cells(I, 1).Value > "" Then
If ActiveSheet.Cells(I, 2).Value > "" Then
Ptr = I
Else
If Ptr > 0 Then
Space = " "
WorkStr = ActiveSheet.Cells(Ptr, 1).Value
S = ActiveSheet.Cells(I, 1).Value
If Right(WorkStr, 1) = "-" Then
WorkStr = Left(WorkStr, Len(WorkStr) - 1)
Space = ""
End If
If Left(S, 1) = "-" Then
S = Right(S, Len(S) - 1)
Space = ""
End If
ActiveSheet.Cells(Ptr, 1).Value = WorkStr & IIf(Right(WorkStr, 1) = " " Or Left(S, 1) = " ", "", Space) & S
End If
ActiveSheet.Cells(I, 1).Value = ""
End If
End If
Next I
End Sub
Sub GetMaxRow()
'
MaxRow = ActiveCell.SpecialCells(xlLastCell).Row
End Sub
对我来说,这看起来像是对VBA宏的调用。基本上,你一步一步地穿过这些行。如果在a列和B列中都找到一行,请设置对该行的引用。如果您在a列中发现一行的值,但在B列中找不到该行的值,则取a列中的值,将其附加到上次记录的引用行的a列,并清空当前的a列。继续执行此操作,直到完成为止。我不知道如何使用VBA:(好的,给我一点时间来写一个简单的教程。你使用哪个版本的Excel?我使用Excel 2016。很好的教程材料!哇,太棒了!你能给代码添加两个条件吗:1)如果行用连字符打断,那么删除连字符并合并它们,没有空格。2)如果行没有连字符就中断了,那么将它们与空格合并。谢谢Gary的学生!非常感谢你,维吉尔!
Dim MaxRow As Integer
Sub MergeRows()
Dim Ptr As Integer
Dim I As Integer
Dim WorkStr As String
Dim S As String
Dim Space As String
ActiveSheet.Cells(1, 1).Activate ' Move to the first cell
GetMaxRow ' Get the last row in the worksheet
ActiveSheet.Cells(1, 1).Activate ' Move to the first cell
Ptr = 0
I = 0
For I = 1 To MaxRow
If ActiveSheet.Cells(I, 1).Value > "" Then
If ActiveSheet.Cells(I, 2).Value > "" Then
Ptr = I
Else
If Ptr > 0 Then
Space = " "
WorkStr = ActiveSheet.Cells(Ptr, 1).Value
S = ActiveSheet.Cells(I, 1).Value
If Right(WorkStr, 1) = "-" Then
WorkStr = Left(WorkStr, Len(WorkStr) - 1)
Space = ""
End If
If Left(S, 1) = "-" Then
S = Right(S, Len(S) - 1)
Space = ""
End If
ActiveSheet.Cells(Ptr, 1).Value = WorkStr & IIf(Right(WorkStr, 1) = " " Or Left(S, 1) = " ", "", Space) & S
End If
ActiveSheet.Cells(I, 1).Value = ""
End If
End If
Next I
End Sub
Sub GetMaxRow()
'
MaxRow = ActiveCell.SpecialCells(xlLastCell).Row
End Sub