vba vb6 和 msflexgrid,如何计算剪贴板文本中的列和行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15357640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb6 and msflexgrid, how to count columns and rows in clipboard text
提问by David Peterson
Back to vb6 and msflexgrid, there is a weakness in pasting text to this control. If the user wanted for example to paste a 2*3 array in msflexgrid he should select 2 rows and 3 columns to paste the data, otherwise only one cell will fill in the msflexgrid (insted of 6 cells). i found if i colud split clipboard and count its rows and columns this problem should solve (select msflexgrid cells based on the array size) . I created the "editpaste"SUB:
回到 vb6 和 msflexgrid,将文本粘贴到此控件有一个弱点。例如,如果用户想要在 msflexgrid 中粘贴 2*3 数组,他应该选择 2 行 3 列来粘贴数据,否则只有一个单元格将填充 msflexgrid(6 个单元格)。我发现如果我可以拆分剪贴板并计算它的行和列,这个问题应该解决(根据数组大小选择 msflexgrid 单元)。我创建了“编辑粘贴”SUB:
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
But my problem is that i have two types of text arrays (text matrixs). the array that came from msflixgrid to clipboard and the array that came from excell to clipboard and i can not make differencess between them in that sub. bellow is a screenshot from them into MSword:
但我的问题是我有两种类型的文本数组(文本矩阵)。从 msflixgrid 到剪贴板的数组和从 Excel 到剪贴板的数组,我无法在该子中对它们进行区分。下面是从他们到 MSword 的截图:
The arrows n that picture are the TAb Characters i have no problem in counting them and the results are equal for all text arrays. but the paragraph signs are tricky and i knew in the second array they are "vbnewline" but in first array my code can not find them and suppose like i have only one line. Do you know a better way to get equal result in counting these columns and rows?
图片中的箭头 n 是 TAb 字符,我对它们进行计数没有问题,并且所有文本数组的结果都相同。但是段落符号很棘手,我知道在第二个数组中它们是“vbnewline”,但是在第一个数组中,我的代码找不到它们,并且假设我只有一行。您知道在计算这些列和行时获得相同结果的更好方法吗?
回答by Hrqls
I used the following code to view the clipboard data :
我使用以下代码查看剪贴板数据:
'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
When I select the cells with 200, 201, 300, 301 in it and click on command2 and then on command1 then the textbox shows :
当我选择包含 200、201、300、301 的单元格并单击 command2 然后单击 command1 时,文本框显示:
32 30 30 09 32 30 31 0D 33 30 30 09 33 30 31
When I put the same data in excel and copy it, and then click command1 then the textbox shows :
当我将相同的数据放入excel并复制时,然后单击command1然后文本框显示:
32 30 30 09 32 30 31 0D 0A 33 30 30 09 33 30 31 0D 0A
The difference between those 2 are that excel used vbCrLF to separate the rows, while the MSFlexGrid only used vbCr
这两个之间的区别在于,excel 使用 vbCrLF 来分隔行,而 MSFlexGrid 仅使用 vbCr
I think you should be ok when you remove all vbLF from the clipboard data before processing it :
我认为在处理之前从剪贴板数据中删除所有 vbLF 时应该没问题:
strText = Replace(strText, vbLf, "")
After that both input methods only use vbCR as row separators
之后两种输入法都只使用 vbCR 作为行分隔符