Results 1 to 5 of 5
  1. #1
    Jallie is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    2

    Converting an Excel VBA macro into an Access module?

    I have a macro in Excel which sorts some data for me. I then import the spreadsheet into a table in Access. What I would like to do is to be able to run the macro on the tables inside Access without having to open Excel at all.

    What the Excel macro currently does is it takes a sequence of numbers, it chooses how many characters to retain on either side, and inserts a counter in the middle of them, and chooses which records to include.

    For example, if I have the numbers 1122334455, 2122334455, 3122334455, 4122334455 and I want to retain 2 characters on the right, and 3 characters on the left, and insert 500 in the middle and start from records which begin with 2, then the new sequence would be:

    21500455, 31501455, 41502455. Here is the code:

    Code:
    Option Explicit
    Sub EventIDCode()
    Dim intSRC As Integer
    Dim intDES As Integer
    
    
    Dim strSRC As String
    Dim strDES As String
    
    
    Dim xx As Integer
    Dim yy As Integer
    Dim xt As String
    Dim yt As String
    
    
    Dim strInit As String
    Dim intInit As Integer
    
    
    strSRC = InputBox("Enter the source column", "Source", "A")
    If strSRC = "" Then Exit Sub
    strDES = InputBox("Enter the destination column", "Destination", "B")
    If strDES = "" Then Exit Sub
    xt = InputBox("Enter # characters from the left most column to retain", "", "4")
    If xt = "" Then Exit Sub
    yt = InputBox("Enter # characters from the right most column to retain", "", "5")
    If yt = "" Then Exit Sub
    strInit = InputBox("Enter first number of the series", "", "1")
    If strInit = "" Then Exit Sub
    
    
    xx = Val(xt)
    yy = Val(yt)
    intSRC = sv(strSRC)
    intDES = sv(strDES)
    intInit = Val(strInit)
    
    
    Dim temp As String
    Dim counter As Integer
    counter = 2
    temp = ActiveSheet.Cells(counter, intSRC)
    Do Until temp = ""
        ActiveSheet.Cells(counter, intDES).Value = Left(temp, xx) & four_digit_conv(intInit + counter - 2) & Right(temp, yy)
        counter = counter + 1
        temp = ActiveSheet.Cells(counter, intSRC)
    Loop
    
    
    
    
    
    
    End Sub
    Function four_digit_conv(i As Integer) As String
    Dim temp As String
    temp = Trim(Str(i))
    Select Case Len(Trim(Str(i)))
        Case 1
            four_digit_conv = "000" & temp
        Case 2
            four_digit_conv = "00" & temp
        Case 3
            four_digit_conv = "0" & temp
        Case 4
            four_digit_conv = temp
    End Select
    End Function
    
    
    Function sv(ByVal s As String) As Integer
    Dim temp As Integer
    Dim i As Integer
    
    
    temp = 0
    For i = 1 To Len(s)
        temp = temp * 26 + (Asc(Mid(s, i, 1)) - 64)
    Next i
    sv = temp
    End Function

    Basically, I don't want to open Excel at all, I would like to have this macro working on a data table inside Access, but when I pasted it into the module, it would not run. How can I convert the VBA so it is accepted by Access?

    Any help is appreciated. Thank you so much for your time.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I think you got your left and right backwards.

    The strings can easily be extracted by expression in query, like:

    SELECT *, Left(fieldname,2) AS IDLeft, Right(fieldname,3) AS IDRight FROM tablename WHERE Left(fieldname,1)>1;

    Incrementing is not easily done in query. Review http://www.lebans.com/rownumber.htm

    It is easy to show a sequential number in a report. Textbox in report has a RunningSum property that allows this.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529

  4. #4
    Jallie is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    2
    June7, thank you very much for your reply, I've tried the query and it works great so far. I will try to follow the instructions to add the increment as well. Thanks again!

    alansidman, I'm really sorry. I have read the article now, and I should have read some more general guidelines before posting to the forum. Thank you for the link, from now on I will follow the instructions in the article.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    A VBA procedure could generate that new ID value and save to another field in table. This requires opening the table as a recordset in VBA, cycle through the recordset, manipulate the original value, save new value to record, commit the recordset edits to table.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 01-30-2013, 07:32 PM
  2. Converting Excel IF to Access iiF???
    By robertr in forum Queries
    Replies: 5
    Last Post: 10-14-2011, 05:53 AM
  3. Converting Excel VBA to Access VBA
    By jo15765 in forum Programming
    Replies: 10
    Last Post: 10-13-2011, 07:59 PM
  4. Converting to access from Excel
    By Alexpi in forum Database Design
    Replies: 3
    Last Post: 05-24-2011, 02:46 PM
  5. Converting Excel Macro into Access Module
    By diddyville in forum Modules
    Replies: 1
    Last Post: 03-28-2011, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums