[英]Looping through string array items error
Scenario: I have a mouse pointer code, that clicks on a certain screen coordinate, copies, then clicks in another coordinate and pastes. 场景:我有一个鼠标指针代码,该代码单击某个屏幕坐标,然后复制,然后单击另一个坐标并粘贴。 My data being copied can change, and some columns have problematic information.
我正在复制的数据可能会更改,并且某些列的信息有问题。
Problem: Some of the columns have data such as "17-2" or "13.5". 问题:一些列中包含诸如“ 17-2”或“ 13.5”之类的数据。 So, for these columns, before the copy paste procedure is done, I change the columns in the excel file to txt, with:
因此,对于这些列,在完成复制粘贴过程之前,我使用以下命令将excel文件中的列更改为txt:
ThisWorkbook.Sheets("Output").Columns("E").NumberFormat = "@"
These problematic columns can change place (be "E" or "J", for example), or even not be present at all. 这些有问题的列可以更改位置(例如,变为“ E”或“ J”),甚至根本不存在。
Objective: I am trying to alter my code, so I get an user input with the column numbers that must be changed to text. 目标:我正在尝试更改代码,因此我得到了用户输入,其中包含必须更改为文本的列号。
Where I got so far: Assuming that I am using a MSGBox to get the user input (in the form of "2/4/5/9", as a string). 到目前为止,我到达的地方:假设我正在使用MSGBox来获取用户输入(以“ 2/4/5/9”的形式作为字符串)。 I first split that into an array, and not I am trying to loop through this array.
我首先将其拆分为一个数组,而不是尝试遍历该数组。 For each number in the array, I use a ColumnLetter function, that gives me the required letter of that, and then I change that column to text.
对于数组中的每个数字,我使用ColumnLetter函数,该函数为我提供所需的字母,然后将该列更改为文本。
Issue: Right now (after checking many posts on how to properly loop through string arrays, here in SO) I got to this code: 问题:现在(在检查了许多有关如何正确遍历字符串数组的文章之后,在SO中),我得到了以下代码:
Dim ColTXT As String
Dim ColTXTArray() As String
Dim i As Long
On Error GoTo ErrorHandler
ColTXT = Application.InputBox("Please enter ascending column numbers to be
changed to text (separated by '/', no blanks)", "Please enter Column
Numbers")
ColTXTArray() = Split(ColTXT, "/")
For i = LBound(ColTXTArray) To UBound(ColTXTArray)
'ColTXTArray (i)
ThisWorkbook.Sheets("Output").Columns(Col_Letter(ColTXTArray(i)))_
.NumberFormat = "@"
Next i
But I am not being able to do this procedure, looping through each item in the array. 但是我无法执行此过程,从而遍历数组中的每个项目。
Question: What would be the best way to do this? 问题:什么是最好的方法?
Obs: In my last run, I got an error in the line (ByRef argument type mismatch): Obs:在上一次运行中,我在该行中遇到了一个错误(ByRef参数类型不匹配):
ThisWorkbook.Sheets("Output").Columns(Col_Letter(ColTXTArray(i)))_
.NumberFormat = "@"
Obs2: This is my function to find a column letter based on a number: Obs2:这是我根据数字查找列字母的功能:
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Worksheets("Output").Cells(1, lngCol).Address(True, False),
"$")
Col_Letter = vArr(0)
End Function
The Columns
collection can be indexed either using a text index, eg Columns("D")
, or by a numeric index, eg Columns(4)
. 可以使用文本索引(例如
Columns("D")
或数字索引(例如Columns(4)
对Columns
集合建立索引。
There is therefore no real need to have a Col_Letter
function in your code, you can just refer to the column using 因此,真正不需要在代码中使用
Col_Letter
函数,您可以使用
ThisWorkbook.Sheets("Output").Columns(CLng(ColTXTArray(i))).NumberFormat = "@"
(The CLng
is converting your String
value, eg "4"
, to a Long
, eg 4
.) (
CLng
将您的String
值(例如"4"
)转换为Long
(例如4
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.