• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Issue with Used Range

Dear Friends,


I have the below code to create a textfile on my desktop with a used range.

When I ran the below macro for the first time used range was 805, then it was 600. But still the below code is using the range as 805 only.


Please suggest.


Sub Create_File()

Dim sFile As String

Dim sText As String

Dim iFileNum As Integer

Dim count_val As Long

count_val = Worksheets("Payments").UsedRange.Rows.Count

sFile = "C:Usersrajender_prasadDesktopTextfile.sql"

'MsgBox sText

iFileNum = FreeFile

Open sFile For Output As iFileNum

For i = 4 To count_val

sText = Worksheets("Payments").Range("N" & i).Value & ";"

Print #iFileNum, sText

'Print #iFileNum, "More text on next line"

'Print #iFileNum, "Etc."

Next

Close #iFileNum

MsgBox "file is created " & sFile

End Sub


Regards,

Prasad
 
Prasad


That sounds a bit odd


I would step through the code and check a few variables as you go along using the Watch or intermediate window
 
Hi


You should better to use


With Worksheets("Payments")

count_val = .Range("n" & .Rows.Count).End(3).Row

End With


Also this article might help you to understand Usedrange in detail.


http://www.excelfox.com/forum/f22/replacement-flawed-usedrange-property-374/


Kris
 
Hi Prasad !     Your problem remembers me another one "well known" …


So try to insert a new line like this :

[pre]
Code:
[…]
Dim count_val As Long
Worksheets("Payments").UsedRange
count_val = Worksheets("Payments").UsedRange.Rows.Count
[…]
[/pre]

Microsoft source :   http://support.microsoft.com/kb/163263/en-us


Regards,

Marc
 
Back
Top