+ Reply to Thread
Results 1 to 5 of 5

Formula in macro, value displayed in TextBox

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    Formula in macro, value displayed in TextBox

    Hi all,

    I need help creating macro for a formula which I have in the attached file. This formula (in cell C2) is generating a product number based on the selected product type in the cell B2, and number of already existing product from that type (range C6:C97).


    The code is made of 12 characters.
    - First characters are allays "Product_"
    - then is followed by a letter which (according the given table in sheet "Pickup_list" in cells H1:O6) describing the product. For example, product 1 always start with letter A, B, C, D or E. Product 2 Always start with F, G, H or I.
    - last 3 characters are alphanumerical which are given subsequently, starting with 0-9 and then continuing from A-Z. For the first product of a type, this characters will be 001, then 002, 003 and so one. After 009 will came 00Z, then 00B, 00C.
    - after are used all combination for Product1 which starts with letter A, we'll move to codes which are starting with letter B (as shown below).

    Product_A001
    Product_A002
    Product_A003

    Product_A009
    Product_ A00A
    Product_ A00B
    Product_ A00Z
    Product_ A010
    Product_ A011

    Product_A01Z
    Product_ A020

    Product_AZZY
    Product_ AZZZ
    Product_ B000
    Product_ B001
    Product_ B003


    The formula in cell C2 in sheet is doing exactly this. But I need instead formula in the sheet, to have formula in macro which will create this code and show it in a TexBox in the UserForm.

    I've inserted in the file a UserForm (activated after is pressed green button named "Show input window"), in which, after selecting the "Product Type" from the drop down menu, I need in the "Product Number" box to be displayed the code based on above mentioned calculation.

    Thank you in advance for the help.
    Igor


    VBA formula.xlsb

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Formula in macro, value displayed in TextBox

    Hi Igor,

    See the attached updated copy of your file, which uses a Macro to:
    a. Update the value in cell 'C3' of Sheet 'Actual situation'.
    b. Updates values in the UserForm.

    Please NOTE that my code was done using Excel 2003, which may have destroyed some of your CONDITIONAL FORMATTING. I suggest you test using my file, then import the UserForm to your file, and cut and paste the following code into your file either from here or from myfile.

    UserForm Code (cut and paste not needed, comes with Importing):
    Private Sub ComboBoxVehicle_Type_Change()
      'This processes a 'ComboBox' Change
      
      Dim sProductType As String
      
      'Get the new 'Product Type'
      sProductType = ComboBoxVehicle_Type.Value
      
      'Update the Global Variables associated with the UserForm
      Call CreateProductCode(sProductType)
    
      'Update the UserForm from the Global Variables
      TextBoxMaxNumberOfCodes.Value = iGblMaxCodes
      TextBoxLastExistingFleet_Number = sGblLastExistingProductCode
      TextBoxNextFleet_Number = sGblNextProductCode
      TextBoxNumberOfCodeInUse = iGblNumberOfCodesInUseForThisProductType
    
    End Sub
    Sheet 'Actual Situation' code module code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
      'This processes a change in the value of cell 'B2'
      If Not Intersect(Target, Range("B2")) Is Nothing Then
        
        Dim sProductType As String
        Dim sNextProductNumber As String
        
        'Get the Product Type from the Input Cell
        sProductType = Target.Value
        
        'Calculate the 'Next Product Number'
        sNextProductNumber = CreateProductCode(sProductType)
        
        'Put the 'Next Product Number' in Cell 'C3'
        Range("C3").Value = sNextProductNumber
        
      End If
    
    End Sub
    Ordinary Code Module Code Module ModCreateProductCode:
    Option Explicit
    
    Public Const sActualDataSheetNAME = "Actual situation"
    Public Const sActualDataSheetDataStartRANGE = "C6:C"
    Public Const sMasterCodeSheetNAME = "Pickup_list"
    
    'Global Variables Used to Update the UserForm
    Public iGblMaxCodes As Long
    Public iGblNumberOfCodesInUseForThisProductType As Long
    Public sGblLastExistingProductCode As String
    Public sGblNextProductCode As String
    
    
    Sub aaaaTestMe()
      'This is used to test function CreateProductCode()
      Dim s As String
      s = CreateProductCode("Product3")
    End Sub
    
    
    Function CreateProductCode(sProductType As String) As String
      'This returns the Next Product Number of a given input Product Type
      'If there is an error an EMPTY STRING "" is returned.
      '
      'This also updates the following Global Variables used by the UserForm
      'a. iGblMaxCodes
      'b. iGblNumberOfCodesInUseForThisProductType
      'c. sGblLastExistingProductCode
      'd. sGblNextProductCode
      '
      'NOTE: Match function is ALWAYS case insensitive
      
      Const nExactMATCH = 0
      
      Dim wsActualData As Worksheet
      Dim wsCodeMaster As Worksheet
      Dim myRangeConstants As Range
      Dim myRangeSingleLetters As Range
      Dim myRangeLetterAndNumberCodes As Range
      
      Dim iIndex1 As Long
      Dim iIndex2 As Long
      Dim iIndex3 As Long
      Dim iIndex4 As Long
      Dim iCodeColumn As Long
      Dim iMaxCodes As Long
      Dim iMaxCodesPerSingleLetter As Long
      Dim iNextCodeNumber As Long
      Dim iNumberOfCodesInUseForThisProductType As Long
      Dim iNumberOfProductSingleLetterCodes As Long
      Dim iNumberOfLetterAndNumberCodes As Long
      Dim iNumbersAndLettersColumn As Long
      Dim iRemainder As String
      Dim iSequenceNumber As Long
      
      Dim s1 As String
      Dim s2 As String
      Dim s3 As String
      Dim s4 As String
      Dim sAddressesInRange As String
      Dim sColumnSingleLetters As String
      Dim sColumnLetterAndNumberCodes As String
      Dim sLastExistingProductCode As String
      Dim sNextProductCode As String
      Dim sRange As String
    
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Create the Worksheet objects
      ''''''''''''''''''''''''''''''''''''''''''''''
      Set wsActualData = ThisWorkbook.Sheets(sActualDataSheetNAME)
      Set wsCodeMaster = ThisWorkbook.Sheets(sMasterCodeSheetNAME)
    
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Find the Column Number in Row 1 that matches the input Product Type
      'A Runtime Error is Generated if there is no match
      ''''''''''''''''''''''''''''''''''''''''''''''
      On Error GoTo MYEXIT
      iCodeColumn = Application.WorksheetFunction.Match(sProductType, wsCodeMaster.Range("1:1"), nExactMATCH)
    
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Get 'Single Letter' Codes Data
      ''''''''''''''''''''''''''''''''''''''''''''''
        
      'Convert the Column to a String
      'Create a Column Range (e.g. 'J:J')
      'Find the Range of Values in the Column (may contain non-contiguous data not in the range)
      'Find the Contiguous Range using the Intersect with the Current Region
      'Get the number of 'Single Letter' Codes
      sColumnSingleLetters = LjmExcelColumnNumberToChar(iCodeColumn)
      sRange = sColumnSingleLetters & ":" & sColumnSingleLetters
      Set myRangeConstants = wsCodeMaster.Range(sRange).SpecialCells(xlCellTypeConstants)
      'Debug.Print myRangeConstants.Address(False, False)
      sAddressesInRange = myRangeConstants.Address(False, False)  '(False, False) removes '$' signs from addresses
      Set myRangeSingleLetters = Intersect(myRangeConstants, wsCodeMaster.Cells(1, sColumnSingleLetters).CurrentRegion)
      'Debug.Print myRangeSingleLetters.Address(False, False)
      iNumberOfProductSingleLetterCodes = myRangeSingleLetters.Count - 1  'Omit Header
      
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Find the Column Number in Row 1 that contains the Code Numbers and Letters
      ''''''''''''''''''''''''''''''''''''''''''''''
      iNumbersAndLettersColumn = Application.WorksheetFunction.Match("Numbers And Letters", wsCodeMaster.Range("1:1"), nExactMATCH)
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Get 'Code Numbers and Letters' Data
      ''''''''''''''''''''''''''''''''''''''''''''''
      
      'Convert the Column to a String
      'Create a Column Range (e.g. 'J:J')
      'Find the Range of Values in the Column (may contain non-contiguous data not in the range)
      'Find the Contiguous Range using the Intersect with the Current Region
      'Get the number of 'Letter and Numbers' Codes
      sColumnLetterAndNumberCodes = LjmExcelColumnNumberToChar(iNumbersAndLettersColumn)
      sRange = sColumnLetterAndNumberCodes & ":" & sColumnLetterAndNumberCodes
      Set myRangeConstants = wsCodeMaster.Range(sRange).SpecialCells(xlCellTypeConstants)
      'Debug.Print myRangeConstants.Address(False, False)
      sAddressesInRange = myRangeConstants.Address(False, False)  '(False, False) removes '$' signs from addresses
      Set myRangeLetterAndNumberCodes = Intersect(myRangeConstants, wsCodeMaster.Cells(1, sColumnLetterAndNumberCodes).CurrentRegion)
      'Debug.Print myRangeLetterAndNumberCodes.Address(False, False)
      iNumberOfLetterAndNumberCodes = myRangeLetterAndNumberCodes.Count - 2   'Omit Header and Last Line
      
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Calculate the Maximum Number of Codes
      ''''''''''''''''''''''''''''''''''''''''''''''
      iMaxCodesPerSingleLetter = iNumberOfLetterAndNumberCodes ^ 3
      iMaxCodes = iNumberOfProductSingleLetterCodes * iMaxCodesPerSingleLetter
    
    
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Get the Number of Codes Already in Use for this 'Product Type'
      ''''''''''''''''''''''''''''''''''''''''''''''
      sRange = sActualDataSheetDataStartRANGE & Rows.Count
      iNumberOfCodesInUseForThisProductType = Application.WorksheetFunction.CountIf(wsActualData.Range(sRange), sProductType)
      
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Get the Current Code Number (Last Existing)
      ''''''''''''''''''''''''''''''''''''''''''''''
      
      If iNumberOfCodesInUseForThisProductType > 0 Then
      
        'NOTE: MINUS 1 is used because the first code offset value is ZERO
        iSequenceNumber = iNumberOfCodesInUseForThisProductType - 1
        iIndex1 = iSequenceNumber \ iMaxCodesPerSingleLetter
        iRemainder = iSequenceNumber - iIndex1 * iMaxCodesPerSingleLetter
    
        iIndex2 = iRemainder \ (iNumberOfLetterAndNumberCodes * iNumberOfLetterAndNumberCodes)
        iRemainder = iRemainder - iIndex2 * (iNumberOfLetterAndNumberCodes * iNumberOfLetterAndNumberCodes)
    
        iIndex3 = iRemainder \ iNumberOfLetterAndNumberCodes
        iRemainder = iRemainder - iIndex3 * iNumberOfLetterAndNumberCodes
    
        iIndex4 = iRemainder
    
        'Get the Values for each digit using Row 2 as a Base
        s1 = wsCodeMaster.Cells(2, iCodeColumn).Offset(iIndex1, 0)
        s2 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex2, 0)
        s3 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex3, 0)
        s4 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex4, 0)
    
        'Create the 'Last Existing' Product Code String
        sLastExistingProductCode = "Product_" & s1 & s2 & s3 & s4
    
      Else
      
        'Create the 'Last Existing' Product Code String
        sLastExistingProductCode = "None"
      
      End If
    
    
    'Set the value of CONDITIONAL COMPILATION Constant 'TEST_CODE_SEQUENCES' to 'True'  to test a SEQUENCE of Product Codes
    'Set the value of CONDITIONAL COMPILATION Constant 'TEST_CODE_SEQUENCES' to 'False' for NORMAL operation
    #Const TEST_CODE_SEQUENCES = False
    #If TEST_CODE_SEQUENCES = True Then
    Dim iX As Long
    For iX = 0 To 50000
    #End If
    
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Get the Next Code Number to Use
      ''''''''''''''''''''''''''''''''''''''''''''''
      iNextCodeNumber = iNumberOfCodesInUseForThisProductType + 1
      
      'NOTE: MINUS 1 is used because the first code offset value is ZERO
      iSequenceNumber = iNextCodeNumber - 1
      iIndex1 = iSequenceNumber \ iMaxCodesPerSingleLetter
      iRemainder = iSequenceNumber - iIndex1 * iMaxCodesPerSingleLetter
    
      iIndex2 = iRemainder \ (iNumberOfLetterAndNumberCodes * iNumberOfLetterAndNumberCodes)
      iRemainder = iRemainder - iIndex2 * (iNumberOfLetterAndNumberCodes * iNumberOfLetterAndNumberCodes)
    
      iIndex3 = iRemainder \ iNumberOfLetterAndNumberCodes
      iRemainder = iRemainder - iIndex3 * iNumberOfLetterAndNumberCodes
    
      iIndex4 = iRemainder
    
      'Get the Values for each digit using Row 2 as a Base
      s1 = wsCodeMaster.Cells(2, iCodeColumn).Offset(iIndex1, 0)
      s2 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex2, 0)
      s3 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex3, 0)
      s4 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex4, 0)
    
      'Create the 'Next' Product Code String
      sNextProductCode = "Product_" & s1 & s2 & s3 & s4
      Debug.Print iNextCodeNumber, sNextProductCode  'Output to Immediate Window (CTRL G in debugger)
      
    #If TEST_CODE_SEQUENCES = True Then
    iNumberOfCodesInUseForThisProductType = iNumberOfCodesInUseForThisProductType + 1
    Next iX
    #End If
    
      'Update the Global Variables for Use by the UserForm
    
      'Put the Data in the UserForm
      iGblMaxCodes = iMaxCodes
      sGblLastExistingProductCode = sLastExistingProductCode
      sGblNextProductCode = sNextProductCode
      iGblNumberOfCodesInUseForThisProductType = iNumberOfCodesInUseForThisProductType
    
      'Set the return value
      CreateProductCode = sNextProductCode
    
    MYEXIT:
      'Clear object pointers
      Set wsActualData = Nothing
      Set wsCodeMaster = Nothing
      Set myRangeConstants = Nothing
      Set myRangeSingleLetters = Nothing
      Set myRangeLetterAndNumberCodes = Nothing
    
    End Function
    
    Private Function LjmExcelColumnNumberToChar(InputColumn As Long) As String
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' This converts an Excel integer column number to "character column letter(s)"
    ' e.g. convert  1 to "A"
    ' e.g. convert 28 to "AB"
    '
    ' This assumes 2 character column limitation of 702 columns = (26 * 27)
    '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      
      If InputColumn > 26 Then
        LjmExcelColumnNumberToChar = Chr(Int((InputColumn - 1) / 26) + 64) & Chr(((InputColumn - 1) Mod 26) + 65)
      Else
        LjmExcelColumnNumberToChar = Chr(InputColumn + 64)
      End If
    
    End Function
    Lewis

  3. #3
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    Re: Formula in macro, value displayed in TextBox

    Hi all,

    I need additional help with the code. The prepared code from LJMetzger works perfectly. But now I need to insert also country prefix, which will identify to which country the product need to go, as shown in the below example.

    Current code : Product_A003
    Required code : Product_BEA003

    Country Country prefix
    Austria AT
    Belgium BE
    ...
    ...
    UK UK
    Ukraine UA

    The country prefix is predetermine. It is in the attached document, in sheet "Pickup_list"

    Thank you in advance for your help.

    Igor

    formula in macro value displayed in textbox - 2015.10.01.xlsm

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Formula in macro, value displayed in TextBox

    Hi Igor,

    The code was changed to meet your new requirements. See the attached file which contains:

    UserForm Code:
    Option Explicit
    
    Private Sub Country_Change()
    
      Dim sValue As String
    
      'Prevent Runtime error caused by manual entry
      'Get the Country Code
      On Error Resume Next
      sValue = Application.VLookup(Country.Value, Range("Country_code"), 2, False)
      If Err.Number = 0 Then
        TextBoxCountryCode = sValue
      Else
        TextBoxCountryCode = ""
      End If
      On Error GoTo 0
    
      'Update the Next Code Number (similar to changing Vehicle Type)
      Call ComboBoxVehicle_Type_Change
    
    End Sub
    
    Private Sub ComboBoxVehicle_Type_Change()
      'This processes a 'ComboBox' Change
      
      Dim sCountryName As String
      Dim sNextProductCode As String
      Dim sProductType As String
      
      'Get the new 'Product Type'
      sProductType = ComboBoxVehicle_Type.Value
      
      'Get the new 'Country Name'
      sCountryName = Country.Value
      
      'Get the Next Product Code
      sNextProductCode = CreateProductCode(sProductType, sCountryName)
    
      'Update the UserForm from the Global Variables
      TextBoxNextFleet_Number = sNextProductCode
    
      'Display or Hide Results Controls as required
      Call DisplayOrHideUserFormControls
      
    End Sub
    
    Private Sub DisplayOrHideUserFormControls()
    
      Dim sCountryCode As String
      Dim sNextProductType As String
      
      'Get the new 'Country Name' (with leading and trailing spaces removed)
      sCountryCode = Trim(TextBoxCountryCode.Value)
      
      'Get the 'Next Product Type' (with leading and trailing spaces removed)
      sNextProductType = Trim(TextBoxNextFleet_Number.Value)
      
      'Display the 'Country Code' only if there is a 'Country Name'
      If Len(sCountryCode) > 0 Then
        LabelCountryCode.Visible = True
        TextBoxCountryCode.Visible = True
      Else
        LabelCountryCode.Visible = False
        TextBoxCountryCode.Visible = False
      End If
      
      
      'Display the Next Product Code only if the value is NOT BLANK
      If Len(sNextProductType) > 0 Then
        LabelNextFleet_Number.Visible = True
        TextBoxNextFleet_Number.Visible = True
      Else
        LabelNextFleet_Number.Visible = False
        TextBoxNextFleet_Number.Visible = False
      End If
      
    End Sub
    Sheet 'Actual Situation' code module code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
      'This processes a change in the value of cell 'B1' or 'B2'
      If Not Intersect(Target, Range("B1:B2")) Is Nothing Then
        
        Dim sCountryName As String
        Dim sProductType As String
        Dim sNextProductNumber As String
        
        'Get the Country Name from the SpreadSheet
        'Get the Product Type from the SpreadSheet
        sCountryName = Range("B1").Value
        sProductType = Range("B2").Value
        
        'Calculate the 'Next Product Number'
        sNextProductNumber = CreateProductCode(sProductType, sCountryName)
        
        'Put the 'Next Product Number' in Cell 'C3'
        Range("C3").Value = sNextProductNumber
        
      End If
    
    End Sub
    Ordinary Code Module Code Module ModCreateProductCode:
    Option Explicit
    
    Public Const sActualDataSheetNAME = "Actual situation"
    Public Const sActualDataSheetDataStartRANGE = "C6:C"
    Public Const sMasterCodeSheetNAME = "Pickup_list"
    
    
    Function CreateProductCode(sProductType As String, sCountryName As String) As String
      'This returns the Next Product Number of a given input Product Type
      'If there is an error an EMPTY STRING "" is returned.
      '
      'NOTE: Match function is ALWAYS case insensitive
      
      Const nExactMATCH = 0
      
      Dim wsActualData As Worksheet
      Dim wsCodeMaster As Worksheet
      Dim myRangeConstants As Range
      Dim myRangeSingleLetters As Range
      Dim myRangeLetterAndNumberCodes As Range
      
      Dim iIndex1 As Long
      Dim iIndex2 As Long
      Dim iIndex3 As Long
      Dim iIndex4 As Long
      Dim iCodeColumn As Long
      Dim iCountryRow As Long
      Dim iCountryNamesColumn As Long
      Dim iMaxCodes As Long
      Dim iMaxCodesPerSingleLetter As Long
      Dim iNextCodeNumber As Long
      Dim iNumberOfCodesInUseForThisProductType As Long
      Dim iNumberOfProductSingleLetterCodes As Long
      Dim iNumberOfLetterAndNumberCodes As Long
      Dim iNumbersAndLettersColumn As Long
      Dim iRemainder As String
      Dim iSequenceNumber As Long
      
      Dim s1 As String
      Dim s2 As String
      Dim s3 As String
      Dim s4 As String
      Dim sAddressesInRange As String
      Dim sColumnSingleLetters As String
      Dim sColumnLetterAndNumberCodes As String
      Dim sCountryCode As String
      Dim sLastExistingProductCode As String
      Dim sNextProductCode As String
      Dim sRange As String
    
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Create the Worksheet objects
      ''''''''''''''''''''''''''''''''''''''''''''''
      Set wsActualData = ThisWorkbook.Sheets(sActualDataSheetNAME)
      Set wsCodeMaster = ThisWorkbook.Sheets(sMasterCodeSheetNAME)
    
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Find the Column Number in Row 1 that matches the input Product Type
      'A Runtime Error is Generated if there is no match
      ''''''''''''''''''''''''''''''''''''''''''''''
      On Error GoTo MYEXIT
      iCodeColumn = Application.WorksheetFunction.Match(sProductType, wsCodeMaster.Range("1:1"), nExactMATCH)
    
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Get 'Single Letter' Codes Data
      ''''''''''''''''''''''''''''''''''''''''''''''
        
      'Convert the Column to a String
      'Create a Column Range (e.g. 'J:J')
      'Find the Range of Values in the Column (may contain non-contiguous data not in the range)
      'Find the Contiguous Range using the Intersect with the Current Region
      'Get the number of 'Single Letter' Codes
      sColumnSingleLetters = LjmExcelColumnNumberToChar(iCodeColumn)
      sRange = sColumnSingleLetters & ":" & sColumnSingleLetters
      Set myRangeConstants = wsCodeMaster.Range(sRange).SpecialCells(xlCellTypeConstants)
      'Debug.Print myRangeConstants.Address(False, False)
      sAddressesInRange = myRangeConstants.Address(False, False)  '(False, False) removes '$' signs from addresses
      Set myRangeSingleLetters = Intersect(myRangeConstants, wsCodeMaster.Cells(1, sColumnSingleLetters).CurrentRegion)
      'Debug.Print myRangeSingleLetters.Address(False, False)
      iNumberOfProductSingleLetterCodes = myRangeSingleLetters.Count - 1  'Omit Header
      
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Find the Column Number in Row 1 that contains the Code Numbers and Letters
      ''''''''''''''''''''''''''''''''''''''''''''''
      iNumbersAndLettersColumn = Application.WorksheetFunction.Match("Numbers And Letters", wsCodeMaster.Range("1:1"), nExactMATCH)
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Get 'Code Numbers and Letters' Data
      ''''''''''''''''''''''''''''''''''''''''''''''
      
      'Convert the Column to a String
      'Create a Column Range (e.g. 'J:J')
      'Find the Range of Values in the Column (may contain non-contiguous data not in the range)
      'Find the Contiguous Range using the Intersect with the Current Region
      'Get the number of 'Letter and Numbers' Codes
      sColumnLetterAndNumberCodes = LjmExcelColumnNumberToChar(iNumbersAndLettersColumn)
      sRange = sColumnLetterAndNumberCodes & ":" & sColumnLetterAndNumberCodes
      Set myRangeConstants = wsCodeMaster.Range(sRange).SpecialCells(xlCellTypeConstants)
      'Debug.Print myRangeConstants.Address(False, False)
      sAddressesInRange = myRangeConstants.Address(False, False)  '(False, False) removes '$' signs from addresses
      Set myRangeLetterAndNumberCodes = Intersect(myRangeConstants, wsCodeMaster.Cells(1, sColumnLetterAndNumberCodes).CurrentRegion)
      'Debug.Print myRangeLetterAndNumberCodes.Address(False, False)
      iNumberOfLetterAndNumberCodes = myRangeLetterAndNumberCodes.Count - 2   'Omit Header and Last Line
      
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Find the Column Number in Row 1 that contains the Country Names
      ''''''''''''''''''''''''''''''''''''''''''''''
      iCountryNamesColumn = Application.WorksheetFunction.Match("Country", wsCodeMaster.Range("1:1"), nExactMATCH)
      
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Get the 'Country Code'
      ''''''''''''''''''''''''''''''''''''''''''''''
      
      'Convert the Column to a String
      'Create a Column Range (e.g. 'J:J')
      'Find the Row that contains the 'Country Name' and 'Country Code'
      
      'Find the Range of Values in the Column (may contain non-contiguous data not in the range)
      'Find the Contiguous Range using the Intersect with the Current Region
      'Get the number of 'Letter and Numbers' Codes
      sColumnLetterAndNumberCodes = LjmExcelColumnNumberToChar(iCountryNamesColumn)
      sRange = sColumnLetterAndNumberCodes & ":" & sColumnLetterAndNumberCodes
      iCountryRow = Application.WorksheetFunction.Match(sCountryName, wsCodeMaster.Range(sRange), nExactMATCH)
      sCountryCode = wsCodeMaster.Cells(iCountryRow, iCountryNamesColumn + 1).Value
      
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Calculate the Maximum Number of Codes
      ''''''''''''''''''''''''''''''''''''''''''''''
      iMaxCodesPerSingleLetter = iNumberOfLetterAndNumberCodes ^ 3
      iMaxCodes = iNumberOfProductSingleLetterCodes * iMaxCodesPerSingleLetter
    
    
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Get the Number of Codes Already in Use for this 'Product Type'
      ''''''''''''''''''''''''''''''''''''''''''''''
      sRange = sActualDataSheetDataStartRANGE & Rows.Count
      iNumberOfCodesInUseForThisProductType = Application.WorksheetFunction.CountIf(wsActualData.Range(sRange), sProductType)
      
      
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Get the Current Code Number (Last Existing)
      ''''''''''''''''''''''''''''''''''''''''''''''
      
      If iNumberOfCodesInUseForThisProductType > 0 Then
      
        'NOTE: MINUS 1 is used because the first code offset value is ZERO
        iSequenceNumber = iNumberOfCodesInUseForThisProductType - 1
        iIndex1 = iSequenceNumber \ iMaxCodesPerSingleLetter
        iRemainder = iSequenceNumber - iIndex1 * iMaxCodesPerSingleLetter
    
        iIndex2 = iRemainder \ (iNumberOfLetterAndNumberCodes * iNumberOfLetterAndNumberCodes)
        iRemainder = iRemainder - iIndex2 * (iNumberOfLetterAndNumberCodes * iNumberOfLetterAndNumberCodes)
    
        iIndex3 = iRemainder \ iNumberOfLetterAndNumberCodes
        iRemainder = iRemainder - iIndex3 * iNumberOfLetterAndNumberCodes
    
        iIndex4 = iRemainder
    
        'Get the Values for each digit using Row 2 as a Base
        s1 = wsCodeMaster.Cells(2, iCodeColumn).Offset(iIndex1, 0)
        s2 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex2, 0)
        s3 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex3, 0)
        s4 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex4, 0)
    
        'Create the 'Last Existing' Product Code String
        sLastExistingProductCode = "Product_" & sCountryCode & s1 & s2 & s3 & s4
    
      Else
      
        'Create the 'Last Existing' Product Code String
        sLastExistingProductCode = "None"
      
      End If
    
    
    'Set the value of CONDITIONAL COMPILATION Constant 'TEST_CODE_SEQUENCES' to 'True'  to test a SEQUENCE of Product Codes
    'Set the value of CONDITIONAL COMPILATION Constant 'TEST_CODE_SEQUENCES' to 'False' for NORMAL operation
    #Const TEST_CODE_SEQUENCES = False
    #If TEST_CODE_SEQUENCES = True Then
    Dim iX As Long
    For iX = 0 To 50000
    #End If
    
      ''''''''''''''''''''''''''''''''''''''''''''''
      'Get the Next Code Number to Use
      ''''''''''''''''''''''''''''''''''''''''''''''
      iNextCodeNumber = iNumberOfCodesInUseForThisProductType + 1
      
      'NOTE: MINUS 1 is used because the first code offset value is ZERO
      iSequenceNumber = iNextCodeNumber - 1
      iIndex1 = iSequenceNumber \ iMaxCodesPerSingleLetter
      iRemainder = iSequenceNumber - iIndex1 * iMaxCodesPerSingleLetter
    
      iIndex2 = iRemainder \ (iNumberOfLetterAndNumberCodes * iNumberOfLetterAndNumberCodes)
      iRemainder = iRemainder - iIndex2 * (iNumberOfLetterAndNumberCodes * iNumberOfLetterAndNumberCodes)
    
      iIndex3 = iRemainder \ iNumberOfLetterAndNumberCodes
      iRemainder = iRemainder - iIndex3 * iNumberOfLetterAndNumberCodes
    
      iIndex4 = iRemainder
    
      'Get the Values for each digit using Row 2 as a Base
      s1 = wsCodeMaster.Cells(2, iCodeColumn).Offset(iIndex1, 0)
      s2 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex2, 0)
      s3 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex3, 0)
      s4 = wsCodeMaster.Cells(2, iNumbersAndLettersColumn).Offset(iIndex4, 0)
    
      'Create the 'Next' Product Code String
      'Code is 'ABCDDD'
      ' A = 'Product_'
      ' B = Two Character Country Code
      ' C = Sequential one Character code associated with a specific product
      ' DDD = Sequential 3 Character code associated with a specific product
      sNextProductCode = "Product_" & sCountryCode & s1 & s2 & s3 & s4
      Debug.Print iNextCodeNumber, sNextProductCode  'Output to Immediate Window (CTRL G in debugger)
      
    #If TEST_CODE_SEQUENCES = True Then
    iNumberOfCodesInUseForThisProductType = iNumberOfCodesInUseForThisProductType + 1
    Next iX
    #End If
    
      'Set the return value
      CreateProductCode = sNextProductCode
    
    MYEXIT:
      'Clear object pointers
      Set wsActualData = Nothing
      Set wsCodeMaster = Nothing
      Set myRangeConstants = Nothing
      Set myRangeSingleLetters = Nothing
      Set myRangeLetterAndNumberCodes = Nothing
    
    End Function
    
    Private Function LjmExcelColumnNumberToChar(InputColumn As Long) As String
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' This converts an Excel integer column number to "character column letter(s)"
    ' e.g. convert  1 to "A"
    ' e.g. convert 28 to "AB"
    '
    ' This assumes 2 character column limitation of 702 columns = (26 * 27)
    '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      
      If InputColumn > 26 Then
        LjmExcelColumnNumberToChar = Chr(Int((InputColumn - 1) / 26) + 64) & Chr(((InputColumn - 1) Mod 26) + 65)
      Else
        LjmExcelColumnNumberToChar = Chr(InputColumn + 64)
      End If
    
    End Function
    Please note that Option Explicit is very important. It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
    Option Explicit
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    Lewis

  5. #5
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    Re: Formula in macro, value displayed in TextBox

    Hi Lewis,

    The code works perfectly!!!!! Thank you a lot for your help!!!

    Cheers.

    Igor

+ 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. Selected data to be displayed in userform textbox
    By cklee in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-12-2015, 09:48 AM
  2. Sum Value Displayed on userform textbox
    By mrswamy2013 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-26-2013, 02:22 PM
  3. [SOLVED] Set number value displayed in cell when certain text value is displayed in another
    By chrisswann in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-18-2013, 09:07 AM
  4. VBA - Macro Code to copy textbox text to another worksheet textbox
    By nitram lowm in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-05-2013, 08:39 AM
  5. Macro to copy textbox data to a duplicate textbox in another worksheet
    By nitram lowm in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-02-2013, 06:10 AM
  6. Currency Displayed in TextBox
    By keenasmustard in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-23-2011, 04:34 AM
  7. Abbreviating path displayed in textbox
    By Road Lizard in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-13-2007, 05:33 PM

Tags for this Thread

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