Personal Workbook Macro Not Operating on Current Worksheet

johnmeyer

New Member
Joined
Oct 23, 2011
Messages
46
Office Version
  1. 2007
Platform
  1. Windows
I have a CSV to Quicken QIF macro that works fine when attached to a workbook, but I want to store it in my personal workbook and then be able to simply open a CSV file by double-clicking on it and then run the macro to create the QIF file.

I am using Excel 2002.

I am pretty sure that my problem has to do with how Excel operates when the macro is not attached to the active workbook.

The highlighted code is my problem. When Excel opens the CSV file, the worksheet is named with the name of that file, and that worksheet is selected. If I step through this macro it fails on the bold line with "Run time error 438."

I have tried dozens of different ways to specify the current workbook (i.e., the CSV file), but always end up failing on this line.

This has got to be stupidly simple, but I've now spent over two hours trying dozens of things, and can't figure it out.

Thanks!!


Code:
Sub CSVtoQIF()
    Dim iFile As Integer
    Dim LastRow As Long
    Dim rng As Range, cell As Range
  [FONT=arial black][B]  Set rng = ActiveWorkbook.Range("All_Cells")[/B][/FONT]
    iFile = FreeFile
    
    Open "e:\temp Quicken BofA.qif" For Output As [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile"]#iFile[/URL]  'Change path to suit
    Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile"]#iFile[/URL] , "!Type:Cash ";
    Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile"]#iFile[/URL] , vbCrLf;
    
    For i = 1 To rng.Rows.Count
      For j = 1 To rng.Columns.Count
        cellValue = rng.Cells(i, j).Value
        If cellValue = "##EOF##" Then
           Exit For
        End If
        Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile"]#iFile[/URL] , cellValue;
        
        If j = rng.Columns.Count Then
            Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile"]#iFile[/URL] , vbCrLf;
            Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile"]#iFile[/URL] , "^";
            Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile"]#iFile[/URL] , vbCrLf;
        Else: Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile"]#iFile[/URL] , vbCrLf;
        End If
      Next j
      If cellValue = "##EOF##" Then
         Exit For
      End If
    Next i
    Print [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile"]#iFile[/URL] , vbCrLf;
    
    Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile"]#iFile[/URL] 
    MsgBox "Finished"
End Sub
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this:

Code:
Set Rng = ActiveWorkbook.Sheets(1).UsedRange
 
Upvote 0
Try this:

Code:
Set Rng = ActiveWorkbook.Sheets(1).UsedRange
That gets me over the hump. Thanks!!!

I think my main problem is that I thought "All_Cells" was a VBA system variable when in fact it was a range name created by some action I took when developing the original macro. That was my brain freeze. By changing to "UsedRange" the macro no longer stops at that point. Now all I need to do is figure out how to get the macro to select all cells in the worksheet, something I'm sure I'll be able to do.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,341
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top