Printing cell value from different sheet to a file

zcopp

New Member
Joined
Aug 3, 2019
Messages
2
When trying to print from the sheet 'CashonHand' I get a error 400. The rest of code works fine without this one line. I googled but unable to find a way to make it work.

TIA, Dennis

Code:
Sub nw()
    Dim afile As String
    afile = "c:\temp\temp1\nw.txt"
    If Len(Dir$(afile)) > 0 Then Kill afile
    Dim str As String
    str = "A15,A12,M12,F6,G6,H6,I6"
    spl = Split(str, ",")
Open "c:\temp\temp1\nw.txt" For Append As #1
    For x = 0 To 6
        Print #1, ActiveSheet.Range(spl(x)).Text
    Next x
           [I][B] Print #1, ActiveWorkbook.Worksheets("CashonHand").Range(“B489”).Text[/B][/I]
Close #1
    ChDir ("C:\Dennis\Software\LapBatch\uploading")
    Shell ("C:\Dennis\Software\LapBatch\uploading\nwUpload.bat")
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi,
You get an error because you cannot print the whole array/range to text file at once. What you try to do is to print values from different ranges in one text line(str = "A15,A12,M12,F6,G6,H6,I6") which is impossible. You should build a text line with values from those ranges first, assign it to string and print the string to txt file.

Regards,
Sebastian

When trying to print from the sheet 'CashonHand' I get a error 400. The rest of code works fine without this one line. I googled but unable to find a way to make it work.

TIA, Dennis

Code:
Sub nw()
    Dim afile As String
    afile = "c:\temp\temp1\nw.txt"
    If Len(Dir$(afile)) > 0 Then Kill afile
    Dim str As String
    str = "A15,A12,M12,F6,G6,H6,I6"
    spl = Split(str, ",")
Open "c:\temp\temp1\nw.txt" For Append As #1
    For x = 0 To 6
        Print #1, ActiveSheet.Range(spl(x)).Text
    Next x
           [I][B] Print #1, ActiveWorkbook.Worksheets("CashonHand").Range(“B489”).Text[/B][/I]
Close #1
    ChDir ("C:\Dennis\Software\LapBatch\uploading")
    Shell ("C:\Dennis\Software\LapBatch\uploading\nwUpload.bat")
End Sub
 
Upvote 0
Hi,
You get an error because you cannot print the whole array/range to text file at once. What you try to do is to print values from different ranges in one text line(str = "A15,A12,M12,F6,G6,H6,I6") which is impossible. You should build a text line with values from those ranges first, assign it to string and print the string to txt file.

Regards,
Sebastian

Sebastion, it prints the cells in the array fine. My problem is this line of code which I highlighted in my original post:

Print #1, ActiveWorkbook.Worksheets("CashonHand").Range(“B489”).Text
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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