PresidentEvil
New Member
- Joined
- Jan 2, 2021
- Messages
- 34
- Office Version
- 2021
- 2016
- Platform
- Windows
Hi,
I'm currently working on a vba macro that does a simple task but not able to get around the solution for a little problem that I'm facing.
I have two columns of data. Text1 is the unqiue ID that is used to fetch data from a database software (using another macro). And Text2 is the data that is fetched.
Looks something like this after data is fetched, just a few samples for example -
This data is copied to another sheet (Step2) where I do the data replacement using Autofilter. Anything that is .0000 will be changed to X and anything other than that is changed to Y in the first visible cell. and then macro uses filldown to drag the values to rest of the cells.
I recorded this macro and edited a few things on my work pc. Now the problem statement:
I'm currently working on a vba macro that does a simple task but not able to get around the solution for a little problem that I'm facing.
I have two columns of data. Text1 is the unqiue ID that is used to fetch data from a database software (using another macro). And Text2 is the data that is fetched.
Looks something like this after data is fetched, just a few samples for example -
This data is copied to another sheet (Step2) where I do the data replacement using Autofilter. Anything that is .0000 will be changed to X and anything other than that is changed to Y in the first visible cell. and then macro uses filldown to drag the values to rest of the cells.
I recorded this macro and edited a few things on my work pc. Now the problem statement:
- If there are only X values (.00 . 000) and no Y values in the list, how do I code it? I know we can use something like 'if' condition there. But not sure how.
- Fill Down/Autofill in the code sometime won't work properly on visible cells (since they are filtered and there are cells in between, hidden).
- Offsetting to first visible cell from B1 and fill down has been difficult when there is only one cell in the list (as this does the fill down to the last row of the sheet, i.e, cell B1048576)
- Autofilter range must be dynamic, as the number of rows can change each time data is fetched.
VBA Code:
Sub Macro1()
'
' Macro1 Macro
'
'
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Step2").Select
ActiveSheet.Paste
Range("A1").Select
Application.CutCopyMode = False
Selection.AutoFilter
ActiveSheet.Range("$A$1:$B$7").AutoFilter Field:=2, Criteria1:= _
".00 . 00000"
Range("B2").Select
ActiveCell.FormulaR1C1 = "X"
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FillDown
ActiveSheet.Range("$A$1:$B$7").AutoFilter Field:=2, Criteria1:="<>X"
Range("B6").Select
ActiveCell.FormulaR1C1 = "Y"
Range("B1").Select
Selection.AutoFilter
End Sub