+ Reply to Thread
Results 1 to 5 of 5

[Solved] Alternative to MoveFirst (not MoveNext)

  1. #1
    Registered User
    Join Date
    05-15-2021
    Location
    Ireland
    MS-Off Ver
    2021
    Posts
    17

    [Solved] Alternative to MoveFirst (not MoveNext)

    Hi, A little question....

    I have a Macro running at it reads data from a CSV file onto my spreadsheet.

    The first 'If' The first record on the list needs to be the last one on my spreadsheet and so on. I can set the number of records I want to load at once (say 10) and they load no problem with MoveFirst.

    I have a second 'If' statement that moves this data table to the right (so from cell K2 to L2 and so on)and I WANT IT TO read the next row on the CSV (say row 11) and places it in the K column and ccary on. The problem is, moveFirst only takes the first record, MoveNext only takes the 2nd. I want to go from the last record (in this example 10) and at the 11th (so now I should have record 11 in k, 10 in L and so on)

    Perhaps easier phrased: Is there a way to add to the MoveFirst start value??

    I attached a sample csv file too


    PHP Code: 
    Sub Button4_Click()

    Dim sht As Worksheet
    Dim rs 
    As Recordset
    Dim NoOfPrices
    firstPriceColxiaLastRowrowNumcolNumintValcolNumVal As Integer
    Dim curr
    coinfilePathfilenamestrconstrSQL As String
    Dim errDtl
    Dim rangeToMove 
    As Range
    On Error 
    GoTo errDtl

    Application
    .ScreenUpdating False
    Application
    .EnableEvents False

    'Set sheets name
    Set sht = Sheets(1)

    '
    Read currency formatPrice ColumnsFirst Price Column from Sheet
    filePath 
    sht.Range("folderPath").Value
    NoOfPrices 
    sht.Range("NoOfPrices").Value
    firstPriceCol 
    sht.Range("firstPriceCol").Value
    intVal 
    sht.Range("intVal").Value
    colNumVal 
    sht.Range("colNumVal").Value


    If filePath "" Then
        sht
    .Range("message").Value "Kindly provide Folder Path, ending with back slash '\'"
        
    sht.Range("folderPath").Activate
        
    Exit Sub
    ElseIf NoOfPrices "" Then
        sht
    .Range("message").Value "Please enter, how many price columns needed for each coins"
        
    sht.Range("NoOfPrices").Activate
        
    Exit Sub
    ElseIf IsEmpty(firstPriceColThen
        sht
    .Range("message").Value "Please enter first column number to set Price (more then 9) "
        
    sht.Range("firstPriceCol").Activate
        
    Exit Sub
    End 
    If

    If 
    firstPriceCol 10 Then
        sht
    .Range("message").Value "First Price column number should be more then 9"
        
    sht.Range("firstPriceCol") = ""
        
    Exit Sub
    End 
    If



    'Count Symbols from 'C' Column
    LastRow = sht.Range("E8000").End(xlUp).row

    '
    If no symbols provided in sheets
    If LastRow Or IsEmpty(sht.Range("E3")) Then
        sht
    .Range("message").Value "Kindly provide symbol/coin list in 'E3 and onwards cells'"
        
    sht.Range("E3").Activate
        
    Exit Sub
    Else

       
    'set range to move on right side after each interval
        Set rangeToMove = sht.Range(sht.Cells(2, firstPriceCol), sht.Cells(LastRow, (firstPriceCol + NoOfPrices) - 1))

        sht.Range("message").Value = "Processing, Please wait"
        
        
    If intVal <> 1 Then
        
     '
    First of All Write Price Column Heading
    For 0 To NoOfPrices 1
        sht
    .Cells(1firstPriceCol i).Value "Price " 1
    Next i
      
      
      
    For 3 To LastRow
            coin 
    sht.Range("E" x).Value
            filename 
    getFileNameFromFolder(filePathcoin)
            
            If 
    Not IsEmpty(filenameThen
            
                Set rs 
    CreateObject("ADODB.Recordset")
                
    strcon "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" filePath ";" _
                
    "Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
                
                
    strSQL "SELECT * FROM " filename
                
                rs
    .Open strSQLstrcon33
                rs
    .MoveFirst
              
              
                i 
    1
                colNum 
    firstPriceCol NoOfPrices 1
                
                
    For 1 To NoOfPrices
                    
    If <= NoOfPrices Then
                        
    If rs("time") <> "" Then
                            
    'sht.Cells(2, colNum) = unixToDate(rs("time"))
                           sht.Cells(x, colNum) = rs("open")
                            i = i + 1
                           colNum = colNum - 1
                           
                       End If
                    Else
                        Exit For
                    End If
                    rs.MoveNext
                Next a
            End If
            
        Next x

    Else
     
         Range("K2:BCZ100").Select
        Selection.Cut
          Range("L2").Select
        ActiveSheet.Paste
     
    '
    First of All Write Price Column Heading
    For 0 To NoOfPrices
        sht
    .Cells(1firstPriceCol i).Value "Price " 1
    Next i
     
     
     
        
    For 3 To LastRow
            coin 
    sht.Range("E" x).Value
            filename 
    getFileNameFromFolder(filePathcoin)
            
            If 
    Not IsEmpty(filenameThen
            
                Set rs 
    CreateObject("ADODB.Recordset")
                
    strcon "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" filePath ";" _
                
    "Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
                
                
    strSQL "SELECT * FROM " filename
                
                rs
    .Open strSQLstrcon33
                rs
    .MoveFirst
              
              
                
    'i = 1
                colNum = firstPriceCol
                
               
                    
                        If rs("time") <> "" Then
                            sht.Cells(2, colNum) = unixToDate(rs("time"))
                            sht.Cells(x, colNum) = rs("open")
                            
                           
                        End If
                   
                    
                    rs.MoveNext
                    
                  sht.Range("NoOfPrices").Value = NoOfPrices + intVal
                  sht.Range("colNumVal").Value = colNum
               
            End If
            
        Next x
        

                
                
     End If
                
                
                
                
                
                
        '
    Autofit Columns
        rangeToMove
    .EntireColumn.AutoFit
        
        sht
    .Range("message").Value "Data processed/executed successfully"
        
        
    Set rs Nothing
        Set rangeToMove 
    Nothing
        Set sht 
    Nothing
        Application
    .EnableEvents True
        Application
    .ScreenUpdating True
        
    Exit Sub
    End 
    If
    errDtl:
        
    Set rs Nothing
        Set rangeToMove 
    Nothing
        Set sht 
    Nothing
        Sheets
    (1).Range("message").Value Err.Description
        
    Exit Sub
    End Sub


    Private Function unixToDate(lgnDt) As Date
    On Error Resume Next
        unixToDate 
    Format((lgnDt 86400 1000) + 25569"DD/MM/YYYY HH:MM:SS")
    End Function


    Function 
    getFileNameFromFolder(pathfilename) As String
    Dim MyObj 
    As ObjectMySource As Objectfile As Variant
     file 
    Dir(path)
     While (
    file <> "")
        If 
    InStr(fileLCase(filename)) > Or InStr(fileUCase(filename)) > 0 Then
           getFileNameFromFolder 
    file
           
    Exit Function
        
    End If
       
    file Dir
    Wend
    End 
    Function


    Sub Macro3()
    '
    Macro3 Macro
    '

    '

    End Sub 
    Attached Files Attached Files
    Last edited by JoeKerr99; 06-29-2021 at 02:15 PM.

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: Alternative to MoveFirst (not MoveNext)

    Please edit your post and close the code in [code] tags (select the code and click icon [#] ).

    Artik

  3. #3
    Registered User
    Join Date
    05-15-2021
    Location
    Ireland
    MS-Off Ver
    2021
    Posts
    17

    Re: Alternative to MoveFirst (not MoveNext)

    Done thanks

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: Alternative to MoveFirst (not MoveNext)

    I didn't parse the code, but try adding the line after rs.MoveFirst:
    Please Login or Register  to view this content.
    Artik

  5. #5
    Registered User
    Join Date
    05-15-2021
    Location
    Ireland
    MS-Off Ver
    2021
    Posts
    17

    Re: Alternative to MoveFirst (not MoveNext)

    Champion of the WORLD! Thank you SO much!!! That's super awesome!!! Works as I wanted!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Alternative to what/if?
    By backyardfun in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-07-2015, 02:26 PM
  2. [SOLVED] Alternative to what/if??
    By backyardfun in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-05-2015, 01:24 PM
  3. [SOLVED] =NOW() alternative?
    By EXLent in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 10-03-2014, 11:22 PM
  4. Is there an alternative for =NOW()
    By Ido Poelman in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-12-2014, 05:14 PM
  5. Alternative to If
    By leviathan86 in forum Excel General
    Replies: 9
    Last Post: 05-20-2014, 07:09 PM
  6. How to implement MoveNext - Mysql
    By lopezio in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-04-2013, 06:00 PM
  7. alternative to IF
    By mzarallo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-28-2011, 03:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1