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". So, for these columns, before the copy paste procedure is done, I change the columns in the excel file to txt, with:
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.
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). 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.
Issue: Right now (after checking many posts on how to properly loop through string arrays, here in SO) I got to this code:
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):
ThisWorkbook.Sheets("Output").Columns(Col_Letter(ColTXTArray(i)))_
.NumberFormat = "@"
Obs2: This is my function to find a column letter based on a number:
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
Col_Letterfunction excepts a Long while you have declaredColTXTArrayas String..Columns(Col_Letter(ColTXTArray(i)))? Why not just do.Columns(CLng(ColTXTArray(i)))? It seems strange to convert a number to a character just so that Excel can look up its mapping table to convert it back to a number again..Columns(Col_Letter(ColTXTArray(i)))to.Columns(Col_Letter(CLng(ColTXTArray(i))))(which would probably work) or did you changelngCol As LongtolngCol As String(which won't work unless you also change.Cells(1, lngCol)to.Cells(1, CLng(lngCol)))