Macro / visual basic / Excel


Macro is a set of commands and functions that are stored in a Microsoft Visual Basic and which is 
used in Microsoft Excel. Following examples shows some syntax in VB Macro. Hope fully you are
good in programming. Because I have not explained the code line by line. But if you know you 
a programming code flows then it is nothing to understand. Good luck.

Ex 01:-

Below Methos shows how to split a Excel cel paragraph by a certain word "Buyers Note"


'''''''''''''''''''''''''''''''''''''
Sub BuyersNote()

x = Range("C3").Value
sText = Split(Range("C3").Value, "Note")
Range("D3").Value = sText(1)

End Sub
'''''''''''''''''''''''''''''''''''

Ex 02 : -

Second example shows the sentence starting with a set of words "Manufactures Life Time"
 is to be separated into a new column.

''''''''''''''''''''''''''''''''''''''''
Sub ManufacturesLifeTime()

Times = -1
sText = Split(Range("C4").Value)

For i = 0 To UBound(sText)
       
       If sText(i) = "Time" Then
                        
            If sText(i - 1) = "Life" Then
                If sText(i - 2) = "Manufactures" Then
                Times = 1
                End If
            End If
            
       End If
       
       If Times = 1 Then
        sTexts = Split(Range("C4").Value, "Manufactures")
        Range("D4").Value = "Manufactures" + " " + sTexts(1)
       End If
       
Next i

End Sub

''''''''''''''''''''''''''''''''''''''''


Ex 03 : - Third one shows that The text that starts with a word "Comprising" and 
finish with the start of one of the above strings ("Buyers Note" OR " Manufactures Life Time ") to go into a new column

'''''''''''''''''''''''''''''''''''''''''
Sub Comprision()

Comprising = -1
Manufactures = -1
Buyer = -1
sentence = ""

sText = Split(Range("C5").Value)

For i = 0 To UBound(sText)
       
   If sText(i) = "Comprising" Then
       Comprising = i
   End If
   If sText(i) = "Time" Then
      If sText(i - 1) = "Life" Then
          If sText(i - 2) = "Manufactures" Then
                Manufactures = i
          End If
      End If
   End If
  
Next i

sTexts = Split(Range("C5").Value)
If Comprising > -1 Then
    If Manufactures > -1 Then
        
        For j = Comprising To UBound(sTexts)
            
            If j <= Manufactures Then
                sentence = sentence + " " + sTexts(j)
            End If
            
        Next j
    
    End If
End If
''''''' End of checking the sentenct end with 'Manufactures Life Time' Either go to below for end of
'' buyer

sText = Split(Range("C5").Value)

For i = 0 To UBound(sText)

If Manufactures = -1 Then
     If sText(i) = "Note" Then
         Buyer = i
     End If
End If

Next i

sTexts = Split(Range("C5").Value)
If Comprising > -1 Then
    If Buyer > -1 Then
    
        For j = Comprising To UBound(sTexts)
            
            If j <= Buyer Then
                sentence = sentence + " " + sTexts(j)
            End If
            
        Next j
    
    End If
End If

'' end of buyer
Range("D5").Value = sentence

End Sub

''''''''''''''''''''''''''''''''''''''''''

Comments

Popular Posts