excelquestion11
New Member
- Joined
- Aug 12, 2022
- Messages
- 1
- Office Version
- 2019
- Platform
- Windows
Hi guys, this is my first time posting on the forum to seek for advises... I learned VBA all from google plus record macro
I am facing a problem which for every month, I have to update an excel based on another excel, and there r really a lot of rows thus consuming a lot of time on this task.
The task is like this:
Sheet1 (original excel) :
Sheet 2 (updated information):
What I will do is:
Therefore, I am looking for VBA code online and searched for a long time ... what I got so far is base on VBA Macro to comment cells according to its value
Sub macro1()
Dim C1 As Range
Set CMwb=ActiveWorkbook.Sheets("Sheet1")
Set BBwb=ActiveWorkbook.Sheets("Sheet2")
BBLastRow=BBwb.Cells(BBwb.Rows.Count, "J").End(xlUp).Row
CMLastRow=CMwb.Cells(CMwb.Rows.Count,"P").End(xlUp).Row
With CreateObject("scripting.dictionary")
For Each C1 In BBwb.Range("F"& BBLastRow)
.Item(C1.Value) = C1.Offset(, 1).Value
Next C1
For Each C1 In CMwb.Range("A"& CMLastRow)
If Not C1.Offset(,1).Comment Is Nothing Then C1.Offset(,1).Comment.Delete
If .Exists(C1.Value) Then
Cl.Offset(,1).AddComment
Cl.Offset(,1).Comment.Text .Item(C1.Value)
End If
Next Cl
End With
End Sub
Sub macro2()
(basically the same as macro 1, just changing the red highlight from 1 to 2)
Sub call macro()
Set target = Sheet2.Range("A2:A8")
Set target2 = Sheet2.range ("B2:B8")
If target.Value = "2022" AND target2.value = "Jan"
Call Macro1
End If
If target.Value = "2022" AND target2.value = "Feb" Then
Call Macro2
If target.Value = "2022" And target2.value = "Mar" Then
.
.
End If
End Sub
I think the above VBA can only add "Extra QTY" to the comment (actually even cant as I dunno y the comment only allows value, eg text, but not numbers) and also I think it cannot support me to call macro for each month as there will be too many macros in the Sub call macro().
I really hope someone could give me some advices on this issue, or will it be impossible to write a vba base on my task nature?
Thank you very much.
I am facing a problem which for every month, I have to update an excel based on another excel, and there r really a lot of rows thus consuming a lot of time on this task.
The task is like this:
Sheet1 (original excel) :
| a | b | c | d | e | f |
1 | Products | Jan-22 | Feb-22 | Mar-22 | Apr-22 | May-22 |
2 | A1 | 200 | 400 | 400 | 100 | 150 |
3 | A2 | 100 | 100 | 300 | 300 | 100 |
4 | A3 | 200 | 200 | 300 | 300 | 200 |
5 | A4 | 100 | 100 | 150 | 100 | 100 |
6 | A5 | 0 | 0 | 0 | 0 | 0 |
7 | A6 | 0 | 0 | 0 | 0 | 0 |
Sheet 2 (updated information):
a | b | c | d | e | f | g | |
1 | Year | Selling month | Channel | Sales type | Status | Products | Extra QTY |
2 | 2022 | Jan | A | 1 off | Confirmed | A1 | 200 |
3 | 2022 | Jan | A | expand | Confirmed | A2 | 400 |
4 | 2022 | Feb | B | 1 off | Confirmed | A3 | 100 |
5 | 2022 | Feb | B | 1 month | Cancelled | A4 | 50 |
6 | 2022 | Mar | C | Special event | Confirmed | A5 | 600 |
7 | 2022 | Mar | C | Special event | Confirmed | A6 | 100 |
8 | 2022 | Mar | A | Special event | Confirmed | A6 | 200 |
What I will do is:
- Start from Sheet 2 Row 1
- Looking at column A, B, E, F, eg 2022 Jan, confirmed, A1
- Go to sheet 1, getcell= column (Jan-22) and rows(A1), and found cell B2 (value 100)
- Add comment in cell B2: A: 200 (values are from sheet2 Channel + Extra Qty, C2 & G2 in this case)
Therefore, I am looking for VBA code online and searched for a long time ... what I got so far is base on VBA Macro to comment cells according to its value
Sub macro1()
Dim C1 As Range
Set CMwb=ActiveWorkbook.Sheets("Sheet1")
Set BBwb=ActiveWorkbook.Sheets("Sheet2")
BBLastRow=BBwb.Cells(BBwb.Rows.Count, "J").End(xlUp).Row
CMLastRow=CMwb.Cells(CMwb.Rows.Count,"P").End(xlUp).Row
With CreateObject("scripting.dictionary")
For Each C1 In BBwb.Range("F"& BBLastRow)
.Item(C1.Value) = C1.Offset(, 1).Value
Next C1
For Each C1 In CMwb.Range("A"& CMLastRow)
If Not C1.Offset(,1).Comment Is Nothing Then C1.Offset(,1).Comment.Delete
If .Exists(C1.Value) Then
Cl.Offset(,1).AddComment
Cl.Offset(,1).Comment.Text .Item(C1.Value)
End If
Next Cl
End With
End Sub
Sub macro2()
(basically the same as macro 1, just changing the red highlight from 1 to 2)
Sub call macro()
Set target = Sheet2.Range("A2:A8")
Set target2 = Sheet2.range ("B2:B8")
If target.Value = "2022" AND target2.value = "Jan"
Call Macro1
End If
If target.Value = "2022" AND target2.value = "Feb" Then
Call Macro2
If target.Value = "2022" And target2.value = "Mar" Then
.
.
End If
End Sub
I think the above VBA can only add "Extra QTY" to the comment (actually even cant as I dunno y the comment only allows value, eg text, but not numbers) and also I think it cannot support me to call macro for each month as there will be too many macros in the Sub call macro().
I really hope someone could give me some advices on this issue, or will it be impossible to write a vba base on my task nature?
Thank you very much.