Macro for writing to specific Excel spreadsheets

KhallP

Board Regular
Joined
Mar 30, 2021
Messages
157
Office Version
  1. 2016
Platform
  1. Windows
I want to make a macro that just inserts values in cells where the name of the worksheets is different from "Potable C1", "Potable C2", "Potable C3.1", "Potable C3.2", the code is not working because it is writing in all spreadsheets, can someone help me?


VBA Code:
Dim sh As Worksheet
Dim counter As Integer, ws As Integer

Public Sub WriteValues()

    ws = ActiveWorkbook.Sheets.Count

    For counter = 1 To ws
        Worksheets(counter).Select
        Set sh = ActiveSheet
    
        If sh.Name <> "Potável C1" Or sh.Name <> "Potável C2" Or sh.Name <> "Potável C3.1" Or sh.Name <> "Potável C3.2" Then
    
                Range("AC12").Select
                If ActiveCell.Value = "" Then
                    With ActiveSheet.Range("AC12:AE12")
                        .Font.Bold = True
                        .Value = Array("Date", "Time", "Value")
                        .HorizontalAlignment = xlRight
                    End With
            
                Else
                End If

                Range("AC13").Select
                If ActiveCell.Value = "" Then
                    ActiveCell.Value = Range("U13").Value
                    Range("AE13").Value = WorksheetFunction.Sum(Range("W13:W108"))
                    Range("AF13").Value = "kWh"
            
                Else
                End If
        
        Else
        End If
    
    Next counter
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Is this what you mean?

Rich (BB code):
If sh.Name <> "Potável C1" Or sh.Name <> "Potável C2" Or sh.Name <> "Potável C3.1" Or sh.Name <> "Potável C3.2" Then
If sh.Name <> "Potável C1" And sh.Name <> "Potável C2" And sh.Name <> "Potável C3.1" And sh.Name <> "Potável C3.2" Then

BTW, there should be no need to activate each sheet to do this task.
 
Upvote 0
Solution
Is this what you mean?

Rich (BB code):
If sh.Name <> "Potável C1" Or sh.Name <> "Potável C2" Or sh.Name <> "Potável C3.1" Or sh.Name <> "Potável C3.2" Then
If sh.Name <> "Potável C1" And sh.Name <> "Potável C2" And sh.Name <> "Potável C3.1" And sh.Name <> "Potável C3.2" Then

BTW, there should be no need to activate each sheet to do this task.
Thank you very much for your answer, corrected my error, but I need to put "Worksheets (counter) .Select" yes, otherwise it doesn't work
 
Upvote 0
but I need to put "Worksheets (counter) .Select" yes, otherwise it doesn't work
No. You rarely need to select anything in vba to work with it and selecting slows your code.
Here is one way to do what you were doing without selecting any sheets or any cells.

VBA Code:
Sub WriteValues_WithoutSelect()
  Dim ws As Worksheet
  
  For Each ws In ActiveWorkbook.Worksheets
    With ws
      If .Name <> "Potável C1" And .Name <> "Potável C2" And .Name <> "Potável C3.1" And .Name <> "Potável C3.2" Then
        If .Range("AC12").Value = "" Then
          With .Range("AC12:AE12")
            .Font.Bold = True
            .Value = Array("Date", "Time", "Value")
            .HorizontalAlignment = xlRight
          End With
        End If
        If .Range("AC13").Value = "" Then
          .Range("AC13").Value = .Range("U13").Value
          .Range("AE13").Value = WorksheetFunction.Sum(.Range("W13:W108"))
          .Range("AF13").Value = "kWh"
        End If
      End If
    End With
  Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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