0

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
12
  • Your Col_Letter function excepts a Long while you have declared ColTXTArray as String. Commented Apr 27, 2017 at 9:28
  • @SJR Changed it, now I get an Object Type error. Commented Apr 27, 2017 at 9:32
  • ^^ Plus why do you do .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. Commented Apr 27, 2017 at 9:32
  • @DGMS89 - when you say you changed it, do you mean you changed .Columns(Col_Letter(ColTXTArray(i))) to .Columns(Col_Letter(CLng(ColTXTArray(i)))) (which would probably work) or did you change lngCol As Long to lngCol As String (which won't work unless you also change .Cells(1, lngCol) to .Cells(1, CLng(lngCol)) ) Commented Apr 27, 2017 at 9:37
  • @YowE3K Why, you ask? Because I stupid, hahah. That works perfectly (your first comment), and I have no idea how it works. Care to write it as an answer? Commented Apr 27, 2017 at 9:38

1 Answer 1

2

The Columns collection can be indexed either using a text index, e.g. Columns("D"), or by a numeric index, e.g. Columns(4).

There is therefore no real need to have a Col_Letter function in your code, you can just refer to the column using

ThisWorkbook.Sheets("Output").Columns(CLng(ColTXTArray(i))).NumberFormat = "@"

(The CLng is converting your String value, e.g. "4", to a Long, e.g. 4.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.