Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combine two macros in VBA Excel

I’m trying to combine two macros in Excel.

Both are working separately, but I don’t know how to combine the two.
Both do a loop.

The first one is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Sub UpdateSheet()

Dim shtRng As Range
Dim c As Variant
Dim rng As Range

Set shtRng = Worksheets("SheetB").Range("F2:F39")

For Each c In shtRng
Worksheets("SheetA").Range("CA1:CZ99").Copy Worksheets(c.Value).Range("CA1")


Worksheets(c.Value).Range("CA1:CZ1").Orientation = 90

Worksheets(c.Value).Columns("CA:CZ").ColumnWidth = 5
  
Worksheets(c.Value).Columns("CA:CZ").HorizontalAlignment = xlCenter
Worksheets(c.Value).Columns("CA:CZ").VerticalAlignment = xlBottom
 
Worksheets(c.Value).Columns("CA").Font.Size = 14
Worksheets(c.Value).Columns("CA").Font.Bold = True

Worksheets(c.Value).Range("CB2:CZ2").Font.Size = 14
Worksheets(c.Value).Range("CB2:CZ2").Font.Bold = True


Worksheets(c.Value).Range("CB3:CZ99").Font.Size = 14
Worksheets(c.Value).Range("CB3:CZ99").Font.Bold = True
Worksheets(c.Value).Range("CB3:CZ99").HorizontalAlignment = xlCenter
Worksheets(c.Value).Range("CB3:CZ99").VerticalAlignment = xlCenter

Next c

End Sub

The second one is:

Sub HideEmptyColumns()

Dim rng As Range

Application.ScreenUpdating = False

For Each rng In Worksheets("c.Value").Range("CA1:CZ1")
If rng.Value = "" Then
rng.EntireColumn.Hidden = True
End If
Next rng

End Sub

It is probably very simple for all you wizards, but I’m still learning.

>Solution :

This should get you in the ballpark:

Sub UpdateSheet()

Dim shtRng As Range
Dim c As Variant
Dim rng As Range

Set shtRng = Worksheets("SheetB").Range("F2:F39")

For Each c In shtRng
Worksheets("SheetA").Range("CA1:CZ99").Copy Worksheets(c.Value).Range("CA1")


Worksheets(c.Value).Range("CA1:CZ1").Orientation = 90

Worksheets(c.Value).Columns("CA:CZ").ColumnWidth = 5
  
Worksheets(c.Value).Columns("CA:CZ").HorizontalAlignment = xlCenter
Worksheets(c.Value).Columns("CA:CZ").VerticalAlignment = xlBottom
 
Worksheets(c.Value).Columns("CA").Font.Size = 14
Worksheets(c.Value).Columns("CA").Font.Bold = True

Worksheets(c.Value).Range("CB2:CZ2").Font.Size = 14
Worksheets(c.Value).Range("CB2:CZ2").Font.Bold = True


Worksheets(c.Value).Range("CB3:CZ99").Font.Size = 14
Worksheets(c.Value).Range("CB3:CZ99").Font.Bold = True
Worksheets(c.Value).Range("CB3:CZ99").HorizontalAlignment = xlCenter
Worksheets(c.Value).Range("CB3:CZ99").VerticalAlignment = xlCenter

'Adding other sub's code here:
For Each rng In Worksheets(c.Value).Range("CA1:CZ1")
   If rng.Value = "" Then
     rng.EntireColumn.Hidden = True
   End If
Next rng

Next c

End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading