thomassamoth
New Member
- Joined
- Mar 2, 2023
- Messages
- 13
- Office Version
- 365
- Platform
- Windows
For Excel VBA, I am trying to make a code which looks at the value in the column "case type" and let it paste a formula or clear a cell, based on said value in the cell of the column "case type". For Case type 1, Column 1 should contain be cleared of any values and Column 2 should contain Formula B, Column 3 should contain Formula C. For Case type 2, Column 1 should contain Formula A, column 2 should be cleared of any values and column 3 should contain formula C. For case 3, Column 1 should contain Formula A, Column 2 should contain formula B and Column 3 should be cleared of any values. If there is no case, all columns should be cleared. The result will look like this (example):
To accomplish this, I have written the following code:
Instead I get the following result:
I think the reason the values in the first two rows (excluding the header) are wrong, because I made a mistake in the offset function, but I haven't figured out how to fix it. Does someone know what I did wrong? Thanks in advance
Case type | Column X [Irrelevant] | Column 1 | Column 2 | Column 3 |
Case type 1 | Formula B | Formula C | ||
Case type 2 | Formula A | Formula C | ||
Case type 3 | Formula A | Formula B | ||
Case type 1 | Formula B | Formula C |
To accomplish this, I have written the following code:
VBA Code:
Dim tbl As ListObject
Dim cell As Range
Dim CaseType As ListColumn
Dim Column1 As ListColumn
Dim Column2 As ListColumn
Dim Column3 As ListColumn
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
Set tbl = ws.ListObjects(1)
Set CaseType = tbl.ListColumns("Case type")
Set Column1 = tbl.ListColumns("Column 1")
Set Column2 = tbl.ListColumns("Column 2")
Set Column3 = tbl.ListColumns("Column 3")
For Each cell In CaseType.DataBodyRange
Select Case cell.Value
Case "Case1"
cell.Offset(0, Column1.Index - CaseType.Index).ClearContents
cell.Offset(0, Column2.Index - CaseType.Index).Formula = "Formula B"
cell.Offset(0, Column3.Index - CaseType.Index).Formula = "Formula C"
Case "Case 2"
cell.Offset(0, Column1.Index - CaseType.Index).Formula = "Formula A"
cell.Offset(0, Column2.Index - CaseType.Index).ClearContents
cell.Offset(0, Column3.Index - CaseType.Index).Formula = "Formula C"
Case "Case 3"
cell.Offset(0, Column1.Index - CaseType.Index).Formula = "Formula A"
cell.Offset(0, Column2.Index - CaseType.Index).Formula = "Formula B"
cell.Offset(0, Column3.Index - CaseType.Index).ClearContents
Case ""
cell.Offset(0, Column1.Index - CaseType.Index).ClearContents
cell.Offset(0, Column1.Index - CaseType.Index).ClearContents
cell.Offset(0, Column1.Index - CaseType.Index).ClearContents
End Select
Next cell
End Select
Instead I get the following result:
Case type | Column 1 | Column 2 | Column 3 |
Formula A | Formula B | Formula C | |
Case type 1 | Formula A | Formula B | Formula C |
Case type 2 | Formula A | Formula C | |
Case type 3 | Formula A | Formula B | |
Case type 1 | Formula B | Formula C |
I think the reason the values in the first two rows (excluding the header) are wrong, because I made a mistake in the offset function, but I haven't figured out how to fix it. Does someone know what I did wrong? Thanks in advance