Column 和 rows 中的数据分开,并且 link 到 value
Separate data in Column and rows, and link to value
如您在此 excel 示例中所见,A 列包含混合数据。
粗体 M 数字是 "keys",蓝色文本是缩进的,蓝色链接到它。我试图弄清楚如何将蓝色文本与粗体 M 数字分开,但在我尝试将此数据插入 SQL 数据库时保持它们的链接。
例如,这是我想要得到的输出,所以我可以将它插入到我的数据库中 table。
我试过将文本添加到列中,但在将它们链接到适当的粗体数字时遇到问题。我认为这是 excel-vba 的要求,但我不知道从哪里开始。任何帮助将不胜感激。
你不需要VBA。如果粗体值始终以 "M" 开头,则执行以下操作:
B1: =A1
B2: =IF(LEFT(A2,1)="M",A2,B1)
C1: =IF(LEFT(A1,1)="M","",A1)
结果中的 B 列和 C 列将根据需要填充数据。
如果你有任何机会需要 VBA
,你可以这样做:
Option Explicit
Public Sub TestMe()
Dim rngCell As Range
Dim colBold As New Collection
Dim colNormal As New Collection
Dim colTimes As New Collection
Dim varMy As Variant
Dim lngCounter As Long
Dim lngSum As Long
Dim lngCur As Long
Dim lngBoldCounter As Long
Dim lngMax As Long
Dim rngMyRange As Range
Set rngMyRange = Range("A1:A20")
For Each rngCell In rngMyRange
If rngCell.Font.Bold Then
colTimes.Add lngSum
colBold.Add rngCell
lngCounter = 0
Else
lngSum = lngSum + 1
lngCounter = lngCounter + 1
colNormal.Add rngCell
End If
Next rngCell
lngCounter = 1
lngSum = 0
lngBoldCounter = 1
lngMax = colBold.Count + 1
rngMyRange.Offset(, 4).Clear
rngMyRange.Offset(, 5).Clear
For Each varMy In colNormal
Cells(lngCounter, 5) = varMy
Cells(lngCounter, 6) = colBold(lngBoldCounter)
If lngCounter < colNormal.Count Then
If colTimes(lngBoldCounter + 1) <= lngCounter Then
lngBoldCounter = lngBoldCounter + 1
If lngBoldCounter = lngMax Then
lngBoldCounter = lngBoldCounter - 1
Debug.Print varMy.Address
End If
End If
End If
lngCounter = lngCounter + 1
Next varMy
End Sub
该代码不是最佳的,因为我是在旅途中制作的,但它考虑了粗体并生成了与它们相关的第 5 列和第 6 列。
如您在此 excel 示例中所见,A 列包含混合数据。
粗体 M 数字是 "keys",蓝色文本是缩进的,蓝色链接到它。我试图弄清楚如何将蓝色文本与粗体 M 数字分开,但在我尝试将此数据插入 SQL 数据库时保持它们的链接。
例如,这是我想要得到的输出,所以我可以将它插入到我的数据库中 table。
我试过将文本添加到列中,但在将它们链接到适当的粗体数字时遇到问题。我认为这是 excel-vba 的要求,但我不知道从哪里开始。任何帮助将不胜感激。
你不需要VBA。如果粗体值始终以 "M" 开头,则执行以下操作:
B1: =A1
B2: =IF(LEFT(A2,1)="M",A2,B1)
C1: =IF(LEFT(A1,1)="M","",A1)
结果中的 B 列和 C 列将根据需要填充数据。
如果你有任何机会需要 VBA
,你可以这样做:
Option Explicit
Public Sub TestMe()
Dim rngCell As Range
Dim colBold As New Collection
Dim colNormal As New Collection
Dim colTimes As New Collection
Dim varMy As Variant
Dim lngCounter As Long
Dim lngSum As Long
Dim lngCur As Long
Dim lngBoldCounter As Long
Dim lngMax As Long
Dim rngMyRange As Range
Set rngMyRange = Range("A1:A20")
For Each rngCell In rngMyRange
If rngCell.Font.Bold Then
colTimes.Add lngSum
colBold.Add rngCell
lngCounter = 0
Else
lngSum = lngSum + 1
lngCounter = lngCounter + 1
colNormal.Add rngCell
End If
Next rngCell
lngCounter = 1
lngSum = 0
lngBoldCounter = 1
lngMax = colBold.Count + 1
rngMyRange.Offset(, 4).Clear
rngMyRange.Offset(, 5).Clear
For Each varMy In colNormal
Cells(lngCounter, 5) = varMy
Cells(lngCounter, 6) = colBold(lngBoldCounter)
If lngCounter < colNormal.Count Then
If colTimes(lngBoldCounter + 1) <= lngCounter Then
lngBoldCounter = lngBoldCounter + 1
If lngBoldCounter = lngMax Then
lngBoldCounter = lngBoldCounter - 1
Debug.Print varMy.Address
End If
End If
End If
lngCounter = lngCounter + 1
Next varMy
End Sub
该代码不是最佳的,因为我是在旅途中制作的,但它考虑了粗体并生成了与它们相关的第 5 列和第 6 列。