Consulting

Results 1 to 5 of 5

Thread: Deciphering VBA Code

  1. #1

    Red face Deciphering VBA Code

    Hello,

    I am very new to VBA.

    Could somebody please decipher the following VBA code:

    Capture.jpg

    What exactly does MyConvert do? Thank you!

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    Overall this macro is a quick means of multiplying any number in a cell by 5% and obtaining the answer in the same cell. (Replaces whatever number was there initially).

    The macro was missing a few lines of code. Here is the updated version :

    Sub MyConvert()
        Dim SelRange As Range
        Dim c As Range
        Set SelRange = Selection
        For Each c In SelRange
            c.Select
            If IsNumeric(c) Then
                If Left(ActiveCell.Formula, 1) <> "=" Then
                    ActiveCell.Formula = "=" + ActiveCell.Formula + "*5%"
                End If
            End If
        Next
    End Sub
    Paste the macro into a Routine Module, paste a command button on Sheet1, and enter any number you want into any cell on Sheet1. Click on that cell so it is selected, then click on the button.


    Explanation :

    Sub MyConvert()
    Dim SelRange As Range                                               'establishes SelRange variable as a range
    Set SelRange = Selection                                            'In other words, any cell selected will be the SelRange
    
    
        For Each c In SelRange                                          'For all the cells you select (could be more than one cell)
        c.Select                                                        'Selected cell / s
    
    
        If IsNumeric(c) Then                                            'If the content is a number
    
    
            If Left(ActiveCell.Formula, 1) <> "=" Then                  'If there isn't a formula in the cell already then ...
    
    
                Active.Cell.Formula = "=" + ActiveCell.Formula + "*5%"  'create a formula that multiplies the existing number by 5%
            End If
        End If
         Next
    End Sub

  3. #3

    Thank you!

    Thank you! That's amazing. You're a lifesaver.

    Quote Originally Posted by Logit View Post
    .
    Overall this macro is a quick means of multiplying any number in a cell by 5% and obtaining the answer in the same cell. (Replaces whatever number was there initially).

    The macro was missing a few lines of code. Here is the updated version :

    Sub MyConvert()
        Dim SelRange As Range
        Dim c As Range
        Set SelRange = Selection
        For Each c In SelRange
            c.Select
            If IsNumeric(c) Then
                If Left(ActiveCell.Formula, 1) <> "=" Then
                    ActiveCell.Formula = "=" + ActiveCell.Formula + "*5%"
                End If
            End If
        Next
    End Sub
    Paste the macro into a Routine Module, paste a command button on Sheet1, and enter any number you want into any cell on Sheet1. Click on that cell so it is selected, then click on the button.


    Explanation :

    Sub MyConvert()
    Dim SelRange As Range                                               'establishes SelRange variable as a range
    Set SelRange = Selection                                            'In other words, any cell selected will be the SelRange
    
    
        For Each c In SelRange                                          'For all the cells you select (could be more than one cell)
        c.Select                                                        'Selected cell / s
    
    
        If IsNumeric(c) Then                                            'If the content is a number
    
    
            If Left(ActiveCell.Formula, 1) <> "=" Then                  'If there isn't a formula in the cell already then ...
    
    
                Active.Cell.Formula = "=" + ActiveCell.Formula + "*5%"  'create a formula that multiplies the existing number by 5%
            End If
        End If
         Next
    End Sub

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
      for each it in selection.specialcells(-4123)
    
      next
    End Sub
    suffices.

  5. #5
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    Quote Originally Posted by snb View Post
    Sub M_snb()
      for each it in selection.specialcells(-4123)
    
      next
    End Sub
    suffices.
    ??????

Tags for this Thread

Posting Permissions

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