PDA

View Full Version : [SOLVED] Deciphering VBA Code



TheTraveler
05-08-2018, 04:46 PM
Hello,

I am very new to VBA.

Could somebody please decipher the following VBA code:

22195

What exactly does MyConvert do? Thank you!

Logit
05-08-2018, 07:52 PM
.
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

TheTraveler
05-09-2018, 03:09 AM
Thank you! That's amazing. You're a lifesaver.


.
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

snb
05-09-2018, 03:26 AM
Sub M_snb()
for each it in selection.specialcells(-4123)

next
End Sub

suffices.

Logit
05-09-2018, 07:05 AM
Sub M_snb()
for each it in selection.specialcells(-4123)

next
End Sub

suffices.

??????