VBA CODE REQUIRED

ShoaibAli

Banned - Rules violations
Joined
Jan 15, 2020
Messages
121
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Platform
  1. Windows
Dear Experts,
Here is the Attached file and i have create some Code to do this which is incomplete Please made required changes and update the code according to requirements.

Sheet 1 has Data and Sheet2 has result


VBA Code:
Sub MyVBACODE()
Dim i As Integer
Range("A:A,G:G,I:AG,AI:AZ").EntireColumn.Delete
Columns("A:A").Insert Shift:=xlToRight
For i = 2 To 10000
Cells(i, 1).Value = Cells(i, 4) & ", " & Cells(i, 2) & " " & Cells(i, 3)
Next i
Range("B:D").EntireColumn.Delete
Columns("A:G").Insert Shift:=xlToRight
Range("L:L").Cut Range("A:A")
Range("K:K").Cut Range("D:D")
Range("H:H").Cut Range("E:E")
Range("J:J").Cut Range("F:F")
Range("i:I").Cut Range("G:G")
worksheet.range("
End Sub

1584465767126.png




1584465803252.png



 
Fluff

I did it by Macro please convert it in VBA.

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveCell.FormulaR1C1 = "=YEARFRAC(RC[-1],RC[-3])"
    Range("G2").Select
    Selection.AutoFill Destination:=Range("G2:G159")
    Range("G2:G159").Select
    Selection.Style = "Comma"
    Columns("G:G").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Sub MyVBACODE2()


Application.ScreenUpdating = False
Dim i As Integer
Range("G:G,I:AG,AI:AZ").EntireColumn.Delete
Columns("A:A").Insert Shift:=xlToRight
LastRow = Range("C" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
Cells(i, 5).Value = Trim(Cells(i, 5) & ", " & Cells(i, 3) & " " & Cells(i, 4))
Next i

Range("I:I").Cut Range("A:A")
Range("B:C").ClearContents
Range("H:H").Cut Range("D:D")
Range("F:F").Cut Range("J:J")
Range("F:F").EntireColumn.Delete
'***
Range("G2:G" & LastRow).FormulaR1C1 = "=YEARFRAC(RC[-1],RC[-3])"
'***
Range("G1") = "Age"
Columns("A:I").EntireColumn.AutoFit
Range("H:H").EntireColumn.Delete


Range("I1").Select
ActiveCell.FormulaR1C1 = "Sex"
Range("I2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""Male"",""M"",""F"")"
Range("I2").Select
Selection.AutoFill Destination:=Range("I2:I159")
Range("I2:I159").Select
Columns("I:I").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues

Range("H:H").EntireColumn.Delete
Range("G:G").Select
Selection.Style = "Comma"
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Link is in my first comment.
That is a link to Ozgrid, as we have discussed before, you MUST supply links to ALL sites where you have asked this question.
Please do so.
 
Upvote 0
Sorry i forgot. But after updating same question did not get solution.


 
Upvote 0
Code need to modify. Please help.

VBA Code:
Sub MyVBACODE2()

Application.ScreenUpdating = False

Dim Myrng As Range
Dim i As Integer
Dim Cell As Variant
Dim Source As Range

Range("G:G,I:AG,AI:AZ").EntireColumn.Delete
Columns("A:A").Insert Shift:=xlToRight
LastRow = Range("C" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
Cells(i, 5).Value = Trim(Cells(i, 5) & ", " & Cells(i, 3) & " " & Cells(i, 4))
Next i

Range("I:I").Cut Range("A:A")
Range("B:C").ClearContents
Range("H:H").Cut Range("D:D")
Range("F:F").Cut Range("J:J")
Range("F:F").EntireColumn.Delete

Range("G2:G" & LastRow).FormulaR1C1 = "=YEARFRAC(RC[-1],RC[-3])"

Range("G1") = "Age"
Columns("A:I").EntireColumn.AutoFit
Range("H:H").EntireColumn.Delete
   Range("I1").Select
   ActiveCell.FormulaR1C1 = "Sex"
   Range("I2").Select
  
   ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""Male"",""M"",""F"")"
   Range("I2").Select
   Selection.AutoFill Destination:=Range("I2:I159")
   Range("I2:I159").Select
   Columns("I:I").Select
   Selection.Copy
   Selection.PasteSpecial Paste:=xlPasteValues
   Range("H:H").EntireColumn.Delete
   Range("G:G").Select
   Selection.Style = "Comma"
  
Range("E:E").Select
Set Myrng = Selection
For Each Cell In Myrng
Cell.Value = WorksheetFunction.Proper(Cell)
Next


Set Source = Range("E:E")
Source.Interior.Color = RGB(221, 235, 247)
For Each Cell In Source
    If Application.WorksheetFunction.CountIf(Source, Cell) > 1 Then
        Cell.Interior.Color = RGB(255, 0, 0)
        
    End If
Next
   Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
In what way does it need to be modified?
 
Upvote 0
Fluff

I have added the remaining function but it is taking too much time to show the result.
 
Upvote 0
Try it like
VBA Code:
Sub MyVBACODE2()

Application.ScreenUpdating = False

Dim Myrng As Range
Dim i As Integer
Dim Cell As Variant
Dim Source As Range

Range("G:G,I:AG,AI:AZ").EntireColumn.Delete
Columns("A:A").Insert Shift:=xlToRight
Lastrow = Range("C" & Rows.Count).End(xlUp).Row
For i = 2 To Lastrow
Cells(i, 5).Value = Trim(Cells(i, 5) & ", " & Cells(i, 3) & " " & Cells(i, 4))
Next i

Range("I:I").Cut Range("A:A")
Range("B:C").ClearContents
Range("H:H").Cut Range("D:D")
Range("F:F").Cut Range("J:J")
Range("F:F").EntireColumn.Delete

Range("G2:G" & Lastrow).FormulaR1C1 = "=YEARFRAC(RC[-1],RC[-3])"

Range("G1") = "Age"
Columns("A:I").EntireColumn.AutoFit
Range("H:H").EntireColumn.Delete
   Range("I1").Select
   ActiveCell.FormulaR1C1 = "Sex"
   With Range("I2:I" & Lastrow)
      .FormulaR1C1 = "=IF(RC[-1]=""Male"",""M"",""F"")"
      .Value = .Value
   End With
   Range("H:H").EntireColumn.Delete
   Range("G:G").Select
   Selection.Style = "Comma"
  
Range("E2:E" & Lastrow).Select
Set Myrng = Selection
For Each Cell In Myrng
Cell.Value = WorksheetFunction.Proper(Cell)
Next


Set Source = Range("E2:E" & Lastrow)
Source.Interior.Color = RGB(221, 235, 247)
For Each Cell In Source
    If Application.WorksheetFunction.CountIf(Source, Cell) > 1 Then
        Cell.Interior.Color = RGB(255, 0, 0)
        
    End If
Next
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
Fluff

ThankYou so much Fluff you helping me alot you are true Gem.
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,849
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top