We help IT Professionals succeed at work.

Excel VBA - Type Mismatch - Variable - Trying to Get Last Row

prodempsey
prodempsey asked
on
326 Views
Last Modified: 2016-04-08
I'm trying to get the last row in a single column, but I'm getting a Type Mismatch error at the line of code that say's    arr = Join(arr, ",")
What am I doing wrong?  For some reason this works when I specifically set the arr variable to Range("A1:A50"), but not when I try to dynamically find the last row.

Sub PhoneNumbersWithCommas()
Dim FF
Dim arr
Dim sFileName As String
Dim sDefaultPath As String
'Dim LastRow As Long
'Dim WS As Worksheet


sDefaultPath = Environ$("UserProfile") & "\My Documents\"
sFileName = "PhoneNumbers.txt"
Set WS = Worksheets("Sheet1")

LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row

    arr = WS.Cells(1, LastRow)
   
    arr = Application.WorksheetFunction.Transpose(arr)
   
    arr = Join(arr, ",")
   
    FF = FreeFile()
    Open sDefaultPath & sFileName For Output As #FF
        Print #FF, arr
    Close #FF
   
   
    RetVal = Shell("Notepad.exe " & sDefaultPath & sFileName, 1)
End Sub
Comment
Watch Question

Martin LissSocial distance - Don't touch your face - Wash your hands for 20 seconds
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Put a breakpoint on this line.
arr = WS.Cells(1, LastRow)

What is the value of LastRow? And if you need more help debugging then please see this article of mine.
Martin LissSocial distance - Don't touch your face - Wash your hands for 20 seconds
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
If your way of calculating the LastRow isn't working then try this

LastRow = Range("A1048576").End(xlUp).Row

BTW you have the Dim for LastRow commented out. Why is that?
prodempseyBI Director
CERTIFIED EXPERT
Top Expert 2016

Author

Commented:
The value of LastRow is 3.  I attached a screenshot.
Screenshot_1.png
prodempseyBI Director
CERTIFIED EXPERT
Top Expert 2016

Author

Commented:
I just commented out the Dim for LastRow because I was testing another way to make it work.  I forgot to uncomment it before pasting it into this thread.
prodempseyBI Director
CERTIFIED EXPERT
Top Expert 2016

Author

Commented:
It's storing the correct value for LastRow, I think it's something going on with the arr variable
Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
prodempseyBI Director
CERTIFIED EXPERT
Top Expert 2016

Author

Commented:
Yup! That was it.  Your the man Sktneer.  Thanks for your help again!
Subodh Tiwari (Neeraj)Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome. Glad I could help. :)
prodempseyBI Director
CERTIFIED EXPERT
Top Expert 2016

Author

Commented:
In case anyone is interested, I used this code to transpose a row of phone numbers entered into column A, and output to notepad with commas between each phone number.  Here is the code with the accepted solution, and I attached the final spreadsheet.
Sub PhoneNumbersWithCommas()
Dim FF
Dim arr
Dim sFileName As String
Dim sDefaultPath As String
Dim LastRow As Long
Dim WS As Worksheet
Dim rng As Range


sDefaultPath = Environ$("UserProfile") & "\My Documents\"
sFileName = "PhoneNumbers.txt"
Set WS = Worksheets("Sheet1")



LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row

On Error Resume Next
Set rng = WS.Range(WS.Cells(1, 1), WS.Cells(LastRow, 1)).SpecialCells(xlCellTypeBlanks)

rng.Rows.Delete Shift:=xlShiftUp

    arr = WS.Range(WS.Cells(1, 1), WS.Cells(LastRow, 1))
    
    arr = Application.WorksheetFunction.Transpose(arr)
    
    arr = Join(arr, ",")
    
    FF = FreeFile()
    Open sDefaultPath & sFileName For Output As #FF
        Print #FF, arr
    Close #FF
    
    
    RetVal = Shell("Notepad.exe " & sDefaultPath & sFileName, 1)
End Sub

Open in new window

Phone-Num-to-Commas-Script.xlsm

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.