Vba vb6和msflexgrid,如何计算剪贴板文本中的列和行
回到vb6和msflexgrid,将文本粘贴到此控件中存在一个弱点。例如,如果用户希望在msflexgrid中粘贴一个2*3数组,他应该选择2行3列来粘贴数据,否则只有一个单元格将填充msflexgrid,而不是6个单元格。 我发现,如果我使用colud分割剪贴板并计算其行和列,这个问题应该可以解决,根据数组大小选择msflexgrid单元格。 我创建了editpasteSUB:Vba vb6和msflexgrid,如何计算剪贴板文本中的列和行,vba,vb6,clipboard,msflexgrid,Vba,Vb6,Clipboard,Msflexgrid,回到vb6和msflexgrid,将文本粘贴到此控件中存在一个弱点。例如,如果用户希望在msflexgrid中粘贴一个2*3数组,他应该选择2行3列来粘贴数据,否则只有一个单元格将填充msflexgrid,而不是6个单元格。 我发现,如果我使用colud分割剪贴板并计算其行和列,这个问题应该可以解决,根据数组大小选择msflexgrid单元格。 我创建了editpasteSUB: Private Sub EditPaste() Dim ARRAYLINES As Variant ' array
Private Sub EditPaste()
Dim ARRAYLINES As Variant ' array with the lines
Dim ARRAYCELLS As Variant ' array with the cells of 1 line to count the cols needed
Dim ARRAYLINESidx As Integer
'§ put clipboard in a textbox named "cliper"
With cliper
.Text = Clipboard.GetText
If .Text = "" Then
Exit Sub
Else
ARRAYLINES = Split(.Text, Chr(13)) 'i also used the Chr(10) vbnewline andvbCRLF to count the lines
End If
End With
'§ put textbox in grid
If ARRAYLINES(0) = "" Then
Exit Sub
Else
ARRAYCELLS = Split(ARRAYLINES(0), vbTab) 'to count the columns
msgbox UBound(ARRAYLINES) & UBound(ARRAYCELLS)
End If
'§ clear array
ReDim ARRAYLINES(0)
ReDim ARRAYCELLS(0)
End Sub
但我的问题是,我有两种类型的文本数组文本矩阵。从msflixgrid到剪贴板的数组和从excell到剪贴板的数组,我无法在该小节中区分它们。下面是从它们到MSword的屏幕截图:
图中的箭头n是制表符,我对它们的计数没有问题,所有文本数组的结果都是相等的。但是段落符号很复杂,我知道在第二个数组中它们是vbnewline,但是在第一个数组中我的代码找不到它们,并且假设我只有一行。
您知道在计算这些列和行时获得相同结果的更好方法吗?我使用以下代码查看剪贴板数据:
'1 form with
' 1 msflexgrid control
' 1 textbox control
' 2 command buttons
Option Explicit
Private Sub Command1_Click()
Dim strText As String
strText = Clipboard.GetText
ShowAscii strText
End Sub
Private Sub Command2_Click()
Clipboard.SetText MSFlexGrid1.Clip
End Sub
Private Sub Form_Load()
Dim intRow As Integer, intCol As Integer
With MSFlexGrid1
For intRow = 0 To .Rows - 1
For intCol = 0 To .Cols - 1
.TextMatrix(intRow, intCol) = CStr(100 * intRow + intCol)
Next intCol
Next intRow
End With 'MSFlexGrid1
End Sub
Private Sub ShowAscii(strText As String)
Dim intChar As Integer
Dim strShow As String
strShow = ""
For intChar = 1 To Len(strText)
strShow = strShow & Right$("00" & Hex$(Asc(Mid$(strText, intChar, 1))), 2) & " "
Next intChar
Text1.Text = strShow
End Sub
当我选择包含200、201、300、301的单元格并单击command2和command1时,文本框显示:
32 30 30 09 32 30 31 0D 33 30 30 09 33 30 31
32 30 30 09 32 30 31 0D 0A 33 30 30 09 33 30 31 0D 0A
当我将相同的数据放入excel并复制它,然后单击command1时,文本框显示:
32 30 30 09 32 30 31 0D 33 30 30 09 33 30 31
32 30 30 09 32 30 31 0D 0A 33 30 30 09 33 30 31 0D 0A
两者之间的区别在于excel使用vbCrLF分隔行,而MSFlexGrid仅使用vbCr
我认为,在处理剪贴板数据之前,从剪贴板中删除所有vbLF时,应该可以:
strText = Replace(strText, vbLf, "")
在这之后,两种输入方法都只使用vbCR作为行分隔符btw:如果不需要,请不要使用变体。。。如果你需要一个字符串数组,你可以将strLines调暗为string在你的示例图片中我看到了阿拉伯数字。我不知道阿拉伯语版本的windows剪贴板和excel是否有不同的工作方式…谢谢你。这个差异和strText=ReplacestrText,vbLf是答案的关键。很高兴它起作用了,而且它与操作系统没有任何区别: