Forum

How To Change An In...
 
Notifications
Clear all

How To Change An Integer Entered In Cell (C5) While Still Keeping Your Formatting Written In VBA

11 Posts
2 Users
0 Reactions
154 Views
(@a-maurizio)
Posts: 214
Reputable Member
Topic starter
 

Hello everyone from Maurizio <br> My
problem and this : I'm trying to recreate a calendar taking advantage of the Excel sheet with Office 2019

Taking as reference the formatting for each cell and Inserted in each cell using only VBA
Such as: To insert Today's Date and all its variants
for its use I wrote these codes for each cell:

'Function For The Start Of The Data Belonging To The Updating Of The Calendar Itself

Sheets("Calendar").Range("A2").FormulaLocal = "=year(A1)"
Sheets("Calendar").Range("A3").FormulaLocal = "=data(A2;A4; 1)"
Sheets("Calendar").Range("A4").FormulaLocal = "=month(A1)"
Sheets("Calendar").Range("A5").FormulaLocal = "= +(A1)"
Sheets("Calendar").Range("H1").FormulaLocal = "=today()"
Sheets("Calendar").Range("C12").Value = VBA .Format(Date, "dddd d mmmm yyyy" & VBA.Format(Time, " - hh:mm: ss"))

And so far so good
But I also used the formula to get the date of the initial month based on the date entered
And in Cell (C5) I wrote this formula:
<br>
Sheets("Calendar").Range("C5").FormulaLocal = "=(A3-WEEKDAY(A3,3))"
And again so far is good .

Now what I would like to know is that of Continue to always keep this formula in cell (C5)
However, having the possibility of transforming its date into an integer number which in this case is the number ( 26 )
How can I do to get all this ...!
Without logically going through its formatting of the Excel sheet
But only using VBA
That's all. Thank you

 
Posted : 07/07/2023 11:53 am
(@keebellah)
Posts: 373
Reputable Member
 

Well, your macro does not work, we do not know what your Data is in "=data(A2;A4; 1)"

and your expalnation is very unclear.

I suggest you to add a sample file with a clear explanation of what you want to see, that will help

 
Posted : 08/07/2023 2:00 am
(@a-maurizio)
Posts: 214
Reputable Member
Topic starter
 

Hi Hans Hallebeek Excuse me if I didn't do it sooner, but at that moment I was in a rush and the nice manners of a self-respecting form like yours slipped my mind:
This is my .
Which in theory would work fine now
The problem is that where I touch with the mouse, it makes all the functions become like numbers, thus making every single procedure written by me as code vanish.
That's all.

 
Posted : 09/07/2023 9:35 am
(@keebellah)
Posts: 373
Reputable Member
 

Hi, I think I know what you're trying to do but you should keep in mind one important thing.
You have hardcoded Italian function names in your macro ( Anno, Mese, etc)  and that will not work with others not using an Italian Office application.

You probably copied this macro form some site and made yopur changes, well, it doesn't work and it's not going to work if I have to rewrite all the code.

You must also understand what the macro is doing and what the implications are when you use WorkSheet_Change option.

What this does is that with EVERY change in the worksheet, the macro restarts so you're in a loop.

You must always stop the error trapping and because of this your macro does not contnue to work either.

In your case you do not have to eneter the day names since if you're using the Italian version just format each date and show the day name as text Format(date, "ddd") or Format(date, "dddd")

ddd = Dom

dddd = Domenica

and so on.

I tried this check and see if it works

 
Posted : 10/07/2023 3:20 am
(@a-maurizio)
Posts: 214
Reputable Member
Topic starter
 

Hello Hans Hallebeek
First I have to say thank you for your suggestions.
But Listen!
The only Macro that I have, as you have also been able to see, is there...! because I wanted to see how the macro compiler would solve it if I Proposed my initial problem to her.
And in some ways he managed to give me a hand, because now by making some changes, everything works the way I want. But then I had to deselect it as you can see
Because it gave me selection problems, in fact it is not called anywhere.

In addition not to contradict your knowledge, but that I know that I use a call to the function that is in Italian I have less; If it works manually great
Why shouldn't it also work calling everything using vba?

That said : What I am trying to create : he simply a calendar created on the excel sheet, using yes ! the cells of the sheet to capture the formatted data.
But instead of using only the Conditional formatting of the sheet itself
It should be able to take the data coming from my VBA written in the form of functions, that's all!

Now by dint of doing, as I said before: My project works well
If I just put these lines of code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo finish

Application.ScreenUpdating = False

With Foglio1

.Range("C12").Value = VBA.Format(Date, "dddd d mmmm yyyy" & VBA.Format(Time, " - hh:mm:ss"))

'Inserimento Nomi Dei Giorni Della Settimana

.Range("C5").Value = "Lun"

.Range("D5").Value = "Mar"

.Range("E5").Value = "Mer"

.Range("F5").Value = "Gio"

.Range("G5").Value = "Ven"

.Range("H5").Value = "Sab"

.Range("I5").Value = "Dom"

'Funzioni Per La Creazione Creazione Dei Numeri Appartenenti Al Calendario

.Range("C6").FormulaLocal = "=(A3-GIORNO.SETTIMANA(A3;3))"

.Range("C6:I11").Select '

.Range("C6:I11").NumberFormat = "d"

.Range("D6").FormulaLocal = "=c6+1"

.Range("E6").FormulaLocal = "=D6+1"

.Range("F6").FormulaLocal = "=E6+1"

.Range("G6").FormulaLocal = "=F6+1"

.Range("H6").FormulaLocal = "=G6+1"

.Range("I6").FormulaLocal = "=H6+1"

.Range("C7").FormulaLocal = "=I6+1"

.Range("D7").FormulaLocal = "=C7+1"

.Range("E7").FormulaLocal = "=D7+1"

.Range("F7").FormulaLocal = "=E7+1"

.Range("G7").FormulaLocal = "=F7+1"

.Range("H7").FormulaLocal = "=G7+1"

.Range("I7").FormulaLocal = "=H7+1"

.Range("C8").FormulaLocal = "=I7+1"

.Range("D8").FormulaLocal = "=C8+1"

.Range("E8").FormulaLocal = "=D8+1"

.Range("F8").FormulaLocal = "=E8+1"

.Range("G8").FormulaLocal = "=F8+1"

.Range("H8").FormulaLocal = "=G8+1"

.Range("I8").FormulaLocal = "=H8+1"

.Range("C9").FormulaLocal = "=I8+1"

.Range("D9").FormulaLocal = "=C9+1"

.Range("E9").FormulaLocal = "=D9+1"

.Range("F9").FormulaLocal = "=E9+1"

.Range("G9").FormulaLocal = "=F9+1"

.Range("H9").FormulaLocal = "=G9+1"

.Range("I9").FormulaLocal = "=H9+1"

.Range("C10").FormulaLocal = "=I9+1"

.Range("D10").FormulaLocal = "=C10+1"

.Range("E10").FormulaLocal = "=D10+1"

.Range("F10").FormulaLocal = "=E10+1"

.Range("G10").FormulaLocal = "=F10+1"

.Range("H10").FormulaLocal = "=G10+1"

.Range("I10").FormulaLocal = "=H10+1"

.Range("C11").FormulaLocal = "=I10+1"

.Range("D11").FormulaLocal = "=C11+1"

Range("A1").Select

End With

Application.ScreenUpdating = True

finish:

However, if I want to insert these functions as a whole, formatted only using VBA:

With Sheet1

'Function For The Start Of The Data Belonging To The Updating Of The Calendar Itself
'.Range("A1").Value = VBA.Format(Now, "dd/mm/yyyy")
'.Range("A2").FormulaLocal = "=year(A1)"
'.Range("A3").FormulaLocal = "=data(A2;A4;1)"
'.Range("A4").FormulaLocal = "=month(A1)"
'.Range("A5").FormulaLocal = "=+(A1)"

'.Range("H1").FormulaLocal = "=today()"

'Current Month Name
'.Range("C4").FormulaLocal = "=+(A1)"

'.Range("C4").Value = VBA.Format([+A1], "mmmm")
'.Selection.NumberFormat = "mmmm"

My calendar creates a huge problem for me that up to now I haven't been able to solve yet which is this:
If I add even these few lines in vba.
And I go back to the excel sheet
All the cells affected by these few lines assume the writing for example: =Year(A1)
But on releasing the mouse from cell (A2)
The cell itself writes me the number (5) and not 2023
And so on for all the others.
While if I exploit only using the formatting of the excel sheet
This doesn't happen.
That's all !

Sure I'll still have to work on it a little bit, but I think it's fixable.
Hello and Thanks again for everything
And good start of the week

 
Posted : 10/07/2023 9:39 am
(@keebellah)
Posts: 373
Reputable Member
 

Happy coding

 
Posted : 11/07/2023 2:29 am
(@a-maurizio)
Posts: 214
Reputable Member
Topic starter
 

Hello Hans Hallebeek I would like to ask you one last thing, if it is possible this is it. keeping all this in mind if I always wanted to take advantage of the vba. Hide the extra numbers of my calendar , how do you think I should proceed ? You have a vague idea all here thanks.

 
Posted : 11/07/2023 4:11 am
(@keebellah)
Posts: 373
Reputable Member
 

Can you be more specific?

 
Posted : 12/07/2023 2:05 am
(@a-maurizio)
Posts: 214
Reputable Member
Topic starter
 

Hello Hans Hallebeek
In Module 2 I created a Function to Hide the extra numbers that don't belong to the selected month.
The problem is that I leave it as Date in Cell (A1) relating to the current month and year; My calendar works great.
However, if I change the bunero of the month that you find in Cell (D1)
You can immediately notice that in the calendar the first numbers of the type (1,2,3) etc...!
They appear only in the lower part of the calendar itself, and then resume the remaining References immediately afterwards in the upper part
And I don't understand why it behaves like this.
Anyway Download this new File With Inside my latest Working Changes ; But only partially.
That's all !
And thanks again for all your effort

 
Posted : 12/07/2023 1:32 pm
(@keebellah)
Posts: 373
Reputable Member
 

I do not see what you want to achieve with all the extra codelines in Module2.

You can do this with conditional formatting

 
Posted : 13/07/2023 3:01 am
(@a-maurizio)
Posts: 214
Reputable Member
Topic starter
 

Hello Hans Hallebeek
No matter how much effort I managed to solve my problem. It was enough just to do something like this

Sub ColoraNumeriMeseAttuale8() On Error GoTo finish Dim ws As Worksheet Dim rng As Range Dim cell As Range Dim meseAttuale As Integer Dim i As Integer ' Imposta il riferimento al foglio di lavoro in cui si trovano i dati Set ws = ThisWorkbook.Sheets("Calendar") ' Imposta il riferimento al range di celle in cui si desidera applicare la formattazione condizionata Set rng = ws.Range("C6:I11") ' Aggiorna con il range di celle desiderato ' Ottieni il mese attuale meseAttuale = Month(ws.Range("A3").Value) Foglio1.Range("C6").FormulaLocal = "=(A3-GIORNO.SETTIMANA(A3;3))" Foglio1.Range("C6:I11").Select Foglio1.Range("C6:I11").NumberFormat = "d" Foglio1.Range("A1").Select ' Nascondi le celle che non appartengono al mese attuale For Each cell In rng If cell.Value <> "" Then If Not (Month(cell.Value) = meseAttuale) Then 'cell.ClearContents cell.Font.Color = RGB(255, 196, 0) cell.Interior.Color = RGB(255, 196, 0) Else cell.Font.Color = RGB(0, 0, 0) cell.Interior.Color = RGB(255, 196, 0) End If End If Next cell finish: End Sub

That's all !
Anyway Thanks Again for Everything Hello and Good Day

 
Posted : 14/07/2023 9:18 am
Share: